Class Ai4r::NeuralNetwork::Hopfield
In: lib/ai4r/neural_network/hopfield.rb
Parent: Object

Hopfield Net =

A Hopfield Network is a recurrent Artificial Neural Network. Hopfield nets are able to memorize a set of patterns, and then evaluate an input, returning the most similar stored pattern (although convergence to one of the stored patterns is not guaranteed). Hopfield nets are great to deal with input noise. If a system accepts a discrete set of inputs, but inputs are subject to noise, you can use a Hopfield net to eliminate noise and identified the given input.

How to Use =

  data_set = Ai4r::Data::DataSet.new :data_items => array_of_patterns
  net = Ai4r::NeuralNetworks::Hopfield.new.train data_set
  net.eval input
    => one of the stored patterns in array_of_patterns

Methods

Included Modules

Ai4r::Data::Parameterizable

Attributes

nodes  [R] 
weights  [R] 

Public Class methods

Public Instance methods

Propagates the input until the network returns one of the memorized patterns, or a maximum of "eval_iterations" times.

You can use run instead of eval to propagate values step by step. With this you can verify the progress of the network output with each step.

E.g.:

  pattern = input
  100.times do
     pattern = net.run(pattern)
     puts pattern.inspect
  end

Prepares the network to memorize the given data set. Future calls to eval (should) return one of the memorized data items. A Hopfield network converges to a local minimum, but converge to one of the "memorized" patterns is not guaranteed.

Protected Instance methods

Initialize all nodes with "inactive" state.

Create a partial weigth matrix:

  [
    [w(1,0)],
    [w(2,0)], [w(2,1)],
    [w(3,0)], [w(3,1)], [w(3,2)],
    ...
    [w(n-1,0)], [w(n-1,1)], [w(n-1,2)], ... , [w(n-1,n-2)]
  ]

where n is the number of nodes.

We are saving memory here, as:

  • w[i][i] = 0 (no node connects with itself)
  • w[i][j] = w[j][i] (weigths are symmetric)

Use read_weight(i,j) to find out weight between node i and j

Select a single node randomly and propagate its state to all other nodes

read_weight(i,j) reads the weigth matrix and returns weight between node i and j

Set all nodes state to the given input. inputs parameter must have the same dimension as nodes

[Validate]