本文介绍了ValueError:图形已断开连接:无法获取张量Tensor的值...访问了以下先前的层,没有出现问题:[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用Keras创建多输入模型,但是出现了错误。这个想法是将文本和相应的主题结合起来,对情绪进行预测。这是代码:

I have been trying to create a multi-input model using Keras, but got errors. The idea is to combine the text and corresonding topics to make predictions for sentiments. Here's the code:

import numpy as np
text = np.random.randint(5000, size=(442702, 200), dtype='int32')
topic = np.random.randint(2, size=(442702, 227), dtype='int32')
sentiment = to_categorical(np.random.randint(5, size=442702), dtype='int32')

from keras.models import Sequential
from keras.layers import Dense, Activation, Embedding, Flatten, GlobalMaxPool1D, Dropout, Conv1D
from keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint
from keras.losses import binary_crossentropy
from keras.optimizers import Adam


text_input = Input(shape=(200,), dtype='int32', name='text')
text_encoded = Embedding(input_dim=5000, output_dim=20, input_length=200)(text_input)
text_encoded = Dropout(0.1)(text_encoded)
text_encoded = Conv1D(300, 3, padding='valid', activation='relu', strides=1)(text_encoded)
text_encoded = GlobalMaxPool1D()(text_encoded)

topic_input = Input(shape=(227,), dtype='int32', name='topic')

concatenated = concatenate([text_encoded, topic_input])
sentiment = Dense(5, activation='softmax')(concatenated)

model = Model(inputs=[text_encoded, topic_input], outputs=sentiment)
# summarize layers
print(model.summary())
# plot graph
plot_model(model)

但是,这给了我以下错误:

However, this gives me the below error:

TypeError:列表中的张量传递给'ConcatV2'Op的'values'的类型[float32,int32]全部匹配。

现在,如果我将topic_input的dtype从'int32'更改为'float32',则会收到另一个错误:

Now if I change dtype of topic_input from 'int32' to 'float32', I got a different error:

ValueError: Graph disconnected: cannot obtain value for tensor Tensor("text_37:0", shape=(?, 200), dtype=int32) at layer "text". The following previous layers were accessed without issue: []

model = Sequential()
model.add(Embedding(5000, 20, input_length=200))
model.add(Dropout(0.1))
model.add(Conv1D(300, 3, padding='valid', activation='relu', strides=1))
model.add(GlobalMaxPool1D())
model.add(Dense(227))
model.add(Activation('sigmoid'))

print(model.summary())

任何指针都受到高度赞赏。

Any pointers are highly appreciated.

推荐答案

Keras功能API的实现几乎没有问题,

There are few issues with your Keras functional API implementation,


  1. 您应将串联层用作串联(axis = -1)([text_encoded,topic_input] )

在连接层中,您尝试将 int32 张量和 float32 张量,这是不允许的。您应该做的是,从keras.backend import cast 和 concatenated = Concatenate(axis = -1)([text_encoded,cast(topic_input,' float32')])

In the concatenate layer you are trying to combine an int32 tensor and a float32 tensor, which is not allowed. What you should do is, from keras.backend import cast and concatenated = Concatenate(axis=-1)([text_encoded, cast(topic_input, 'float32')]).

您遇到了变量冲突,其中有两个情感变量,一个指向 to_categorical 输出,另一个指向最后一个 Dense 层的输出。

You got variable conflicts, there are two sentiment variables, one pointing to a to_categorical output and the other the output of the final Dense layer.

您的模型输入不能是中间张量,例如 text_encoded 。它们应该来自 Input 层。

Your model inputs cannot be intermediate tensors like text_encoded. They should come from Input layers.

您的实现,这是TF 1.13中的代码的有效版本(我不确定这是否正是您想要的)。

To help with your implementation, here's a working version of your code (I am not sure if this is exactly what you wanted though) in TF 1.13.

from keras.utils import to_categorical
text = np.random.randint(5000, size=(442702, 200), dtype='int32')
topic = np.random.randint(2, size=(442702, 227), dtype='int32')
sentiment1 = to_categorical(np.random.randint(5, size=442702), dtype='int32')

from keras.models import Sequential
from keras.layers import Input, Dense, Activation, Embedding, Flatten, GlobalMaxPool1D, Dropout, Conv1D, Concatenate, Lambda
from keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint
from keras.losses import binary_crossentropy
from keras.optimizers import Adam
from keras.backend import cast
from keras.models import Model

text_input = Input(shape=(200,), dtype='int32', name='text')
text_encoded = Embedding(input_dim=5000, output_dim=20, input_length=200)(text_input)
text_encoded = Dropout(0.1)(text_encoded)
text_encoded = Conv1D(300, 3, padding='valid', activation='relu', strides=1)(text_encoded)
text_encoded = GlobalMaxPool1D()(text_encoded)

topic_input = Input(shape=(227,), dtype='int32', name='topic')

topic_float = Lambda(lambda x:cast(x, 'float32'), name='Floatconverter')(topic_input)

concatenated = Concatenate(axis=-1)([text_encoded, topic_float])
sentiment = Dense(5, activation='softmax')(concatenated)

model = Model(inputs=[text_input, topic_input], outputs=sentiment)
# summarize layers
print(model.summary())

希望这些帮助。

这篇关于ValueError:图形已断开连接:无法获取张量Tensor的值...访问了以下先前的层,没有出现问题:[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 00:17