class Chainer::Functions::Activation::TanhGrad

Public Class Methods

new(x) click to toggle source
Calls superclass method Chainer::FunctionNode::new
# File lib/chainer/functions/activation/tanh.rb, line 49
def initialize(x)
  super()

  # The original input `x` is only required for cuDNN.
  # If it is None, this class does not use cuDNN.
  # Note that x must be c-contiguous and it is checked
  # in Tanh.forward_gpu.
  @x = x
end

Public Instance Methods

backward(indexes, grad_outputs) click to toggle source
# File lib/chainer/functions/activation/tanh.rb, line 67
def backward(indexes, grad_outputs)
  y, gy = get_retained_inputs
  g = grad_outputs[0]

  y_mul_g = y * g
  grad_y = -2 * gy * y_mul_g
  ggy = g - y * y_mul_g
  [grad_y, ggy]
end
forward(inputs) click to toggle source
# File lib/chainer/functions/activation/tanh.rb, line 59
def forward(inputs)
  retain_inputs([0, 1])
  y, gy = inputs

  one = y.class.new.fill(1)
  [Utils::Array.force_array(gy * (one - y * y))]
end