![](/img/trans.png)
[英]"ValueError: Input 0 of layer "sequential" is incompatible with the layer" In prediction
[英]ValueError: Input 0 of layer "sequential" is incompatible with the layer
我目前正在使用 keras/tensorflow 實現我的第一個項目,到目前為止它運行良好,但現在我遇到了一個我不知道如何解決的錯誤。 我在谷歌上搜索了很多,發現了很多相關問題,但沒有一個能解決我的問題。
這是我想要實現的目標:
我有一個聊天機器人 model,我用數據集對其進行了訓練,以提供“標准”對話,例如你好,你好嗎等等。 現在我想用一個數據集“擴展”現有的 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')
訓練數據在單獨的class中准備,並以某個json文件作為輸入。
現在我只是將 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)
但是,當我運行它時出現此錯誤:
ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 652), found shape=(None, 71)
我假設數據是問題所在。但是它的結構完全相同,只是更短。
現在我的問題:
任何幫助表示贊賞!
問題可能出在這一行
model.add(Dense(256, input_shape=(len(training_data_x[0]),), activation=activations.relu))
您根據training_data_x
的特征維度的大小定義 model 的輸入形狀。 現在您已經定義了這個非常具體的輸入形狀,輸入 model 的所有數據必須具有相同的特征維度大小。 這就是你錯誤的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.