Improve normalizer for scalar

This commit is contained in:
Shangtong Zhang
2017-10-07 21:23:02 -06:00
parent fae9a85f31
commit 51f125c01e
3 changed files with 13 additions and 4 deletions
+11 -2
View File
@@ -4,6 +4,7 @@
# declaration at the top #
#######################################################################
import torch
import numpy as np
class Normalizer:
def __init__(self, o_size):
@@ -22,13 +23,21 @@ class StaticNormalizer:
self.online_stats = SharedStats(o_size)
def __call__(self, o_):
o = torch.FloatTensor(o_)
if np.isscalar(o_):
o = torch.FloatTensor([o_])
else:
o = torch.FloatTensor(o_)
self.online_stats.feed(o)
if self.offline_stats.n[0] == 0:
return o_
std = (self.offline_stats.v + 1e-6) ** .5
o = (o - self.offline_stats.m) / std
return o.numpy().reshape(o_.shape)
o = o.numpy()
if np.isscalar(o_):
o = np.asscalar(o)
else:
o = o.reshape(o_.shape)
return o
class SharedStats:
def __init__(self, o_size):