我试图让 LSTM 模型继续运行,因为它的最后一次运行停止了。在我尝试适应网络之前,一切都可以正常编译。然后它给出一个错误:ValueError:检查目标时出错:预期dense_29具有3维,但得到形状为(672, 1)的数组我检查了诸如this和this之类的各种文章, 但我没有看到我的代码有什么问题。from keras import Sequentialfrom keras.preprocessing.sequence import pad_sequencesfrom sklearn.model_selection import train_test_splitfrom keras.models import Sequential,Modelfrom keras.layers import LSTM, Dense, Bidirectional, Input,Dropout,BatchNormalizationfrom keras import backend as Kfrom keras.engine.topology import Layerfrom keras import initializers, regularizers, constraintsfrom keras.callbacks import ModelCheckpointfrom keras.models import load_modelimport os.pathimport osfilepath="Train-weights.best.hdf5"act = 'relu'model = Sequential()model.add(BatchNormalization(input_shape=(10, 128)))model.add(Bidirectional(LSTM(128, dropout=0.5, activation=act, return_sequences=True)))model.add(Dense(1,activation='sigmoid'))if (os.path.exists(filepath)): print("extending training of previous run") model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) with open('model_architecture.json', 'r') as f: model = model_from_json(f.read()) model.load_weights(filepath)else: print("First run") model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) model.fit(x_train, y_train, validation_data=(x_val, y_val), epochs=100, batch_size=32, callbacks=callbacks_list, verbose=2) model.save_weights(filepath) with open('model_architecture.json', 'w') as f: f.write(model.to_json()) checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max') callbacks_list = [checkpoint] model.fit(x_train, y_train, validation_data=(x_val, y_val), epochs=100, batch_size=32, callbacks=callbacks_list, verbose=0)
1 回答
holdtom
TA贡献1805条经验 获得超10个赞
尝试一下model.summary()
,您会看到网络中最后一层(即 Dense 层)的输出形状是(None, 10, 1)
。因此,您提供给模型的标签(即y_train
)也必须具有 形状(num_samples, 10, 1)
。
如果输出形状(None, 10, 1)
不是您想要的(例如,您想要(None, 1)
作为模型的输出形状),那么您需要修改您的模型定义。实现这一目标的一个简单修改是return_sequences=True
从 LSTM 层中删除参数。
添加回答
举报
0/150
提交
取消