在过去的两个星期中,我一直遇到这个问题,但没有找到解决方案。因此,我正在训练一个用于文本生成的keras顺序模型,它可以正常工作,没问题,但是在完成训练并保存该模型之后,当我尝试加载它并对其进行进一步训练时,由于层。
代码如下:
def save_trained_model(trained_model, file_name):
with open(f'model_gen/{file_name}.json', 'w') as json:
try:
json.write(trained_model.to_json())
except:
print("Model could not be saved in a .json format.")
try:
trained_model.save_weights(f'model_gen/{file_name}.h5')
except:
print("Models weights could not be saved in a .h5 format.")
save_trained_model方法:
def load_saved_model(FILE_NAME):
with open(f'model_gen/{FILE_NAME}.json') as json:
try:
model = model_from_json(json.read())
print("Loaded json file for the model.")
try:
model.load_weights(f'model_gen/{FILE_NAME}.h5')
print("Loaded weights for the model.")
except:
print("Models weights could not be loaded from the .h5 format.")
except:
print("Model could not be loaded from the .json format.")
return model
和load_saved_model:
Traceback (most recent call last):
File ".../src/tweet_generator.py", line 201, in <module>
train_gen()
File ".../src/tweet_generator.py", line 70, in train_gen
model.fit(X, Y, batch_size=80, epochs=30)
File "...\Anaconda\envs\gputest\lib\site-packages\keras\engine\training.py", line 1154, in fit
batch_size=batch_size)
File "...\Anaconda\envs\gputest\lib\site-packages\keras\engine\training.py", line 621, in _standardize_user_data
exception_prefix='target')
File "...\Anaconda\envs\gputest\lib\site-packages\keras\engine\training_utils.py", line 145, in standardize_input_data
str(data_shape))
ValueError: Error when checking target: expected dense_2 to have shape (6620,) but got array with shape (6609,)
尝试再次训练后的输出是:
{{1}}
6609是第一次训练之前的vocab_size,并且看起来在经过一次训练后有所增加。我可以手动将其更改为6620,但它可以正常工作,但不正确,这意味着它似乎不恢复运行。例如,第一次训练的最后一次损失是2.7,在手动更改vocab_size并再次对加载的模型进行训练后,损失从25或类似的损失开始。所以它不是恢复。
谁知道我该如何解决?
谢谢!