我使用了一个自定义的LSTM模型,内部结构有一些修改,但整体是没有问题的,可以进行训练和预测,但是保存模型后调用一直报错。tensorflow版本是2.13.0,keras版本是2.13.1,这是模型训练和保存的过程:
inputs = tf.keras.layers.Input(shape=(len_input, train_x.shape[-1]))
outputs = LSTM(num_hiddens, batch_size)(inputs)
model = tf.keras.Model(inputs, outputs)
model.summary()
model.compile(optimizer=tf.keras.optimizers.Adam(), loss=['mse'])
history_lstm = model.fit(
train_x,
np.squeeze(train_y),
batch_size=batch_size,
epochs=epochs,
validation_split=validation_split,
callbacks=[checkpoint, lr]
)
# --------------------------------------------------------------------------
# 保存LSTM模型
# --------------------------------------------------------------------------
model.save(rf'{path_output}model\model_LSTM_{site_name}.h5', save_format='h5')
下面是调用模型的过程:
loaded_model = tf.keras.models.load_model('./outputSY/model/model_LSTM_57249.h5', compile=False)
报错:
Traceback (most recent call last):
File "F:\precipitation_XAI\_code\CLSTM\测试make_input_data.py", line 7, in <module>
loaded_model = tf.keras.models.load_model('./outputSY/model/model_LSTM_57249.h5', compile=False)
File "D:\miniconda3\envs\gee\lib\site-packages\keras\src\saving\saving_api.py", line 238, in load_model
return legacy_sm_saving_lib.load_model(
File "D:\miniconda3\envs\gee\lib\site-packages\keras\src\utils\traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "D:\miniconda3\envs\gee\lib\site-packages\keras\src\layers\rnn\lstm.py", line 891, in from_config
return cls(**config)
TypeError: __init__() missing 1 required positional argument: 'units'
使用keras调用也报同样的错误,试了很多方法都不行,求大神!!!
inputs = tf.keras.layers.Input(shape=(len_input, train_x.shape[-1]))
outputs = LSTM(num_hiddens, batch_size)(inputs)
model = tf.keras.Model(inputs, outputs)
model.summary()
model.compile(optimizer=tf.keras.optimizers.Adam(), loss=['mse'])
history_lstm = model.fit(
train_x,
np.squeeze(train_y),
batch_size=batch_size,
epochs=epochs,
validation_split=validation_split,
callbacks=[checkpoint, lr]
)
# --------------------------------------------------------------------------
# 保存LSTM模型
# --------------------------------------------------------------------------
model.save(rf'{path_output}model\model_LSTM_{site_name}.h5', save_format='h5')
下面是调用模型的过程:
loaded_model = tf.keras.models.load_model('./outputSY/model/model_LSTM_57249.h5', compile=False)
报错:
Traceback (most recent call last):
File "F:\precipitation_XAI\_code\CLSTM\测试make_input_data.py", line 7, in <module>
loaded_model = tf.keras.models.load_model('./outputSY/model/model_LSTM_57249.h5', compile=False)
File "D:\miniconda3\envs\gee\lib\site-packages\keras\src\saving\saving_api.py", line 238, in load_model
return legacy_sm_saving_lib.load_model(
File "D:\miniconda3\envs\gee\lib\site-packages\keras\src\utils\traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "D:\miniconda3\envs\gee\lib\site-packages\keras\src\layers\rnn\lstm.py", line 891, in from_config
return cls(**config)
TypeError: __init__() missing 1 required positional argument: 'units'
使用keras调用也报同样的错误,试了很多方法都不行,求大神!!!