gpt4 book ai didi

python - 保存并继续训练 LSTM 网络

转载 作者:行者123 更新时间:2023-11-30 09:45:13 26 4
gpt4 key购买 nike

我尝试让 LSTM 模型在上次运行结束后继续运行。一切编译都很好,直到我尝试适应网络。然后就报错了:

ValueError: Error when checking target: expected dense_29 to have 3 dimensions, but got array with shape (672, 1)

我检查了各种文章,例如 thisthis但我看不出我的代码有什么问题。

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)

最佳答案

尝试 model.summary(),您会看到网络中最后一层(即 Dense 层)的输出形状为 (None, 10, 1)。因此,您提供给模型的标签(即 y_train)也必须具有 (num_samples, 10, 1) 的形状。

如果输出形状 (None, 10, 1) 不是您想要的(例如,您希望 (None, 1) 作为模型的输出形状)那么你需要修改你的模型定义。实现这一目标的一个简单修改是从 LSTM 层中删除 return_sequences=True 参数。

关于python - 保存并继续训练 LSTM 网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53413505/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com