How to TRAIN further a previously trained agent?
Show older comments
Hi,
My agent was programmed to stop after reaching an average reward of X. How do I load and extend the training further?
I did enable saving of the experiences and it has created the agent file
Rajesh
Accepted Answer
More Answers (3)
Anh Tran
on 21 Feb 2020
I will answer again, hopefully clear your confusion.
% Train the agent
trainingStats = train(agent, env, trainOpts);
After this line, even though the 'agent' is not returned as an output, its learnable parameters are updated. Learnable parameters, e.g. the weights and biases of the actor/critic neural networks, determines the logic behind the agent (and how it chooses action given an observation).
Now if you execute sim() or train() after this line, the 'agent' will simulate or continue training with the latest parameters.
Rajesh's workflow is very close to resume training (reuse the experiences gathered in the past, start from latest parameters). I revised the code with additional comments. Currently the noise model resets when you train again. You can consider setting the noise variance option to a lower value (still need to be > 0 because we want the agent to always explore) than that of your previous train session.
% Set to true, to resume training from a saved agent
resumeTraining = true;
% Set ResetExperienceBufferBeforeTraining to false to keep experience from the previous session
agentOpts.ResetExperienceBufferBeforeTraining = ~(resumeTraining);
if resumeTraining
% Load the agent from the previous session
sprintf('- Resume training of: %s', PRE_TRAINED_MODEL_FILE);
load(PRE_TRAINED_MODEL_FILE,'saved_agent');
agent = saved_agent;
else
% Create a fresh new agent
agent = rlDDPGAgent(actor, critic, agentOpts);
end
% Train the agent
trainingStats = train(agent, env, trainOpts);
2 Comments
Stav Bar-Sheshet
on 4 Jun 2020
Hi, this is an excellent thread!
What I'm curios about is if you continue training doest the state of the optimizer is saved and continues from the same point?
Sayak Mukherjee
on 23 Feb 2021
for restarting the run with saved agent, the saved agent shaould have 'SaveExperienceBufferWithAgent' parameter set to true, right?
Jonas Woeste
on 11 Jun 2022
Got it to work in Matlab 2022a where its a touch different:
Clue is to save the trainOpts variable after training, which then will technically be a training result object. After restoring this, increase the MaxEpisodes for further training...
% Do the agent, env stuff...
% Load pretrained agent
if isfile('trained_agent.mat')
load("trained_agent.mat","trainOpts")
% increase the max epochs to go on training
cur_episodes = trainOpts.TrainingOptions.MaxEpisodes;
trainOpts.TrainingOptions.MaxEpisodes = cur_episodes + num_epochs;
end
% Train
trainOpts = train(agent,env,trainOpts);
% Save
save("trained_agent.mat","trainOpts")
Please someone update the documentation about this. There its still suggesting to save the agents object...
Sourav Bairagya
on 10 Dec 2019
0 votes
In this case, you can resume your training with the previous experience buffer as a starting point.
You have to set the 'SaveExperienceBufferWithAgent' agent option to 'true'.
For some agents, such as those with large experience buffers and image-based observations, the memory required for saving their experience buffer is large. In these cases, you must ensure that there is enough memory available for the saved agents.
For more informations you can leverage this link:
5 Comments
Rajesh Siraskar
on 10 Dec 2019
mr robot
on 30 Jan 2020
How large is "large" for an experience buffer of 1e6?
Pei Seng Tan
on 24 Apr 2022
Is the option "SaveExperienceBufferWithAgent" still available for MATLAB 2022a? As no longer found it in the documentation. Will the experianced buffer with agent be saved or not to be saved since this option is removed in later documentation?
Jonas Woeste
on 10 Jun 2022
Its not being saved, as the saved file is of size ~25kB regardless of trained epochs. A hint for a working practice for saving and continuing on trained agents would be nice.
轩
on 14 Jun 2024
It seems that the option is under the structure agent.AgentOptions.InfoToSave
Categories
Find more on Training and Simulation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!