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: Performs the gradient ascent step in the policy gradient algorithm.
  
  Args:
      test: If true, it uses the test dataloader and policy.
docstr_tok: ["Performs","the","gradient","ascent","step","in","the","policy","gradient","algorithm",".","Args",":","test",":","If","true",",","it","uses","the","test","dataloader","and","policy","."]
code: def _gradient_ascent(self, test=False):
      """Performs the gradient ascent step in the policy gradient algorithm.
  
      Args:
          test: If true, it uses the test dataloader and policy.
      """
      # get batch data from dataloader
      obs, last_actions, price_variations, trf_mu = (
          next(iter(self.test_dataloader))
          if test
          else next(iter(self.train_dataloader))
      )
      obs = obs.to(self.device)
      last_actions = last_actions.to(self.device)
      price_variations = price_variations.to(self.device)
      trf_mu = trf_mu.unsqueeze(1).to(self.device)
  
      # define policy loss (negative for gradient ascent)
      mu = (
          self.test_policy.mu(obs, last_actions)
          if test
          else self.train_policy.mu(obs, last_actions)
      )
      policy_loss = -torch.mean(
          torch.log(torch.sum(mu * price_variations * trf_mu, dim=1))
      )
  
      # update policy network
      if test:
          self.test_policy.zero_grad()
          policy_loss.backward()
          self.test_optimizer.step()
      else:
          self.train_policy.zero_grad()
          policy_loss.backward()
          self.train_optimizer.step()
  
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 6 for calling the PolicyGradient obj's underlying member method code with example usage: obj._gradient_ascent(test) without return types