Class: DNN::Activations::ELU

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

Constant Summary collapse

NMath =
Xumo::NMath

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Layers::Layer

#build, #built?, #prev_layer, #shape

Constructor Details

#initialize(alpha = 1.0) ⇒ ELU



124
125
126
# File 'lib/dnn/core/activations.rb', line 124

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

Instance Attribute Details

#alphaObject (readonly)

Returns the value of attribute alpha.



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

def alpha
  @alpha
end

Class Method Details

.load_hash(hash) ⇒ Object



120
121
122
# File 'lib/dnn/core/activations.rb', line 120

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

Instance Method Details

#backward(dout) ⇒ Object



139
140
141
142
143
144
145
146
# File 'lib/dnn/core/activations.rb', line 139

def backward(dout)
  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)
  dout * (dx + dx2)
end

#forward(x) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/dnn/core/activations.rb', line 128

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_hashObject



148
149
150
# File 'lib/dnn/core/activations.rb', line 148

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