Class: Ai4r::NeuralNetwork::Hopfield

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Data::Parameterizable

#get_parameters, included, #set_parameters

Constructor Details

#initializeHopfield

Returns a new instance of Hopfield.



45
46
47
48
49
50
# File 'lib/ai4r/neural_network/hopfield.rb', line 45

def initialize
  @eval_iterations = 500
  @active_node_value = 1
  @inactive_node_value = -1
  @threshold = 0
end

Instance Attribute Details

#nodesObject (readonly)

Returns the value of attribute nodes.



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

def nodes
  @nodes
end

#weightsObject (readonly)

Returns the value of attribute weights.



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

def weights
  @weights
end

Instance Method Details

#eval(input) ⇒ Object

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



81
82
83
84
85
86
87
88
# File 'lib/ai4r/neural_network/hopfield.rb', line 81

def eval(input)
  set_input(input)
  @eval_iterations.times do
    propagate  
    break if @data_set.data_items.include?(@nodes)
  end
  return @nodes
end

#run(input) ⇒ Object

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


73
74
75
76
77
# File 'lib/ai4r/neural_network/hopfield.rb', line 73

def run(input)
  set_input(input)
  propagate
  return @nodes
end

#train(data_set) ⇒ Object

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.



56
57
58
59
60
61
# File 'lib/ai4r/neural_network/hopfield.rb', line 56

def train(data_set)
  @data_set = data_set
  initialize_nodes(@data_set)
  initialize_weights(@data_set)
  return self
end