Class: Ai4r::NeuralNetwork::Hopfield
- Inherits:
-
Object
- Object
- Ai4r::NeuralNetwork::Hopfield
- 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
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
-
#weights ⇒ Object
readonly
Returns the value of attribute weights.
Instance Method Summary collapse
-
#energy ⇒ Object
Calculate network energy using current node states and weights.
-
#eval(input, trace: false) ⇒ Object
Propagates the input until the network returns one of the memorized patterns, or a maximum of “eval_iterations” times.
- #initialize(params = {}) ⇒ Object constructor
-
#run(input) ⇒ Object
You can use run instead of eval to propagate values step by step.
-
#train(data_set) ⇒ Object
Prepares the network to memorize the given data set.
Methods included from Data::Parameterizable
#get_parameters, included, #set_parameters
Constructor Details
#initialize(params = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ai4r/neural_network/hopfield.rb', line 52 def initialize(params = {}) @eval_iterations = 500 @active_node_value = 1 @inactive_node_value = -1 @threshold = 0 @weight_scaling = nil @stop_when_stable = false @update_strategy = :async_random # Deterministic random generator to guarantee reproducible behaviour @rng = Random.new(3) set_parameters(params) if params && !params.empty? end |
Instance Attribute Details
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
35 36 37 |
# File 'lib/ai4r/neural_network/hopfield.rb', line 35 def nodes @nodes end |
#weights ⇒ Object (readonly)
Returns the value of attribute weights.
35 36 37 |
# File 'lib/ai4r/neural_network/hopfield.rb', line 35 def weights @weights end |
Instance Method Details
#energy ⇒ Object
Calculate network energy using current node states and weights. Energy = -0.5 * Σ w_ij * s_i * s_j
138 139 140 141 142 143 144 145 146 |
# File 'lib/ai4r/neural_network/hopfield.rb', line 138 def energy sum = 0.0 @nodes.each_with_index do |s_i, i| i.times do |j| sum += read_weight(i, j) * s_i * @nodes[j] end end -sum end |
#eval(input, trace: false) ⇒ Object
Propagates the input until the network returns one of the memorized patterns, or a maximum of “eval_iterations” times.
If trace: true is passed the method returns a hash with the :states and :energies recorded at every iteration (including the initial state). This can be used to visualize convergence.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/ai4r/neural_network/hopfield.rb', line 106 def eval(input, trace: false) set_input(input) prev_energy = energy if trace states = [@nodes.clone] energies = [prev_energy] end @eval_iterations.times do propagate new_energy = energy if trace states << @nodes.clone energies << new_energy end if @data_set.data_items.include?(@nodes) return(if trace { states: states, energies: energies } else @nodes end) end break if @stop_when_stable && new_energy == prev_energy prev_energy = new_energy end trace ? { states: states, energies: energies } : @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
91 92 93 94 95 |
# File 'lib/ai4r/neural_network/hopfield.rb', line 91 def run(input) set_input(input) propagate @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.
71 72 73 74 75 76 77 |
# File 'lib/ai4r/neural_network/hopfield.rb', line 71 def train(data_set) @data_set = data_set validate_training_data initialize_nodes(@data_set) initialize_weights(@data_set) self end |