MCPcopy
hub / github.com/junyanz/iGAN / batchnorm

Function batchnorm

lib/ops.py:57–88  ·  view source on GitHub ↗

batchnorm with support for not using scale and shift parameters as well as inference values (u and s) and partial batchnorm (via a) will detect and use convolutional or fully connected version

(X, g=None, b=None, u=None, s=None, a=1., e=1e-8)

Source from the content-addressed store, hash-verified

55
56
57def batchnorm(X, g=None, b=None, u=None, s=None, a=1., e=1e-8):
58 """
59 batchnorm with support for not using scale and shift parameters
60 as well as inference values (u and s) and partial batchnorm (via a)
61 will detect and use convolutional or fully connected version
62 """
63 if X.ndim == 4:
64 if u is not None and s is not None:
65 b_u = u.dimshuffle('x', 0, 'x', 'x')
66 b_s = s.dimshuffle('x', 0, 'x', 'x')
67 else:
68 b_u = T.mean(X, axis=[0, 2, 3]).dimshuffle('x', 0, 'x', 'x')
69 b_s = T.mean(T.sqr(X - b_u), axis=[0, 2, 3]).dimshuffle('x', 0, 'x', 'x')
70 if a != 1:
71 b_u = (1. - a) * 0. + a * b_u
72 b_s = (1. - a) * 1. + a * b_s
73 X = (X - b_u) / T.sqrt(b_s + e)
74 if g is not None and b is not None:
75 X = X * g.dimshuffle('x', 0, 'x', 'x') + b.dimshuffle('x', 0, 'x', 'x')
76 elif X.ndim == 2:
77 if u is None and s is None:
78 u = T.mean(X, axis=0)
79 s = T.mean(T.sqr(X - u), axis=0)
80 if a != 1:
81 u = (1. - a) * 0. + a * u
82 s = (1. - a) * 1. + a * s
83 X = (X - u) / T.sqrt(s + e)
84 if g is not None and b is not None:
85 X = X * g + b
86 else:
87 raise NotImplementedError
88 return X
89
90
91def deconv(X, w, subsample=(1, 1), border_mode=(0, 0), conv_mode='conv'):

Callers 10

genFunction · 0.90
discrimFunction · 0.90
gen_batchnormFunction · 0.90
discrim_batchnormFunction · 0.90
predictFunction · 0.90
predict_batchnormFunction · 0.90
gen_testFunction · 0.90
disc_testFunction · 0.90
gen_testFunction · 0.90
predict_testFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected