class NeuralNetwork::Network

Attributes

error[RW]
hidden_layers[RW]
input_layer[RW]
output_layer[RW]

Public Class Methods

new(layer_sizes) click to toggle source
# File lib/neural_network/network.rb, line 5
def initialize(layer_sizes)
  @input_layer    = Layer.new(layer_sizes.shift)
  @output_layer   = Layer.new(layer_sizes.pop)
  @hidden_layers  = layer_sizes.map{|layer_size| Layer.new(layer_size)}
  @error          = 0

  connect_layers
end

Public Instance Methods

activate(input_values) click to toggle source
# File lib/neural_network/network.rb, line 14
def activate(input_values)
  @input_layer.activate(input_values)
  @hidden_layers.each(&:activate)
  @output_layer.activate
end
train(target_outputs) click to toggle source

all layers in reverse, back propogate!!

# File lib/neural_network/network.rb, line 21
def train(target_outputs)
  @output_layer.train(target_outputs)

  # set the new network error after training
  @error = error_function(target_outputs)

  @hidden_layers.reverse.each(&:train)
  @input_layer.train
end

Private Instance Methods

connect_layers() click to toggle source
# File lib/neural_network/network.rb, line 39
def connect_layers
  layers = [
    [@input_layer],
    @hidden_layers,
    [@output_layer]
  ].flatten

  layers.each_with_index do |layer, index|
    nextIndex = index + 1

    if layers[nextIndex]
      layer.connect(layers[nextIndex])
    end
  end
end
error_function(target_outputs) click to toggle source
# File lib/neural_network/network.rb, line 33
def error_function(target_outputs)
  @output_layer.neurons.each_with_index.reduce(0) do |sum, (neuron, index)|
    sum + 0.5 * (target_outputs[index] - neuron.output) ** 2
  end / @output_layer.neurons.length
end