Class: Ai4r::Som::Layer

Inherits:
Object
  • Object
show all
Includes:
Data::Parameterizable
Defined in:
lib/ai4r/som/layer.rb

Overview

responsible for the implementation of the algorithm’s decays currently has methods for the decay of the radius, influence and learning rate. Has only one phase, which ends after the number of epochs is passed by the Som-class.

Parameters

  • nodes => number of nodes in the SOM (nodes x nodes). Has to be the same number

you pass to the SOM. Has to be an integer

  • radius => the initial radius for the neighborhood

  • epochs => number of epochs the algorithm runs, has to be an integer. By default it is set to 100

  • learning_rate => sets the initial learning rate

Direct Known Subclasses

TwoPhaseLayer

Instance Method Summary collapse

Methods included from Data::Parameterizable

#get_parameters, included, #set_parameters

Constructor Details

#initialize(nodes, radius, epochs = 100, learning_rate = 0.7) ⇒ Layer

Returns a new instance of Layer.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ai4r/som/layer.rb', line 34

def initialize(nodes, radius, epochs = 100, learning_rate = 0.7)
  raise("Too few nodes") if nodes < 3
  
  @nodes = nodes
  @epochs = epochs
  @radius = radius
  @time_for_epoch = @epochs / Math.log(nodes / 4.0)
  @time_for_epoch = @epochs + 1.0 if @time_for_epoch < @epochs

  @initial_learning_rate = learning_rate
end

Instance Method Details

#influence_decay(distance, radius) ⇒ Object

calculates the influnce decay for a certain distance and the current radius of the epoch



48
49
50
# File 'lib/ai4r/som/layer.rb', line 48

def influence_decay(distance, radius)
  Math.exp(- (distance.to_f**2 / 2.0 / radius.to_f**2))
end

#learning_rate_decay(epoch) ⇒ Object

calculates the learning rate decay. uses @time_for_epoch again and same rule applies:



60
61
62
# File 'lib/ai4r/som/layer.rb', line 60

def learning_rate_decay(epoch)
  @initial_learning_rate * ( 1 - epoch / @time_for_epoch)
end

#radius_decay(epoch) ⇒ Object

calculates the radius decay for the current epoch. Uses @time_for_epoch which has to be higher than the number of epochs, otherwise the decay will be - Infinity



54
55
56
# File 'lib/ai4r/som/layer.rb', line 54

def radius_decay(epoch)
  (@radius * ( 1 - epoch/ @time_for_epoch)).round
end