class_docstr: Class implementing policy gradient algorithm to train portfolio
  optimization agents.
  
  Note:
      During testing, the agent is optimized through online learning.
      The parameters of the policy is updated repeatedly after a constant
      period of time. To disable it, set learning rate to 0.
  
  Attributes:
      train_env: Environment used to train the agent
      train_policy: Policy used in training.
      test_env: Environment used to test the agent.
      test_policy: Policy after test online learning.
class_docstr_tok: ["Class","implementing","policy","gradient","algorithm","to","train","portfolio","optimization","agents",".","Note",":","During","testing",",","the","agent","is","optimized","through","online","learning",".","The","parameters","of","the","policy","is","updated","repeatedly","after","a","constant","period","of","time",".","To","disable","it",",","set","learning","rate","to","0",".","Attributes",":","train_env",":","Environment","used","to","train","the","agent","train_policy",":","Policy","used","in","training",".","test_env",":","Environment","used","to","test","the","agent",".","test_policy",":","Policy","after","test","online","learning","."]
docstr: Training sequence.
  
  Args:
      episodes: Number of episodes to simulate.
docstr_tok: ["Training","sequence",".","Args",":","episodes",":","Number","of","episodes","to","simulate","."]
code: def train(self, episodes=100):
      """Training sequence.
  
      Args:
          episodes: Number of episodes to simulate.
      """
      for i in tqdm(range(1, episodes + 1)):
          obs = self.train_env.reset()  # observation
          self.train_pvm.reset()  # reset portfolio vector memory
          done = False
  
          while not done:
              # define last_action and action and update portfolio vector memory
              last_action = self.train_pvm.retrieve()
              obs_batch = np.expand_dims(obs, axis=0)
              last_action_batch = np.expand_dims(last_action, axis=0)
              action = self.train_policy(obs_batch, last_action_batch)
              self.train_pvm.add(action)
  
              # run simulation step
              next_obs, reward, done, info = self.train_env.step(action)
  
              # add experience to replay buffer
              exp = (
                  obs,
                  last_action,
                  info["price_variation"],
                  info["trf_mu"],
              )
              self.train_buffer.append(exp)
  
              # update policy networks
              if len(self.train_buffer) == self.train_batch_size:
                  self._gradient_ascent()
  
              obs = next_obs
  
          # gradient ascent with episode remaining buffer data
          self._gradient_ascent()
  
          # validation step
          if self.validation_env:
              self.test(self.validation_env)
  
file_path: FinRL/finrl/agents/portfolio_optimization/algorithms.py
imports: from __future__ import annotations
  import copy
  import numpy
  import torch
  from torch.optim import AdamW
  from torch.utils.data import DataLoader
  from tqdm import tqdm
  from .architectures import EIIE
  from .utils import PVM
  from .utils import ReplayBuffer
  from .utils import RLDataset
label_desc: Use image node_id 3 for calling the PolicyGradient obj's underlying member method code with example usage: obj.train(episodes) without return types