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, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Layers::Layer

#build, #built?, #call, call, #output_shape

Constructor Details

#initialize(alpha = 1.0) ⇒ ELU

Returns a new instance of ELU.

Parameters:

  • alpha (Float) (defaults to: 1.0) —

    The slope when the output value is negative.



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

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

Instance Attribute Details

#alpha ⇒ Object (readonly)

Returns the value of attribute alpha.



109
110
111
# File 'lib/dnn/core/activations.rb', line 109

def alpha
  @alpha
end

Class Method Details

.from_hash(hash) ⇒ Object



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

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

Instance Method Details

#backward(dy) ⇒ Object



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

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 * Xumo::NMath.exp(@x)
  dy * (dx + dx2)
end

#forward(x) ⇒ Object



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

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 * Xumo::NMath.exp(x) - @alpha
  x1 + x2
end

#to_hash ⇒ Object



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

def to_hash
  super(alpha: @alpha)
end