Answers.Body: <p>The environment you described fits a multi-armed bandit problem where the action does not influence the next state encountered. Algorithms such as A2C are designed for situations where the state transition is dependent on the action. If this is not the case, you should look for multi-armed bandit type algorithms that are more suited for the job.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-06T02:21:49.977Z
Answers.LastActivityDate: 2023-08-06T02:21:49.977Z
Answers.UserDisplayName: languageoftheuniverse
Body: <p>I'm quite new to RL and have been trying to train an A2C model from stable_baselines3 to derive an integer sequence based on 3 other input sequences of floats. I have a custom gym environment that computes the agent rewards based on the actions taken on the <code>step</code> method. Spaces are:</p>
  <pre><code>self.observation_space = spaces.Dict({
      &quot;s1&quot;: spaces.Box(
          low=0,
          high=float(&quot;inf&quot;),
          shape=(48,),
          dtype=np.float64
      ),
      &quot;s2&quot;: spaces.Box(
          low=0,
          high=float(&quot;inf&quot;),
          shape=(48,),
          dtype=np.float64
      ),
      &quot;s3&quot;: spaces.Box(
          low=0,
          high=float(&quot;inf&quot;),
          shape=(48,),
          dtype=np.float64
      ),
  })
  self.action_space = spaces.MultiDiscrete([5]*48)
  </code></pre>
  <p>The observables do not change based on the <code>action</code>, so there is not state carried over between <code>step</code> calls; for each <code>obs</code> a given <code>action</code> will maximise the reward. Then the environment would transition to the next state with a completely new <code>obs</code> that will have an unrelated new optimal <code>action</code>.</p>
  <p>For training I run:</p>
  <pre><code>env_train = Monitor(gym.make(&quot;gym/MyCustomEnvironment-v0&quot;, data=data_train), log_dir)
  model = A2C(&quot;MultiInputPolicy&quot;, env_train, verbose=0, device=&quot;cpu&quot;)    
  model.learn(total_timesteps=timesteps)
  </code></pre>
  <p>The rewards/timesteps plot converges towards a value in a logarithmic curve. Even though when I evaluate the trained model on a new observation I always get the same set of actions.</p>
  <pre><code>env = Monitor(gym.make(&quot;gym/MyCustomEnvironment-v0&quot;, data=data_test), log_dir)
  obs, _ = env.reset()
  action, _ = model.predict(obs, deterministic=True)
  </code></pre>
  <p>Here the value of <code>action</code> will be the same or very similar regardless of the data used to create the environment (which is then used to derive <code>obs</code>). This is very surprising because the optimal policy that would maximise training rewards would be to choose different output sequences depending on the input sequence.</p>
  <p>I have tried multiple things, including training with large datasets, changing learning rates, gamma and network topology, playing with episode lengths, etc. I have even changed the reward function to one that produces arbitrary numbers and even in that case the &quot;trained&quot; model will always output the same actions regardless of <code>obs</code>.</p>
  <p>Am I just misusing A2C for a use-case it's not intended for?</p>
  
Tags: <reinforcement-learning><python><actor-critic-methods><gym><stable-baselines>
Title: Getting always the same action on an A2C from stable_baselines3
UserDisplayName: Jesuspc
ThreadText: # Getting always the same action on an A2C from stable_baselines3
  
  (Asked by: username_0 on 2023-08-01)
  
  I'm quite new to RL and have been trying to train an A2C model from stable_baselines3 to derive an integer sequence based on 3 other input sequences of floats. I have a custom gym environment that computes the agent rewards based on the actions taken on the step method. Spaces are:
  self.observation_space = spaces.Dict({
      "s1": spaces.Box(
          low=0,
          high=float("inf"),
          shape=(48,),
          dtype=np.float64
      ),
      "s2": spaces.Box(
          low=0,
          high=float("inf"),
          shape=(48,),
          dtype=np.float64
      ),
      "s3": spaces.Box(
          low=0,
          high=float("inf"),
          shape=(48,),
          dtype=np.float64
      ),
  })
  self.action_space = spaces.MultiDiscrete([5]*48)
  
  The observables do not change based on the action, so there is not state carried over between step calls; for each obs a given action will maximise the reward. Then the environment would transition to the next state with a completely new obs that will have an unrelated new optimal action.
  For training I run:
  env_train = Monitor(gym.make("gym/MyCustomEnvironment-v0", data=data_train), log_dir)
  model = A2C("MultiInputPolicy", env_train, verbose=0, device="cpu")    
  model.learn(total_timesteps=timesteps)
  
  The rewards/timesteps plot converges towards a value in a logarithmic curve. Even though when I evaluate the trained model on a new observation I always get the same set of actions.
  env = Monitor(gym.make("gym/MyCustomEnvironment-v0", data=data_test), log_dir)
  obs, _ = env.reset()
  action, _ = model.predict(obs, deterministic=True)
  
  Here the value of action will be the same or very similar regardless of the data used to create the environment (which is then used to derive obs). This is very surprising because the optimal policy that would maximise training rewards would be to choose different output sequences depending on the input sequence.
  I have tried multiple things, including training with large datasets, changing learning rates, gamma and network topology, playing with episode lengths, etc. I have even changed the reward function to one that produces arbitrary numbers and even in that case the "trained" model will always output the same actions regardless of obs.
  Am I just misusing A2C for a use-case it's not intended for?
  
  **Tags:** reinforcement-learning, python, actor-critic-methods, gym, stable-baselines
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-08-06. Score: 1
  
  The environment you described fits a multi-armed bandit problem where the action does not influence the next state encountered. Algorithms such as A2C are designed for situations where the state transition is dependent on the action. If this is not the case, you should look for multi-armed bandit type algorithms that are more suited for the job.
  
  
  ---
  
  