Class: Chainer::Functions::Activation::Tanh

Inherits:
Chainer::FunctionNode show all
Defined in:
lib/chainer/functions/activation/tanh.rb

Overview

Hyperbolic tangent function.

Instance Attribute Summary

Attributes inherited from Chainer::FunctionNode

#inputs, #outputs, #rank

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Chainer::FunctionNode

#apply, #backward_accumulate, #forward_cpu, #get_retained_inputs, #get_retained_outputs, #initialize, #label, #output_data, #retain_inputs, #retain_outputs, #unchain

Constructor Details

This class inherits a constructor from Chainer::FunctionNode

Class Method Details

.tanh(x) ⇒ Chainer::Variable

Elementwise hyperbolic tangent function.

$$ f(x)=\tanh(x). $$

Examples:

> x = Numo::SFloat.new(3).seq(-1, 2)
=> Numo::SFloat#shape=[3]
[-1, 1, 3]
> F = Chainer::Functions::Activation::Tanh
> F.tanh(x).data
=> Numo::SFloat#shape=[3]
[-0.761594, 0.761594, 0.995055]

Parameters:

  • x (Chainer::Variable or Numo::NArray or Cumo::NArray)

    Input variable. A $(s_1, s_2, …, s_N)$-shaped float array.

Returns:

  • (Chainer::Variable)

    Output variable. A $(s_1, s_2, …, s_N)$-shaped float array.



23
24
25
# File 'lib/chainer/functions/activation/tanh.rb', line 23

def self.tanh(x)
  self.new.apply([x]).first
end

Instance Method Details

#backward(indexes, grad_outputs) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/chainer/functions/activation/tanh.rb', line 35

def backward(indexes, grad_outputs)
  if @use_cudnn
    x = get_retained_inputs.first.data
  else
    x = nil
  end

  y = get_retained_outputs.first
  gy = grad_outputs.first
  TanhGrad.new(x).apply([y, gy])
end

#forward(x) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/chainer/functions/activation/tanh.rb', line 27

def forward(x)
  xm = Chainer.get_array_module(x[0])
  y = Utils::Array.force_array(xm::NMath.tanh(x[0]))
  retain_outputs([0])
  @use_cudnn = false
  [y]
end