Class: DNN::Layers::ELU

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

Instance Attribute Summary collapse

Attributes inherited from Layer

#input_shape, #name

Instance Method Summary collapse

Methods inherited from Layer

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

Constructor Details

#initialize(alpha = 1.0) ⇒ ELU



103
104
105
106
# File 'lib/dnn/core/activations.rb', line 103

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

Instance Attribute Details

#alphaObject (readonly)

Returns the value of attribute alpha.



100
101
102
# File 'lib/dnn/core/activations.rb', line 100

def alpha
  @alpha
end

Instance Method Details

#backward(dy) ⇒ Object



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

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



108
109
110
111
112
113
114
115
116
117
# File 'lib/dnn/core/activations.rb', line 108

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

#load_hash(hash) ⇒ Object



132
133
134
# File 'lib/dnn/core/activations.rb', line 132

def load_hash(hash)
  initialize(hash[:alpha])
end

#to_hashObject



128
129
130
# File 'lib/dnn/core/activations.rb', line 128

def to_hash
  super(alpha: @alpha)
end