Class: Brain::Hopfield::Sample

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

Overview

Uses a given Hopfield object for a successive accosiation with a given sample.

When there is too much noise in the initial sample, it can associate it with an inverse version of a learning sample. Example:

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

Initial sample [1, -1, -1, -1] containes too much noise to be comparable with [1, 1, 1, 1]. So sample is associated with the inverse version of [1, 1, 1, 1] which equals [-1, -1, -1, -1].

Instance Method Summary collapse

Constructor Details

#initialize(net, sample) ⇒ Sample

A sample must have same dimension as a Hopfield object.



20
21
22
23
24
25
26
# File 'lib/brain/hopfield/sample.rb', line 20

def initialize(net, sample)
  @net, @initial, @indexes = net, sample, []
  @current, @iterations, @associated = initial, 0, false
  
  sample_must_have_same_dimension_as_net!
  @associated = true if known_sample?
end

Instance Method Details

#associated?Boolean

Returns true if the current sample is accosiated.

Returns:

  • (Boolean)


29
30
31
# File 'lib/brain/hopfield/sample.rb', line 29

def associated?
  @associated
end

#currentObject

Current state of sample.



59
60
61
# File 'lib/brain/hopfield/sample.rb', line 59

def current
  @current
end

#energyObject

Measure of closerness to learning samples. The least energy state corresponds to an association with a learning sample.



44
45
46
# File 'lib/brain/hopfield/sample.rb', line 44

def energy
  (Matrix.row_vector(current) * net.weights).row(0).inner_product current
end

#initialObject

Initial state of sample.



54
55
56
# File 'lib/brain/hopfield/sample.rb', line 54

def initial
  @initial
end

#iterationsObject

Number of itererations.



64
65
66
# File 'lib/brain/hopfield/sample.rb', line 64

def iterations
  @iterations
end

#netObject

A corresponding Hopfield object.



49
50
51
# File 'lib/brain/hopfield/sample.rb', line 49

def net
  @net
end

#run(number = 1) ⇒ Object

Used to update a state of the current sample in a random order. A number of updated positions is controlled with a given parameter.



34
35
36
37
38
39
40
41
# File 'lib/brain/hopfield/sample.rb', line 34

def run(number = 1)
  number.times do
    break if associated?
    update_next_neuron
    @associated = true if minimal_energy? or known_sample?
  end
  current
end