【发布时间】:2018-11-21 13:47:26
【问题描述】:
我尝试让 LSTM 模型在其最后一次运行停止的情况下继续运行。在我尝试适应网络之前,一切都编译得很好。然后它给出了一个错误:
ValueError: 检查目标时出错:预期 dense_29 具有 3 个维度,但得到的数组形状为 (672, 1)
我查看了各种文章,例如 this 和 this,但我看不出我的代码有什么问题。
from keras import Sequential
from keras.preprocessing.sequence import pad_sequences
from sklearn.model_selection import train_test_split
from keras.models import Sequential,Model
from keras.layers import LSTM, Dense, Bidirectional, Input,Dropout,BatchNormalization
from keras import backend as K
from keras.engine.topology import Layer
from keras import initializers, regularizers, constraints
from keras.callbacks import ModelCheckpoint
from keras.models import load_model
import os.path
import os
filepath="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)
【问题讨论】:
标签: python machine-learning keras lstm recurrent-neural-network