code: def finish_episode(policy, optimizer, gamma):
      """
      Training code. Calculates actor and critic loss and performs backprop.
      """
      R = 0
      saved_actions = policy.saved_actions
      policy_losses = []  # list to save actor (policy) loss
      value_losses = []  # list to save critic (value) loss
      returns = deque()  # list to save the true values
  
      # calculate the true value using rewards returned from the environment
      for r in policy.rewards[::-1]:
          # calculate the discounted value
          R = r + gamma * R
          returns.appendleft(R)
  
      returns = torch.tensor(returns)
      returns = (returns - returns.mean()) / (returns.std() + eps)
  
      for (log_prob, value), R in zip(saved_actions, returns):
          advantage = R - value.item()
  
          # calculate actor (policy) loss
          policy_losses.append(-log_prob * advantage)
  
          # calculate critic (value) loss using L1 smooth loss
          value_losses.append(
              F.smooth_l1_loss(value, torch.tensor([R]))
          )
  
      # reset gradients
      optimizer.zero_grad()
  
      # sum up all the values of policy_losses and value_losses
      loss = (
          torch.stack(policy_losses).sum()
          + torch.stack(value_losses).sum()
      )
  
      # perform backprop
      loss.backward()
      optimizer.step()
      # reset rewards and action buffer
      del policy.rewards[:]
      del policy.saved_actions[:]
  
file_path: ignite/examples/reinforcement_learning/actor_critic.py
imports: import argparse
  from collections import deque, namedtuple
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  import torch.optim
  from torch.distributions import Categorical
  from ignite.engine import Engine, Events
label_desc: Use image node_id 2 for calling a global function with example usage: finish_episode(policy, optimizer, gamma) without return types