(seg, gamma, lam)
return
Compute target value using TD(lambda) estimator, and advantage with GAE(lambda)

  :param seg: (dict) the current segment of the trajectory (see traj_segment_generator return for more information)
  :param gamma: (float) Discount factor
  :param lam: (float) GAE factor
Compute target value using TD(lambda) estimator, and advantage with GAE(lambda)
Compute target value using TD ( lambda ) estimator and advantage with GAE ( lambda )
def add_vtarg_and_adv(seg, gamma, lam):
  """
  Compute target value using TD(lambda) estimator, and advantage with GAE(lambda)

  :param seg: (dict) the current segment of the trajectory (see traj_segment_generator return for more information)
  :param gamma: (float) Discount factor
  :param lam: (float) GAE factor
  """
  # last element is only used for last vtarg, but we already zeroed it if last new = 1
  episode_starts = np.append(seg["episode_starts"], False)
  vpred = seg["vpred"]
  nexvpreds = seg["nextvpreds"]
  rew_len = len(seg["rewards"])
  seg["adv"] = np.empty(rew_len, 'float32')
  rewards = seg["rewards"]
  lastgaelam = 0
  for step in reversed(range(rew_len)):
    nonterminal = 1 - float(episode_starts[step + 1])
    delta = rewards[step] + gamma * nexvpreds[step] - vpred[step]
    seg["adv"][step] = lastgaelam = delta + gamma * lam * nonterminal * lastgaelam
  seg["tdlamret"] = seg["adv"] + seg["vpred"]

  return
def add_vtarg_and_adv ( seg , gamma , lam ) : # last element is only used for last vtarg, but we already zeroed it if last new = 1 episode_starts = np . append ( seg [ "episode_starts" ] , False ) vpred = seg [ "vpred" ] nexvpreds = seg [ "nextvpreds" ] rew_len = len ( seg [ "rewards" ] ) seg [ "adv" ] = np . empty ( rew_len , 'float32' ) rewards = seg [ "rewards" ] lastgaelam = 0 for step in reversed ( range ( rew_len ) ) : nonterminal = 1 - float ( episode_starts [ step + 1 ] ) delta = rewards [ step ] + gamma * nexvpreds [ step ] - vpred [ step ] seg [ "adv" ] [ step ] = lastgaelam = delta + gamma * lam * nonterminal * lastgaelam seg [ "tdlamret" ] = seg [ "adv" ] + seg [ "vpred" ] return