Class: Brain::Hopfield

Inherits:
Object
  • Object
show all
Defined in:
lib/brain/hopfield.rb

Overview

Represents a Hopfield net.

  net = Brain::Hopfield[[-1, -1, -1, -1], [1, 1, 1, 1]]
  sample = net.associate [1, 1, -1, 1]
  sample.run until sample.associated?
  sample.current # => [1, 1, 1, 1]
  sample.iterations # => 2

In this example, it can take up to 4 iterations to associate the sample (actual value depends on a random order of updating neurons).

Defined Under Namespace

Classes: Sample

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(learning_samples) ⇒ Hopfield

Learning samples must be not empty and have same dimension.



25
26
27
28
29
30
31
32
33
# File 'lib/brain/hopfield.rb', line 25

def initialize(learning_samples)
  @learning_samples = learning_samples
  learning_samples_must_be_not_empty!
  
  @dimension = @learning_samples.first.size
  learning_samples_must_have_same_dimension!
  
  calculate_weight_matrix
end

Class Method Details

.[](*learning_samples) ⇒ Object

Shortcut for creation of a new Hopfield object.



20
21
22
# File 'lib/brain/hopfield.rb', line 20

def self.[](*learning_samples)
  new learning_samples
end

Instance Method Details

#associate(sample) ⇒ Object

Returns a Brain::Hopfield::Sample object which can be used for an association of given sample.



36
37
38
# File 'lib/brain/hopfield.rb', line 36

def associate(sample)
  Sample.new self, sample
end

#dimensionObject

Dimension attribute.



51
52
53
# File 'lib/brain/hopfield.rb', line 51

def dimension
  @dimension
end

#learning_samplesObject

Learning samples attribute.



41
42
43
# File 'lib/brain/hopfield.rb', line 41

def learning_samples
  @learning_samples
end

#weightsObject

Weights attribute.



46
47
48
# File 'lib/brain/hopfield.rb', line 46

def weights
  @weights
end