Problem: If I save and load model (ANN model) in the same session, the result from the loaded model is different to the model already in the session (prior to saving)
Ideally shouldn’t a trained model (in session/memory) and loaded model (post save and load) be identical?
I searched but have not found any leads yet. Any help/leads would be appreciated. Thanks in advance
Error Message: There is no error message But in my case, the results from the session model (ANN) are very bad (unreasonable with very high MAE) and the results from the loaded model (saved session model) are satisfactory (Fairly good MAE). Am not able to understand, why such a huge difference?
Code as below
#This give bad results
xyz_forecast = xyzDemandForecast(variable_collection)
modelframe_dict, history_dict = xyz_forecast.fit(
['train_1','train_2','train_3'],
['validation'],
train_test_config=pipeline_config,
)
result_df = xyz_forecast.predict(
[test_df], "xyz_DEMAND_POT", predict_config=pipeline_config
)
#This gives good result
xyz_forecast = xyzDemandForecast(variable_collection)
modelframe_dict, history_dict = xyz_forecast.fit(
['train_1','train_2','train_3'],
['validation'],
train_test_config=pipeline_config,
)
save_model(f"_forecaster_model_{EXPERIMENT_NAME}", xyz_forecast)
xyz_forecast = load_model(f"_forecaster_model_{EXPERIMENT_NAME}")
result_df = xyz_forecast.predict(
[test_df], "xyz_DEMAND_POT", predict_config=pipeline_config
)
#Save Code
def save_model(dir_name: str, forecaster_object: xyzDemandForecast):
if not os.path.isdir(dir_name):
os.mkdir(dir_name)
temp_model_dict = dict()
for model_name, model in forecaster_object.modelframe_dict.items():
file_name = _get_model_filename(model_name)
model.model_object.save(dir_name + f"/{file_name}") # model name
temp_model_dict[model_name] = tf.keras.models.clone_model(model.model_object)
model.model_object = None
pickle.dump(forecaster_object, open(dir_name + "/forecaster_object.pkl", "wb"))
for model_name, model in forecaster_object.modelframe_dict.items():
model.model_object = temp_model_dict[model_name] # model name
#Load Code
def load_model(dir_name: str):
forecaster_object = pickle.load(
open(dir_name + "/forecaster_object.pkl", "rb")
)
for model_name, model in forecaster_object.modelframe_dict.items():
file_name = _get_model_filename(model_name)
model.model_object = tf.keras.models.load_model(
dir_name + f"/{file_name}"
)
return forecaster_object