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

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.



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

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

Instance Attribute Details

#alphaObject (readonly)

Returns the value of attribute alpha.



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

def alpha
  @alpha
end

Class Method Details

.load_hash(hash) ⇒ Object



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

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

Instance Method Details

#backward(dout) ⇒ Object



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

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



118
119
120
121
122
123
124
125
126
127
# File 'lib/dnn/core/activations.rb', line 118

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



138
139
140
# File 'lib/dnn/core/activations.rb', line 138

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