-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtransforms.py
101 lines (75 loc) · 1.89 KB
/
transforms.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import torch
from running_stat import RunningStat
class Transform:
'''
Composes several transformation and applies them sequentially
Attributes
----------
filters : list
a list of callables
Methods
-------
__call__(x)
sequentially apply the callables in filters to the input and return the
result
'''
def __init__(self, *filters):
'''
Parameters
----------
filters : variatic argument list
the sequence of transforms to be applied to the input of
__call__
'''
self.filters = list(filters)
def __call__(self, x):
for f in self.filters:
x = f(x)
return x
class ZFilter:
'''
A z-scoring filter
Attributes
----------
running_stat : RunningStat
an object that keeps track of an estimate of the mean and standard
deviation of the observations seen so far
Methods
-------
__call__(x)
Update running_stat with x and return the result of z-scoring x
'''
def __init__(self):
self.running_stat = RunningStat()
def __call__(self, x):
self.running_stat.update(x)
x = (x - self.running_stat.mean) / (self.running_stat.std + 1e-8)
return x
class Bound:
'''
Implements a bounding function
Attributes
----------
low : int
the lower bound
high : int
the upper bound
Methods
-------
__call__(x)
applies the specified bounds to x and returns the result
'''
def __init__(self, low, high):
'''
Parameters
----------
low : int
the lower bound
high : int
the upper bound
'''
self.low = low
self.high = high
def __call__(self, x):
x = torch.clamp(x, self.low, self.high)
return x