Class: Chainer::Functions::Activation::Relu

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

Overview

Rectified Linear Unit.

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

.relu(x) ⇒ Chainer::Variable

Rectified Linear Unit function.

$$ f(x)=\max(0, x). $$

Examples:

> x = Numo::SFloat[[-1, 0], [2, -3], [-2, 1]]
> (x < 0).any?
=> true
> F = Chainer::Functions::Activation::Relu
> y = F.relu(x)
> (y.data < 0).any?
=> false
> y.shape
=> [3, 2]

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.



25
26
27
28
# File 'lib/chainer/functions/activation/relu.rb', line 25

def self.relu(x)
  y, = self.new.apply([x])
  y
end

Instance Method Details

#backward(indexes, gy) ⇒ Object



35
36
37
38
# File 'lib/chainer/functions/activation/relu.rb', line 35

def backward(indexes, gy)
  y = get_retained_outputs.first
  ReLUGrad2.new(y).apply([gy[0]])
end

#forward(x) ⇒ Object



30
31
32
33
# File 'lib/chainer/functions/activation/relu.rb', line 30

def forward(x)
  retain_outputs([0])
  [Utils::Array.force_array(x[0].class.maximum(x[0], 0))]
end