Source code for machina.noise.ounoise

"""
This is implementation of Ornstein-Uhlenbeck process.
"""

import numpy as np
import torch

from machina.utils import get_device
from machina.noise.base import BaseActionNoise


[docs]class OUActionNoise(BaseActionNoise): """ noise produced by Ornstein-Uhlenbeck process. """ def __init__(self, ac_space, sigma=0.2, theta=.15, dt=1e-2, x0=None): BaseActionNoise.__init__(self, ac_space) self.mu = np.zeros(self.ac_space[0]) self.theta = theta self.sigma = sigma * np.ones_like(self.mu) self.dt = dt self.x0 = x0 self.reset() def __call__(self, device='cpu'): x = self.x_prev + self.theta * (self.mu - self.x_prev) * self.dt + \ self.sigma * np.sqrt(self.dt) * \ np.random.normal(size=self.mu.shape) self.x_prev = x return torch.tensor(x, dtype=torch.float, device=device)
[docs] def reset(self): if self.x0 is not None: self.x_prev = self.x0 else: self.x_prev = np.zeros_like(self.mu, dtype=np.float32)