Created
April 2, 2020 15:25
-
-
Save Chion82/f8ef2e9d9b8aa94fbf306ff8e4748481 to your computer and use it in GitHub Desktop.
Implementations of discount function used to calculate discounted rewards in Reinforcement Learning
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from scipy.signal import lfilter | |
def discount_readable(r, gamma): | |
""" Compute the gamma-discounted rewards over an episode | |
""" | |
discounted_r, cumul_r = np.zeros_like(r), 0 | |
for t in reversed(range(0, len(r))): | |
cumul_r = r[t] + cumul_r * gamma | |
discounted_r[t] = cumul_r | |
return discounted_r | |
discount_wtf = lambda x, gamma: lfilter([1],[1,-gamma],x[::-1])[::-1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment