Class: Chainer::Functions::Normalization::BatchNormalizationGrad
- Inherits:
-
Chainer::Function
- Object
- Chainer::Function
- Chainer::Functions::Normalization::BatchNormalizationGrad
- Includes:
- Calculation
- Defined in:
- lib/chainer/functions/normalization/batch_normalization.rb
Instance Attribute Summary
Attributes inherited from Chainer::Function
#inputs, #output_data, #outputs, #owned_node, #rank, #retain_after_backward
Instance Method Summary collapse
- #backward(inputs, grad_outputs) ⇒ Object
- #forward(inputs) ⇒ Object
-
#initialize(eps, expander, axis, mean, inv_std) ⇒ BatchNormalizationGrad
constructor
A new instance of BatchNormalizationGrad.
Methods included from Calculation
#apply_bn_fwd, #x_hat, #zero_if_none
Methods inherited from Chainer::Function
#backward_cpu, #backward_gpu, #call, #forward_cpu, #forward_gpu, #label, #node, #retain_inputs, #retain_outputs
Constructor Details
#initialize(eps, expander, axis, mean, inv_std) ⇒ BatchNormalizationGrad
Returns a new instance of BatchNormalizationGrad.
107 108 109 110 111 112 113 |
# File 'lib/chainer/functions/normalization/batch_normalization.rb', line 107 def initialize(eps, , axis, mean, inv_std) @eps = eps = @axis = axis @mean = mean @inv_std = inv_std end |
Instance Method Details
#backward(inputs, grad_outputs) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/chainer/functions/normalization/batch_normalization.rb', line 132 def backward(inputs, grad_outputs) = x, gamma, gy = inputs gx1, ggamma1, = output_data ggx1, gggamma1, ggbeta1 = grad_outputs xp = Chainer.get_array_module(x) # auxiliary values inv_m = gamma.class.new.fill(1.0 / x.size.div(gamma.size)) r = ggx1.nil? ? 0 : (gx1 * ggx1).sum(axis: @axis) coeff = gamma * @inv_std coeff_m = coeff * inv_m x_hat = x_hat(x, .(@mean), .(@inv_std)) # handle None in output gradients ggx1 = zero_if_none(xp, ggx1, x.shape, x.class) gggamma1 = zero_if_none(xp, gggamma1, gamma.shape, gamma.class) ggbeta1 = zero_if_none(xp, ggbeta1, gamma.shape, gamma.class) gggamma2 = gggamma1 - coeff_m * (x_hat * ggx1).sum(axis: @axis) ggbeta2 = ggbeta1 - coeff_m * ggx1.sum(axis: @axis) ggamma2 = r / gamma gx_hat2 = (.(gggamma2) * gy - .(coeff_m * ggamma1) * ggx1) gstd2 = -@inv_std * (r + (x_hat * gx_hat2).sum(axis: @axis)) gmean2 = -@inv_std * gx_hat2.sum(axis: @axis) gx2 = .(@inv_std) * gx_hat2 + inv_m * (.(gmean2) + x_hat * .(gstd2)) ggy2 = (.(gggamma2) * x_hat + .(ggbeta2) + .(coeff) * ggx1) [gx2, ggamma2, ggy2] end |
#forward(inputs) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/chainer/functions/normalization/batch_normalization.rb', line 115 def forward(inputs) retain_inputs([0, 1, 2]) x, gamma, gy = inputs = inv_m = gamma.class.new.fill(1.0 / x.size.div(gamma.size)) xp = Chainer.get_array_module(x) gbeta = gy.sum(axis: @axis) x_hat = x_hat(x, .(@mean), .(@inv_std)) ggamma = (gy * x_hat).sum(axis: @axis) gx = .(gamma * @inv_std) * (gy - (x_hat * .(ggamma) + .(gbeta)) * inv_m) retain_outputs([0, 1]) [gx, ggamma, gbeta] end |