def discount_episode_rewards(rewards=[], gamma=0.99):
 discounted_r = np.zeros_like(rewards, dtype=np.float32)
 running_add = 0
 for t in reversed(xrange(0, rewards.size)):
  if (rewards[t] != 0):
   running_add = 0
  running_add = ((running_add * gamma) + rewards[t])
  discounted_r[t] = running_add
 return discounted_r

def discount_episode_rewards ( rewards = [ ] , gamma = 0.99 ) : discounted_r = np . zeros_like ( rewards , dtype = np . float32 ) running_add = 0 for t in reversed ( xrange ( 0 , rewards . size ) ) : if ( rewards [ t ] != 0 ) : running_add = 0 running_add = ( ( running_add * gamma ) + rewards [ t ] ) discounted_r [ t ] = running_add return discounted_r
take 1d float array of rewards and compute discounted rewards for an episode .