Class: CooCoo::ActivationFunctions::LeakyReLU

Inherits:
Identity show all
Defined in:
lib/coo-coo/activation_functions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Identity

#initial_bias, method_missing, #name, #prep_input, #prep_output_target, #to_s

Constructor Details

#initialize(pos = 1.0, neg = 0.0001) ⇒ LeakyReLU

Returns a new instance of LeakyReLU.



163
164
165
166
# File 'lib/coo-coo/activation_functions.rb', line 163

def initialize(pos = 1.0, neg = 0.0001)
  @positive_coeff = pos.to_f
  @negative_coeff = neg.to_f
end

Instance Attribute Details

#negative_coeffObject

Returns the value of attribute negative_coeff.



169
170
171
# File 'lib/coo-coo/activation_functions.rb', line 169

def negative_coeff
  @negative_coeff
end

#positive_coeffObject

Returns the value of attribute positive_coeff.



168
169
170
# File 'lib/coo-coo/activation_functions.rb', line 168

def positive_coeff
  @positive_coeff
end

Instance Method Details

#==(other) ⇒ Object



201
202
203
204
205
# File 'lib/coo-coo/activation_functions.rb', line 201

def ==(other)
  other.kind_of?(self.class) &&
    positive_coeff == other.positive_coeff &&
    negative_coeff == other.negative_coeff
end

#call(x) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/coo-coo/activation_functions.rb', line 171

def call(x)
  pos = x > 0

  if pos.kind_of?(FalseClass)
    x * @negative_coeff
  elsif pos.kind_of?(TrueClass)
    x * @positive_coeff
  else
    neg = x <= 0
    (x * pos * @positive_coeff) + (x * neg * @negative_coeff)
  end
end

#derivative(x, y = nil) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/coo-coo/activation_functions.rb', line 184

def derivative(x, y = nil)
  y ||= call(x)
  pos = y > 0
  if pos.kind_of?(FalseClass)
    @negative_coeff
  elsif pos.kind_of?(TrueClass)
    @positive_coeff
  else
    neg = y <= 0
    (pos * @positive_coeff) + (neg * @negative_coeff)
  end
end

#initial_weights(num_inputs, size) ⇒ Object



197
198
199
# File 'lib/coo-coo/activation_functions.rb', line 197

def initial_weights(num_inputs, size)
  CooCoo::Vector.rand(num_inputs * size) * (2.0 / (num_inputs * size).to_f).sqrt
end