Class: Newral::Networks::BackpropagationNetwork

Inherits:
Network
  • Object
show all
Defined in:
lib/newral/networks/backpropagation_network.rb

Instance Attribute Summary

Attributes inherited from Network

#layers, #neurons, #output

Instance Method Summary collapse

Methods inherited from Network

#add_layer, #add_neuron, #calculate, #calculate_error, #connect, define, #move, #move_random, #number_of_directions, #output_of_neuron, #set_weights_and_bias, #update, #update_first_layer_with_vector, #update_layers, #update_neuron, #update_with_vector

Constructor Details

#initialize(number_of_inputs: 2, number_of_hidden: 2, number_of_outputs: 2) ⇒ BackpropagationNetwork

Returns a new instance of BackpropagationNetwork.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/newral/networks/backpropagation_network.rb', line 8

def initialize( number_of_inputs:2, number_of_hidden:2, number_of_outputs:2 )
  super()
  add_layer "hidden" do 
    number_of_hidden.times do |idx|
      add_neuron "hidden_#{idx}", weight_length:number_of_inputs
    end 
  end

  
  add_layer "output" do 
    number_of_outputs.times do |idx|
      add_neuron "output_#{idx}", weight_length:number_of_hidden
    end 
  end
  
  # in this network all hidden neurons link to all output neurons
  @layers["hidden"].neurons.each do |hidden_neuron|
    @layers["output"].neurons.each  do |output_neuron|
      output_neuron.add_input hidden_neuron
    end 
  end
end

Instance Method Details

#output_weights(neuron) ⇒ Object

gets the weights of the output neurons this input feeds to this of course can be done much simpler (as its always the nth weight of the output neuron) however we want to stay explicit



54
55
56
57
58
59
60
61
62
63
# File 'lib/newral/networks/backpropagation_network.rb', line 54

def output_weights( neuron )
  raise Errors::OnlyPossibleForHidden unless @layers["hidden"].neurons.member?( neuron )
  weights = []
  @layers["output"].neurons.each do |output_neuron|
    output_neuron.inputs.each_with_index do |input,idx| 
      weights << output_neuron.weights[ idx ] if input == neuron
    end 
  end 
 weights 
end

#train(input: [], output: []) ⇒ Object

gets an array of inputs and the corresponding expected outputs first we update our output layer then our hidden layer



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/newral/networks/backpropagation_network.rb', line 35

def train( input: [], output: [] )
  before_error =  calculate_error( input: input,output: output )
  input.each_with_index do |input,idx| 
    calculated_output = update_with_vector( input )
    @layers["output"].neurons.each_with_index do |neuron,neuron_idx|
      neuron.adjust_weights( expected: output[ idx ][ neuron_idx ])
    end 

    @layers["hidden"].neurons.each do |neuron|
      neuron.adjust_weights( expected: output[ idx ], layer: :hidden, output: calculated_output, weights_at_output_nodes: output_weights( neuron ))
    end 
  end
  new_error =  calculate_error( input: input,output: output )
  before_error-new_error
end