[英]ValueError: Input 0 of layer "sequential" is incompatible with the layer
I am currently implementing my first project using keras/tensorflow, and so far it works pretty good, but now I ran into an error I don't know how to solve.我目前正在使用 keras/tensorflow 实现我的第一个项目,到目前为止它运行良好,但现在我遇到了一个我不知道如何解决的错误。 I googled a lot and found lots of relating questions, but none solved my issue.
我在谷歌上搜索了很多,发现了很多相关问题,但没有一个能解决我的问题。
Here is what I want to achieve:这是我想要实现的目标:
I have a model for a chatbot that I trained with a dataset to provide "standard" conversations like hello, how are u and stuff.我有一个聊天机器人 model,我用数据集对其进行了训练,以提供“标准”对话,例如你好,你好吗等等。 Now I want to "extend" the existing Model with a dataset that provides answers to questions related to shipping, what's in stock etc.
现在我想用一个数据集“扩展”现有的 Model,该数据集提供与运输、库存等相关问题的答案。
Here is my working/already trained model:这是我的工作/已经训练过的 model:
# create Sequential model
model = Sequential()
# add first layer with input shape dependent on size of input and "relu" activation function
model.add(Dense(256, input_shape=(len(training_data_x[0]),), activation=activations.relu))
# add Dropout to prevent overfitting
model.add(Dropout(0.6))
# additional layer with 64 neurons
model.add(Dense(128, activation=activations.relu))
model.add(Dropout(0.2))
# Additional dense layer with num of neurons of classes & softmax activation function
# -> adds results in output layer to "1" to get %
model.add(Dense(len(training_data_y[0]), activation=activations.softmax))
# print(len(training_data_y[0])) = 71
sgd = SGD(learning_rate=0.01, decay=1e-6, momentum=0.9, nesterov=True)
# compile the model
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
output = model.fit(np.array(training_data_x), np.array(training_data_y), epochs=200, batch_size=5, verbose=1)
plot_model_output(output)
model.summary()
model.save('./MyModel_tf', save_format='tf')
The training data is prepared in a separate class and takes a certain json file as input.训练数据在单独的class中准备,并以某个json文件作为输入。
Now I just swapped the JSON file for the one with data related to the stuff I want to add to the model and tried fitting it like this:现在我只是将 JSON 文件交换为包含与我要添加到 model 的内容相关的数据的文件,并尝试像这样安装它:
json_data = json.loads(open('data.json').read())
model = load_model('MyModel_tf')
model.fit(np.array(training_data_x), np.array(training_data_y), epochs=200, batch_size=5, verbose=1)
However when I run it I get this error:但是,当我运行它时出现此错误:
ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 652), found shape=(None, 71)
I am assuming the data is the problem.. however it is structured exactly the same just way shorter.我假设数据是问题所在。但是它的结构完全相同,只是更短。
Now my question(s):现在我的问题:
Any help appreciated!任何帮助表示赞赏!
The problem is probably coming from this line问题可能出在这一行
model.add(Dense(256, input_shape=(len(training_data_x[0]),), activation=activations.relu))
where you define the input shape of your model based on the size of the feature dimension of training_data_x
.您根据
training_data_x
的特征维度的大小定义 model 的输入形状。 Now that you have defined this very specific input shape, all data fed into your model must have the same feature dimension size.现在您已经定义了这个非常具体的输入形状,输入 model 的所有数据必须具有相同的特征维度大小。 That is the reason for your error.
这就是你错误的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.