Class: NeuralNetwork::Network

Inherits:
Object
  • Object
show all
Defined in:
lib/neural_network/network.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(layer_sizes) ⇒ Network

Returns a new instance of Network.



5
6
7
8
9
10
11
12
# 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

Instance Attribute Details

#errorObject

Returns the value of attribute error.



3
4
5
# File 'lib/neural_network/network.rb', line 3

def error
  @error
end

#hidden_layersObject

Returns the value of attribute hidden_layers.



3
4
5
# File 'lib/neural_network/network.rb', line 3

def hidden_layers
  @hidden_layers
end

#input_layerObject

Returns the value of attribute input_layer.



3
4
5
# File 'lib/neural_network/network.rb', line 3

def input_layer
  @input_layer
end

#output_layerObject

Returns the value of attribute output_layer.



3
4
5
# File 'lib/neural_network/network.rb', line 3

def output_layer
  @output_layer
end

Instance Method Details

#activate(input_values) ⇒ Object



14
15
16
17
18
# 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) ⇒ Object

all layers in reverse, back propogate!!



21
22
23
24
25
26
27
28
29
# 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