Class: DNN::Activations::ELU

Inherits:
Layers::Layer show all
Defined in:
lib/dnn/core/activations.rb

Instance Attribute Summary collapse

Attributes inherited from Layers::Layer

#input_shape, #learning_phase

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Layers::Layer

#build, #built?, #output_shape

Constructor Details

#initialize(alpha = 1.0) ⇒ ELU

Returns a new instance of ELU.

Parameters:

  • (defaults to: 1.0)

    The slope when the output value is negative.



118
119
120
# File 'lib/dnn/core/activations.rb', line 118

def initialize(alpha = 1.0)
  @alpha = alpha
end

Instance Attribute Details

#alpha ⇒ Float (readonly)

Return the alpha value.

Returns:

  • Return the alpha value.



111
112
113
# File 'lib/dnn/core/activations.rb', line 111

def alpha
  @alpha
end

Class Method Details

.from_hash(hash) ⇒ Object



113
114
115
# File 'lib/dnn/core/activations.rb', line 113

def self.from_hash(hash)
  self.new(hash[:alpha])
end

Instance Method Details

#backward(dy) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/dnn/core/activations.rb', line 133

def backward(dy)
  dx = Xumo::SFloat.ones(@x.shape)
  dx[@x < 0] = 0
  dx2 = Xumo::SFloat.zeros(@x.shape)
  dx2[@x < 0] = 1
  dx2 *= @alpha * NMath.exp(@x)
  dy * (dx + dx2)
end

#forward(x) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/dnn/core/activations.rb', line 122

def forward(x)
  @x = x
  x1 = Xumo::SFloat.zeros(x.shape)
  x1[x >= 0] = 1
  x1 *= x
  x2 = Xumo::SFloat.zeros(x.shape)
  x2[x < 0] = 1
  x2 *= @alpha * NMath.exp(x) - @alpha
  x1 + x2
end

#to_hash ⇒ Object



142
143
144
# File 'lib/dnn/core/activations.rb', line 142

def to_hash
  {class: self.class.name, alpha: @alpha}
end