class SimpleNeuralNetwork::Layer
Attributes
network[RW]
neurons[RW]
List of #{size} neurons
next_layer[RW]
prev_layer[RW]
size[RW]
Number of neurons
Public Class Methods
new(size, network)
click to toggle source
# File lib/layer.rb, line 16 def initialize(size, network) @size = size @neurons = [] @network = network populate_neurons end
Public Instance Methods
get_output()
click to toggle source
The method that drives network output resolution. get_output
calculates the array of node values for this layer. This is calculated by recursively fetching the output from the previous layer, then applying edge/node weight rules. The first layer will fetch it's values from @network.inputs
# File lib/layer.rb, line 28 def get_output if !prev_layer # This is the first layer, so the output set is simply the network input set @network.inputs else # Each neuron output value is calculated by: # output[i] = ( # (prev_layer.neurons[0] * prev_layer.neurons[0].edges[i]) # + (prev_layer.neurons[1] * prev_layer.neurons[1].edges[i]) # + ... # ) + self.neurons[i].bias prev_layer_output = prev_layer.get_output # Generate the output values for the layer (0..@size-1).map do |i| value = 0 prev_layer_output.each_with_index do |output, index| value += (output * prev_layer.neurons[index].edges[i]) end value + @neurons[i].bias end end end
initialize_neuron_edges()
click to toggle source
# File lib/layer.rb, line 55 def initialize_neuron_edges return unless @next_layer @neurons.each do |neuron| neuron.initialize_edges(@next_layer.size) end end
Private Instance Methods
populate_neurons()
click to toggle source
# File lib/layer.rb, line 65 def populate_neurons @size.times do @neurons << Neuron.new(layer: self) end end