mirror of
https://github.com/saymrwulf/stable-baselines3.git
synced 2026-05-22 22:10:16 +00:00
* Add auto formatting with black and isort * Reformat code * Ignore typing errors * Add note about line length * Add minimum version for isort * Add commit-checks * Update docker image * Fixed lost import (during last merge) * Fix opencv dependency
39 lines
1.4 KiB
Python
39 lines
1.4 KiB
Python
from typing import Tuple
|
|
|
|
import numpy as np
|
|
|
|
|
|
class RunningMeanStd(object):
|
|
def __init__(self, epsilon: float = 1e-4, shape: Tuple[int, ...] = ()):
|
|
"""
|
|
Calulates the running mean and std of a data stream
|
|
https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm
|
|
|
|
:param epsilon: (float) helps with arithmetic issues
|
|
:param shape: (tuple) the shape of the data stream's output
|
|
"""
|
|
self.mean = np.zeros(shape, np.float64)
|
|
self.var = np.ones(shape, np.float64)
|
|
self.count = epsilon
|
|
|
|
def update(self, arr: np.ndarray) -> None:
|
|
batch_mean = np.mean(arr, axis=0)
|
|
batch_var = np.var(arr, axis=0)
|
|
batch_count = arr.shape[0]
|
|
self.update_from_moments(batch_mean, batch_var, batch_count)
|
|
|
|
def update_from_moments(self, batch_mean: np.ndarray, batch_var: np.ndarray, batch_count: int) -> None:
|
|
delta = batch_mean - self.mean
|
|
tot_count = self.count + batch_count
|
|
|
|
new_mean = self.mean + delta * batch_count / tot_count
|
|
m_a = self.var * self.count
|
|
m_b = batch_var * batch_count
|
|
m_2 = m_a + m_b + np.square(delta) * self.count * batch_count / (self.count + batch_count)
|
|
new_var = m_2 / (self.count + batch_count)
|
|
|
|
new_count = batch_count + self.count
|
|
|
|
self.mean = new_mean
|
|
self.var = new_var
|
|
self.count = new_count
|