Module: Neuronet::Backpropagate

Included in:
MiddleNeuron, Neuron, NoisyBackpropagate, OutputNeuron
Defined in:
lib/neuronet/backpropagate.rb

Overview

Backpropagate provides simple, clamp-limited weight/bias updates.

Instance Method Summary collapse

Instance Method Details

#backpropagate(error) ⇒ Object

Back-propagates errors, updating bias and connection weights. Clamps updates to [-max, +max]. Recursively calls on connected neurons.



9
10
11
12
13
14
15
# File 'lib/neuronet/backpropagate.rb', line 9

def backpropagate(error)
  return if @backpropagated

  @backpropagated = true
  update_bias(error)
  update_connections(error)
end

#backpropagate!(error) ⇒ Object



42
43
44
45
# File 'lib/neuronet/backpropagate.rb', line 42

def backpropagate!(error)
  reset_backpropagated!
  backpropagate(error)
end

#reset_backpropagated!Object

rubocop: enable Style/NestedTernaryOperator



35
36
37
38
39
40
# File 'lib/neuronet/backpropagate.rb', line 35

def reset_backpropagated!
  return unless @backpropagated

  @backpropagated = false
  connections.each { |c| c.neuron.reset_backpropagated! }
end

#update_bias(error) ⇒ Object

rubocop: disable Style/NestedTernaryOperator



18
19
20
21
22
# File 'lib/neuronet/backpropagate.rb', line 18

def update_bias(error)
  bmax = Config.bias_clamp
  b = bias + error
  self.bias = b.abs > bmax ? (b.positive? ? bmax : -bmax) : b
end

#update_connections(error) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/neuronet/backpropagate.rb', line 24

def update_connections(error)
  wmax = Config.weight_clamp
  connections.each do |c|
    n = c.neuron
    w = c.weight + (n.activation * error)
    c.weight = w.abs > wmax ? (w.positive? ? wmax : -wmax) : w
    n.backpropagate(error)
  end
end