Answers.Body: <p>You can mitigate <a href="https://en.wikipedia.org/wiki/Catastrophic_interference" rel="nofollow noreferrer">catastrophic forgetting</a> by storing the trajectories generated by the actors during training in a replay buffer. Then, you sample trajectories from that replay buffer. This way, each mini-batch of experience will contain data from multiple days.</p>
  <p>There are many strategies to do this sampling, but you can start with uniform sampling. From what you're describing, it doesn't seem that storage is going to be an issue (288 data points per day is small), so you can keep all trajectories. If you can't afford to store all trajectories, then you should also design a strategy to remove them from the replay buffer.</p>
  <p>You can refer to this <a href="https://www.tensorflow.org/agents/tutorials/5_replay_buffers_tutorial" rel="nofollow noreferrer">handy guide</a> describing how to implement a replay buffer in TensorFlow.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-23T07:29:35.093Z
Answers.Text: Thanks Raphael for your answer. I have to admit that I have problems understanding it. You mentioned the catastrophic forgetting. Actually, I don't want to forget the information from the previous days. And what do you mean by sampling trajectories from a replay buffer? And just for the record: I am not using TensorFlow for reinforcement learning so the posted link is not suitable for me (altough I appreciate that you have posted it).
Answers.UserDisplayName: PeterBe
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-30T08:26:31.913Z
Answers.Text: Thanks Rapahel for your answer. Any comments to my last comment? I'll highly appreciate every further comment from you.
Answers.UserDisplayName: PeterBe
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-07T09:06:20.417Z
Answers.Text: Any further comments? I have problems understanding your answer. Would you mind elaborating a little bit more on it (see my first comment).
Answers.UserDisplayName: PeterBe
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-09T18:14:22.130Z
Answers.Text: Apologies I was on holidays :) The method I'm describing is supposed to prevent catastrophic forgetting. The idea is to store the 288 decisions the agent has made on day 1 (with associated state and reward). That's a trajectory. Then the next day, you train on a mixture of the decisions made on this day and those of day 1. On day 3 you train on a mixture of decisions made on this day and those of day 1 and 2, and so on. Is that clearer?
Answers.UserDisplayName: Raphael Lopez Kaufman
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-10T11:51:30.300Z
Answers.Text: Thanks for your comment Raphael. Actually I still don't understand it. Shall I train an agent and then test it on day 1 and store the values for each timeslot? But how shall this work. After training the model, I can just apply it on other days. How can I tell the model to futher learn another way and still somehow consider the values from the training of day 1?
Answers.UserDisplayName: PeterBe
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-13T17:41:14.777Z
Answers.Text: I don't know what ML framework you use, but it's fairly standard in most to continue training the same model architecture by restoring the weights from a previous training run as the starting point for a new training run. So, on day two, reload the weights from the training done on day one, and train the model on a mixture of day one and day two data. On day three, reload the weights from the training done on day two and train the model on a mixture of day one, two and three data, and so on. Does it make sense?
Answers.UserDisplayName: Raphael Lopez Kaufman
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-14T08:38:06.340Z
Answers.Text: Thanks for your answer and effort. I really appreciate it. I use the Framework Stable-Baselines 3 (https://stable-baselines3.readthedocs.io/en/master/) and currently the algorithm A2C. So I have to find a way to continue training the model after having loaded weights from a previous run. What I don't understand in your comment is how to "train the model on a mixture of day one and day two data". My environment is a simulation of different days. One run is for one day. So it does not make sense to "mix" the days as the data would be inconsistant and unrealistic if you create a new day
Answers.UserDisplayName: PeterBe
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-14T18:48:35.300Z
Answers.Text: A2C is not a good fit for experience replay because it's an on policy algorithm (there are off policy variants though). You could switch to something like DQN or offline RL which are better suited to replaying experience.
  In DQN, it's fine to mix data from several days because you learn on transitions $(s_t, a_t, r_{t+1}, s_{t+1})$ rather than whole episodes.
  
  PS: don't forget to upvote my comments if you find them useful!
Answers.UserDisplayName: Raphael Lopez Kaufman
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-15T08:29:48.567Z
Answers.Text: Thanks for your comment. The reason why I don't use DQN is that I have a continious 3-dimensional action space (meaning I have 3 continuous variables). To be totally honest, I still have fundamental problems with your suggested approach. Mixing the days is not reasonable as I also get a cumulative reward at the end of the episode (next to the reward after every action).
Answers.UserDisplayName: PeterBe
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-15T08:31:47.123Z
Answers.Text: Further, I don't understand how your trajectory approach should work. You said I should "store the 288 decisions the agent has made on day 1". Are you talking about the weights of the model or why else should I store the decisions of the agent after training? Your described pipeline is not clear to me. When I train the model on day 1, then I could store the weights of the model. Shall I then run the agent again on day 1 with the stored weights of the model and test the agent on the same day and store which decisions it is making? If so, what shall I do with the stored actions?
Answers.UserDisplayName: PeterBe
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-15T19:23:29.630Z
Answers.Text: The terminal reward is not a problem, it's part of the final transition. You train your agent on batches of transitions, not on whole trajectories. That's what you're mixing: in your batches, you'll have transitions from day 1, day 2, etc. When I say "store the 288 decisions the agent has made on day 1", I mean the 288 transitions $(s_t, a_t, r_{t+1},  s_{t+1})$. At the end of training on these 288 transitions, you should separately store the weights of the model.
Answers.UserDisplayName: Raphael Lopez Kaufman
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-20T15:27:41.437Z
Answers.Text: Thanks Raphael for your comment and effort. I really appreciate it. Unfortunately I have to admit that I don't understand your answer and still  don't know what to do. In my current setting, I train the agent only for one day and afterwards I can test it. Now I don't understand what to do. One training episode always ends after one day. I have to find a way to tell the training algorithm to switch the days during the training after one episode. How can I do that? I think this should be independant from your trajectory and replay buffer approach
Answers.UserDisplayName: PeterBe
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-24T07:50:45.820Z
Answers.Text: Thanks Rapahel for your answer. Any comments to my last comment? I'll highly appreciate every further comment from you.
Answers.UserDisplayName: PeterBe
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-07T18:25:40.913Z
Answers.Text: I'm not sure how I can be of more help unfortunately. Hopefully someone else understands your problem better than I do
Answers.UserDisplayName: Raphael Lopez Kaufman
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-09T15:57:00.720Z
Answers.Text: @Rapahel: Thanks for your comment. Would you mind giving a little bit more information about how to do the things you suggest? How can I generate the trajectories and how can I train on a mixture of day 1 and 2 as you describe? For example you wrote "So, on day two, reload the weights from the training done on day one, and train the model on a mixture of day one and day two data. " --> How can I mix those 2 days. Maybe you can describe how to successively train an RL agent on the same environment with different data on your own or maybe you know a link where this is described
Answers.UserDisplayName: PeterBe
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-18T06:46:47.740Z
Answers.Text: Thanks for your comments. Any comment to my last comment? I'll highly appreciate every further comment from you.
Answers.UserDisplayName: PeterBe
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-22T09:21:33.970Z
Answers.Text: @ Raphael Lopez Kaufman: Thanks Raphael for your comments so far. Would you mind elaborating a little bit more on my previous comment. There I describe what I don't understand about your given answers so far.
Answers.UserDisplayName: PeterBe
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-19T17:51:29.727Z
Answers.LastActivityDate: 2022-05-19T17:51:29.727Z
Answers.UserDisplayName: Raphael Lopez Kaufman
Body: <p>I have a scheduling problem that I am trying to solve with RL (if you are interested in more details you can read about it here <a href="https://ai.stackexchange.com/questions/28888/reinforcement-learning-applicable-to-a-scheduling-problem">Reinforcement learning applicable to a scheduling problem?</a>).</p>
  <p>I have created an own environment (OpenAI-Gym) and I have trained the model for one specific day of the simulation. So I have 288 timesteps for one day (1 for each 5 minutes) and the simulation last until the end of the day. So the agent needs to make 288 decisions when having 1 control variable.</p>
  <p>Now my question is whether it is possible to successively train an RL agent on the same environment for different days? The environment and reward function will stay the same but the input data will change as every day has different input data (temperature, heat demand, electricity price etc.). So I would like to train an agent for one day and then tell the agent to train on another day but not forget everything it has learned during the training of the first day. Thus I can make sure that the agent is not overfitting to one special input data but also has the ability to generalize and thus be applicable for different days.</p>
  <p>Do you know if and how I can do this?</p>
  <p><strong>Reminder</strong>: Can anybody tell me more about this by now. I'll highly appreciate every further comment as I still don't know how to do this.</p>
  
Tags: <reinforcement-learning>
Title: Is it possible to successively train an RL agent on the same environment with different data
UserDisplayName: PeterBe
ThreadText: # Is it possible to successively train an RL agent on the same environment with different data
  
  (Asked by: username_0 on 2022-05-18)
  
  I have a scheduling problem that I am trying to solve with RL (if you are interested in more details you can read about it here [Reinforcement learning applicable to a scheduling problem?](https://ai.stackexchange.com/questions/28888/reinforcement-learning-applicable-to-a-scheduling-problem)).
  
  I have created an own environment (OpenAI-Gym) and I have trained the model for one specific day of the simulation. So I have 288 timesteps for one day (1 for each 5 minutes) and the simulation last until the end of the day. So the agent needs to make 288 decisions when having 1 control variable.
  
  Now my question is whether it is possible to successively train an RL agent on the same environment for different days? The environment and reward function will stay the same but the input data will change as every day has different input data (temperature, heat demand, electricity price etc.). So I would like to train an agent for one day and then tell the agent to train on another day but not forget everything it has learned during the training of the first day. Thus I can make sure that the agent is not overfitting to one special input data but also has the ability to generalize and thus be applicable for different days.
  
  Do you know if and how I can do this?
  
  **Reminder**: Can anybody tell me more about this by now. I'll highly appreciate every further comment as I still don't know how to do this.
  
  **Tags:** reinforcement-learning
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-05-19. Score: 1
  
  You can mitigate [catastrophic forgetting](https://en.wikipedia.org/wiki/Catastrophic_interference) by storing the trajectories generated by the actors during training in a replay buffer. Then, you sample trajectories from that replay buffer. This way, each mini-batch of experience will contain data from multiple days.
  
  There are many strategies to do this sampling, but you can start with uniform sampling. From what you're describing, it doesn't seem that storage is going to be an issue (288 data points per day is small), so you can keep all trajectories. If you can't afford to store all trajectories, then you should also design a strategy to remove them from the replay buffer.
  
  You can refer to this [handy guide](https://www.tensorflow.org/agents/tutorials/5_replay_buffers_tutorial) describing how to implement a replay buffer in TensorFlow.
  
  > **Comments:**
  > **username_0**: Thanks Raphael for your answer. I have to admit that I have problems understanding it. You mentioned the catastrophic forgetting. Actually, I don't want to forget the information from the previous days. And what do you mean by sampling trajectories from a replay buffer? And just for the record: I am not using TensorFlow for reinforcement learning so the posted link is not suitable for me (altough I appreciate that you have posted it).  
  > **username_0**: Thanks Rapahel for your answer. Any comments to my last comment? I'll highly appreciate every further comment from you.  
  > **username_0**: Any further comments? I have problems understanding your answer. Would you mind elaborating a little bit more on it (see my first comment).  
  > **username_1**: Apologies I was on holidays :) The method I'm describing is supposed to prevent catastrophic forgetting. The idea is to store the 288 decisions the agent has made on day 1 (with associated state and reward). That's a trajectory. Then the next day, you train on a mixture of the decisions made on this day and those of day 1. On day 3 you train on a mixture of decisions made on this day and those of day 1 and 2, and so on. Is that clearer?  
  > **username_0**: Thanks for your comment Raphael. Actually I still don't understand it. Shall I train an agent and then test it on day 1 and store the values for each timeslot? But how shall this work. After training the model, I can just apply it on other days. How can I tell the model to futher learn another way and still somehow consider the values from the training of day 1?  
  > **username_1**: I don't know what ML framework you use, but it's fairly standard in most to continue training the same model architecture by restoring the weights from a previous training run as the starting point for a new training run. So, on day two, reload the weights from the training done on day one, and train the model on a mixture of day one and day two data. On day three, reload the weights from the training done on day two and train the model on a mixture of day one, two and three data, and so on. Does it make sense?  
  > **username_0**: Thanks for your answer and effort. I really appreciate it. I use the Framework Stable-Baselines 3 (https://stable-baselines3.readthedocs.io/en/master/) and currently the algorithm A2C. So I have to find a way to continue training the model after having loaded weights from a previous run. What I don't understand in your comment is how to "train the model on a mixture of day one and day two data". My environment is a simulation of different days. One run is for one day. So it does not make sense to "mix" the days as the data would be inconsistant and unrealistic if you create a new day  
  > **username_1**: A2C is not a good fit for experience replay because it's an on policy algorithm (there are off policy variants though). You could switch to something like DQN or offline RL which are better suited to replaying experience. In DQN, it's fine to mix data from several days because you learn on transitions $(s_t, a_t, r_{t+1}, s_{t+1})$ rather than whole episodes.  PS: don't forget to upvote my comments if you find them useful!  
  > **username_0**: Thanks for your comment. The reason why I don't use DQN is that I have a continious 3-dimensional action space (meaning I have 3 continuous variables). To be totally honest, I still have fundamental problems with your suggested approach. Mixing the days is not reasonable as I also get a cumulative reward at the end of the episode (next to the reward after every action).  
  > **username_0**: Further, I don't understand how your trajectory approach should work. You said I should "store the 288 decisions the agent has made on day 1". Are you talking about the weights of the model or why else should I store the decisions of the agent after training? Your described pipeline is not clear to me. When I train the model on day 1, then I could store the weights of the model. Shall I then run the agent again on day 1 with the stored weights of the model and test the agent on the same day and store which decisions it is making? If so, what shall I do with the stored actions?  
  > **username_1**: The terminal reward is not a problem, it's part of the final transition. You train your agent on batches of transitions, not on whole trajectories. That's what you're mixing: in your batches, you'll have transitions from day 1, day 2, etc. When I say "store the 288 decisions the agent has made on day 1", I mean the 288 transitions $(s_t, a_t, r_{t+1},  s_{t+1})$. At the end of training on these 288 transitions, you should separately store the weights of the model.  
  > **username_0**: Thanks Raphael for your comment and effort. I really appreciate it. Unfortunately I have to admit that I don't understand your answer and still  don't know what to do. In my current setting, I train the agent only for one day and afterwards I can test it. Now I don't understand what to do. One training episode always ends after one day. I have to find a way to tell the training algorithm to switch the days during the training after one episode. How can I do that? I think this should be independant from your trajectory and replay buffer approach  
  > **username_0**: Thanks Rapahel for your answer. Any comments to my last comment? I'll highly appreciate every further comment from you.  
  > **username_1**: I'm not sure how I can be of more help unfortunately. Hopefully someone else understands your problem better than I do  
  > **username_0**: @Rapahel: Thanks for your comment. Would you mind giving a little bit more information about how to do the things you suggest? How can I generate the trajectories and how can I train on a mixture of day 1 and 2 as you describe? For example you wrote "So, on day two, reload the weights from the training done on day one, and train the model on a mixture of day one and day two data. " --> How can I mix those 2 days. Maybe you can describe how to successively train an RL agent on the same environment with different data on your own or maybe you know a link where this is described  
  > **username_0**: Thanks for your comments. Any comment to my last comment? I'll highly appreciate every further comment from you.  
  > **username_0**: @ Raphael Lopez Kaufman: Thanks Raphael for your comments so far. Would you mind elaborating a little bit more on my previous comment. There I describe what I don't understand about your given answers so far.  
  
  ---
  
  