Module: Neuronet::Trainable

Included in:
Deep, FeedForward, MLP, Perceptron
Defined in:
lib/neuronet/trainable.rb

Overview

Trainable adds error backpropagation and training.

Instance Method Summary collapse

Instance Method Details

#pairs(pairs, nju: expected_nju) ⇒ Object



6
7
8
# File 'lib/neuronet/trainable.rb', line 6

def pairs(pairs, nju: expected_nju)
  pairs.shuffle.each { |inputs, targets| train(inputs, targets, nju:) }
end

#pivot(errors) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/neuronet/trainable.rb', line 18

def pivot(errors)
  error = index = 0.0
  errors.each_with_index do |e, i|
    next unless e.abs > error.abs

    error = e
    index = i
  end
  [error, index]
end

#sum_of_squared_errors(pairs) ⇒ Object



29
30
31
32
33
34
# File 'lib/neuronet/trainable.rb', line 29

def sum_of_squared_errors(pairs)
  pairs.sum do |inputs, targets|
    actuals = self * inputs
    targets.zip(actuals).sum { |t, a| (e = t - a) * e }
  end
end

#train(inputs, targets, nju:) ⇒ Object



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

def train(inputs, targets, nju:)
  actuals = self * inputs
  errors = targets.zip(actuals).map { |target, actual| target - actual }
  error, index = pivot(errors)
  neuron = output_layer[index]
  neuron.backpropagate!(error / nju)
end