-
Notifications
You must be signed in to change notification settings - Fork 0
/
OUNoise.py
48 lines (38 loc) · 1.49 KB
/
OUNoise.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
37
38
39
40
41
42
43
44
45
46
47
48
import numpy as np
import torch
# from https://github.com/songrotek/DDPG/blob/master/ou_noise.py
class OUNoise:
def __init__(self, action_dimension, scale=0.1, mu=0, theta=0.15, sigma=0.2):
self.action_dimension = action_dimension
self.scale = scale
self.mu = mu
self.theta = theta
self.sigma = sigma
self.state = np.ones(self.action_dimension) * self.mu
self.reset()
def reset(self):
self.state = np.ones(self.action_dimension) * self.mu
def noise(self):
x = self.state
dx = self.theta * (self.mu - x) + self.sigma * np.random.standard_normal(len(x))
self.state = x + dx
return torch.tensor(self.state * self.scale).float()
# class OUNoise:
# """Ornstein-Uhlenbeck process."""
# def __init__(self, size, seed, mu=0., theta=0.15, sigma=0.2):
# """Initialize parameters and noise process."""
# self.mu = mu * np.ones(size)
# self.size = size
# self.theta = theta
# self.sigma = sigma
# self.seed = random.seed(seed)
# self.reset()
# def reset(self):
# """Reset the internal state (= noise) to mean (mu)."""
# self.state = copy.copy(self.mu)
# def sample(self):
# """Update internal state and return it as a noise sample."""
# x = self.state
# dx = self.theta * (self.mu - x) + self.sigma * np.random.standard_normal(self.size)
# self.state = x + dx
# return self.state