I'm working on a machine learning university project and I need to save an "agent" (an object) containing some complex stuff that allows me to do other stuff ahahah...I'm using pickle but unfortunately there is an error....AttributeError: Can't pickle local object 'constant_fn.<locals>.func'
this is a piece of my code:
from finrl.agents.stablebaselines3.models import DRLAgent
import pickle
import os
if os.path.isfile("./filename_pi.obj"):
print("-FILE FOUND-")
file_pi = open('filename_pi.obj', 'rb')
trained_a2c = pickle.load(file_pi)
file_pi.close()
else:
print("-FILE NOT FOUND-")
#A2C
print("Training A2C model")
agent = DRLAgent(env=env_train)
model_a2c = agent.get_model("a2c")
trained_a2c = agent.train_model(model=model_a2c, tb_log_name="a2c", total_timesteps=50000)
file_pi = open('filename_pi.obj', 'wb')
pickle.dump(trained_a2c, file_pi)
file_pi.close()
Reading similar problems I understood that the problem is in something that is not global, but the problem is that I can not modify anything that is inside .get_model and .train_model because they are methods of a library not written by me and that I can not touch. Is there anything I can do? Maybe I don't have to pass "trained_a2c" ? or you recommend me to change the road?
Check this:
And this for better design: