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: Tests the policy with online learning.
  
  Args:
    env: Environment to be used in testing.
    policy: Policy architecture to be used. If None, it will use the training
      architecture.
    online_training_period: Period in which an online training will occur. To
      disable online learning, use a very big value.
    batch_size: Batch size to train neural network. If None, it will use the
      training batch size.
    lr: Policy neural network learning rate. If None, it will use the training
      learning rate
    optimizer: Optimizer of neural network. If None, it will use the training
      optimizer
  
  Note:
      To disable online learning, set learning rate to 0 or a very big online
      training period.
docstr_tok: ["Tests","the","policy","with","online","learning",".","Args",":","env",":","Environment","to","be","used","in","testing",".","policy",":","Policy","architecture","to","be","used",".","If","None",",","it","will","use","the","training","architecture",".","online_training_period",":","Period","in","which","an","online","training","will","occur",".","To","disable","online","learning",",","use","a","very","big","value",".","batch_size",":","Batch","size","to","train","neural","network",".","If","None",",","it","will","use","the","training","batch","size",".","lr",":","Policy","neural","network","learning","rate",".","If","None",",","it","will","use","the","training","learning","rate","optimizer",":","Optimizer","of","neural","network",".","If","None",",","it","will","use","the","training","optimizer","Note",":","To","disable","online","learning",",","set","learning","rate","to","0","or","a","very","big","online","training","period","."]
code: def test(
      self,
      env,
      policy=None,
      online_training_period=10,
      lr=None,
      optimizer=None,
  ):
      """Tests the policy with online learning.
  
      Args:
        env: Environment to be used in testing.
        policy: Policy architecture to be used. If None, it will use the training
          architecture.
        online_training_period: Period in which an online training will occur. To
          disable online learning, use a very big value.
        batch_size: Batch size to train neural network. If None, it will use the
          training batch size.
        lr: Policy neural network learning rate. If None, it will use the training
          learning rate
        optimizer: Optimizer of neural network. If None, it will use the training
          optimizer
  
      Note:
          To disable online learning, set learning rate to 0 or a very big online
          training period.
      """
      self._setup_test(
          env, policy, online_training_period, lr, optimizer
      )
  
      obs = self.test_env.reset()  # observation
      self.test_pvm.reset()  # reset portfolio vector memory
      done = False
      steps = 0
  
      while not done:
          steps += 1
          # define last_action and action and update portfolio vector memory
          last_action = self.test_pvm.retrieve()
          obs_batch = np.expand_dims(obs, axis=0)
          last_action_batch = np.expand_dims(last_action, axis=0)
          action = self.test_policy(obs_batch, last_action_batch)
          self.test_pvm.add(action)
  
          # run simulation step
          next_obs, reward, done, info = self.test_env.step(action)
  
          # add experience to replay buffer
          exp = (
              obs,
              last_action,
              info["price_variation"],
              info["trf_mu"],
          )
          self.test_buffer.append(exp)
  
          # update policy networks
          if steps % online_training_period == 0:
              self._gradient_ascent(test=True)
  
          obs = next_obs
  
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 5 for calling the PolicyGradient obj's underlying member method code with example usage: obj.test(env, policy, online_training_period, lr, optimizer) without return types