-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathutils.py
58 lines (41 loc) · 1.33 KB
/
utils.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
import numpy
from scipy.stats import norm
numpy.seterr(all='ignore')
def pdf(x,mu,sigma): #normal distribution pdf
x = (x-mu)/sigma
return numpy.exp(-x**2/2)/(numpy.sqrt(2*numpy.pi)*sigma)
def invLogCDF(x,mu,sigma): #normal distribution cdf
x = (x - mu) / sigma
return norm.logcdf(-x) #note: we mutiple by -1 after normalization to better get the 1-cdf
def sigmoid(x):
return 1. / (1 + numpy.exp(-x))
def dsigmoid(x):
return x * (1. - x)
def tanh(x):
return numpy.tanh(x)
def dtanh(x):
return 1. - x * x
def softmax(x):
e = numpy.exp(x - numpy.max(x)) # prevent overflow
if e.ndim == 1:
return e / numpy.sum(e, axis=0)
else:
return e / numpy.array([numpy.sum(e, axis=1)]).T # ndim = 2
def ReLU(x):
return x * (x > 0)
def dReLU(x):
return 1. * (x > 0)
class rollmean:
def __init__(self,k):
self.winsize = k
self.window = numpy.zeros(self.winsize)
self.pointer = 0
def apply(self,newval):
self.window[self.pointer]=newval
self.pointer = (self.pointer+1) % self.winsize
return numpy.mean(self.window)
# probability density for the Gaussian dist
# def gaussian(x, mean=0.0, scale=1.0):
# s = 2 * numpy.power(scale, 2)
# e = numpy.exp( - numpy.power((x - mean), 2) / s )
# return e / numpy.square(numpy.pi * s)