-
Notifications
You must be signed in to change notification settings - Fork 1
/
ou_noise.py
37 lines (29 loc) · 1.03 KB
/
ou_noise.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import numpy as np
class OUNoise:
def __init__(self, num_actions, mu=0, theta=0.15, sigma=0.2, delta=0.5):
self.num_actions = num_actions
self.mu = mu
self.theta = theta
self.sigma = sigma
self.delta = delta
self.reset()
def generate(self):
#x = self.state
#dx = self.theta * (self.mu - x) + self.sigma * np.random.randn(len(x))
#self.state = x + dx
#return self.state
prev_ou_level = self.ou_level
drift = self.theta * (self.mu - prev_ou_level) * self.delta
randomness = np.random.normal(loc=0, scale=np.sqrt(self.delta)*self.sigma, size=None) # Brownian motion
self.ou_level = prev_ou_level + drift + randomness
return self.ou_level
def reset(self):
self.ou_level = np.ones(self.num_actions) * self.mu
if __name__ == "__main__":
noise = OUNoise(3)
outputs = []
for _ in range(1000):
outputs.append(noise.generate())
import matplotlib.pyplot as plt
plt.plot(outputs)
plt.show()