Class: DNN::Layers::BatchNormalization
Instance Attribute Summary collapse
#params, #trainable
Attributes inherited from Layer
#input_shape
Class Method Summary
collapse
Instance Method Summary
collapse
#build, #update
Methods inherited from Layer
#build, #built?, #output_shape
Constructor Details
Returns a new instance of BatchNormalization.
311
312
313
314
|
# File 'lib/dnn/core/layers.rb', line 311
def initialize(momentum: 0.9)
super()
@momentum = momentum
end
|
Instance Attribute Details
#momentum ⇒ Object
Returns the value of attribute momentum.
305
306
307
|
# File 'lib/dnn/core/layers.rb', line 305
def momentum
@momentum
end
|
Class Method Details
.load_hash(hash) ⇒ Object
307
308
309
|
# File 'lib/dnn/core/layers.rb', line 307
def self.load_hash(hash)
self.new(momentum: hash[:momentum])
end
|
Instance Method Details
#backward(dout, learning_phase) ⇒ Object
333
334
335
336
337
338
339
340
341
342
343
344
|
# File 'lib/dnn/core/layers.rb', line 333
def backward(dout, learning_phase)
batch_size = dout.shape[0]
@beta.grad = dout.sum(0)
@gamma.grad = (@xn * dout).sum(0)
dxn = @gamma.data * dout
dxc = dxn / @std
dstd = -((dxn * @xc) / (@std**2)).sum(0)
dvar = 0.5 * dstd / @std
dxc += (2.0 / batch_size) * @xc * dvar
dmean = dxc.sum(0)
dxc - dmean / batch_size
end
|
#forward(x, learning_phase) ⇒ Object
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
# File 'lib/dnn/core/layers.rb', line 316
def forward(x, learning_phase)
if learning_phase
mean = x.mean(0)
@xc = x - mean
var = (@xc**2).mean(0)
@std = Xumo::NMath.sqrt(var + 1e-7)
xn = @xc / @std
@xn = xn
@running_mean.data = @momentum * @running_mean.data + (1 - @momentum) * mean
@running_var.data = @momentum * @running_var.data + (1 - @momentum) * var
else
xc = x - @running_mean.data
xn = xc / Xumo::NMath.sqrt(@running_var.data + 1e-7)
end
@gamma.data * xn + @beta.data
end
|
#to_hash ⇒ Object
346
347
348
|
# File 'lib/dnn/core/layers.rb', line 346
def to_hash
super({momentum: @momentum})
end
|