Class: DNN::Activations::LeakyReLU

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 = 0.3) ⇒ LeakyReLU

Returns a new instance of LeakyReLU.

Parameters:

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

    The slope when the output value is negative.



86
87
88
# File 'lib/dnn/core/activations.rb', line 86

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

Instance Attribute Details

#alpha ⇒ Float (readonly)

Return the alpha value.

Returns:

  • (Float) —

    Return the alpha value.



79
80
81
# File 'lib/dnn/core/activations.rb', line 79

def alpha
  @alpha
end

Class Method Details

.from_hash(hash) ⇒ Object



81
82
83
# File 'lib/dnn/core/activations.rb', line 81

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

Instance Method Details

#backward(dy) ⇒ Object



97
98
99
100
101
# File 'lib/dnn/core/activations.rb', line 97

def backward(dy)
  @x[@x > 0] = 1
  @x[@x <= 0] = @alpha
  dy * @x
end

#forward(x) ⇒ Object



90
91
92
93
94
95
# File 'lib/dnn/core/activations.rb', line 90

def forward(x)
  @x = x.clone
  a = Xumo::SFloat.ones(x.shape)
  a[x <= 0] = @alpha
  x * a
end

#to_hash ⇒ Object



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

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