Class: DNN::Layers::BatchNormalization

Inherits:
HasParamLayer show all
Defined in:
lib/dnn/core/layers.rb

Instance Attribute Summary

Attributes inherited from HasParamLayer

#grads, #params

Instance Method Summary collapse

Methods inherited from HasParamLayer

#build, #initialize, #update

Methods inherited from Layer

#build, #builded?, #initialize, #prev_layer, #shape, #to_hash

Constructor Details

This class inherits a constructor from DNN::Layers::HasParamLayer

Instance Method Details

#backward(dout) ⇒ Object



459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/dnn/core/layers.rb', line 459

def backward(dout)
  batch_size = dout.shape[0]
  @grads[:beta] = dout.sum(0)
  @grads[:gamma] = (@xn * dout).sum(0)
  dxn = @params[:gamma] * 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) ⇒ Object



450
451
452
453
454
455
456
457
# File 'lib/dnn/core/layers.rb', line 450

def forward(x)
  @mean = x.mean(0)
  @xc = x - @mean
  @var = (@xc**2).mean(0)
  @std = NMath.sqrt(@var + 1e-7)
  @xn = @xc / @std
  @params[:gamma] * @xn + @params[:beta]
end