class SimpleNeuralNetwork::Network
Attributes
inputs[RW]
layers[RW]
An array of layers
Public Class Methods
new()
click to toggle source
# File lib/network.rb, line 18 def initialize @layers = [] @inputs = [] end
Public Instance Methods
create_layer(neurons:)
click to toggle source
# File lib/network.rb, line 47 def create_layer(neurons:) unless @layers.empty? new_layer = Layer.new(neurons, self) prev_layer = @layers.last @layers << new_layer new_layer.prev_layer = prev_layer prev_layer.next_layer = new_layer else @layers << Layer.new(neurons, self) end end
initialize_edges()
click to toggle source
This traverses the network and initializes all neurons with edges Initializes with random weights between -5 and 5
# File lib/network.rb, line 63 def initialize_edges @layers.each(&:initialize_neuron_edges) end
input_size()
click to toggle source
Returns the number of input nodes
# File lib/network.rb, line 38 def input_size @layers[0].size end
output_size()
click to toggle source
Returns the number of output nodes
# File lib/network.rb, line 43 def output_size @layers[-1].size end
run(inputs)
click to toggle source
Run an input set against the neural network. Accepts an array of input integers between 0 and 1 of length input_size
Returns
# File lib/network.rb, line 26 def run(inputs) unless inputs.size == input_size && inputs.all? { |input| input >= 0 && input <= 1 } raise InvalidInputError.new("Invalid input passed to Network#run") end @inputs = inputs # Get output from last layer. It recursively depends on layers before it. @layers[-1].get_output end