Class: SimpleNeuralNetwork::Network
- Inherits:
-
Object
- Object
- SimpleNeuralNetwork::Network
- Defined in:
- lib/network.rb
Defined Under Namespace
Classes: InvalidInputError
Instance Attribute Summary collapse
-
#edge_initialization_function ⇒ Object
Returns the value of attribute edge_initialization_function.
-
#inputs ⇒ Object
Returns the value of attribute inputs.
-
#layers ⇒ Object
An array of layers.
-
#neuron_bias_initialization_function ⇒ Object
Returns the value of attribute neuron_bias_initialization_function.
-
#normalization_function ⇒ Object
Returns the value of attribute normalization_function.
Class Method Summary collapse
-
.deserialize(string) ⇒ Object
Deserialize a JSON neural network back into a Ruby object Note that the normalization function will need to be reset.
Instance Method Summary collapse
- #clear_edge_caches ⇒ Object
- #create_layer(neurons:) ⇒ Object
-
#initialize ⇒ Network
constructor
A new instance of Network.
-
#input_size ⇒ Object
Returns the number of input nodes.
-
#output_size ⇒ Object
Returns the number of output nodes.
- #reset_normalization_function ⇒ Object
-
#run(inputs, skip_validation: false) ⇒ Object
Run an input set against the neural network.
-
#serialize ⇒ Object
Serializes the neural network into a JSON string.
Constructor Details
#initialize ⇒ Network
Returns a new instance of Network.
31 32 33 34 35 36 37 38 |
# File 'lib/network.rb', line 31 def initialize @layers = [] @inputs = [] @normalization_function = method(:default_normalization_function) @edge_initialization_function = method(:default_edge_initialization_function) @neuron_bias_initialization_function = method(:default_neuron_bias_initialization_function) end |
Instance Attribute Details
#edge_initialization_function ⇒ Object
Returns the value of attribute edge_initialization_function.
28 29 30 |
# File 'lib/network.rb', line 28 def edge_initialization_function @edge_initialization_function end |
#inputs ⇒ Object
Returns the value of attribute inputs.
24 25 26 |
# File 'lib/network.rb', line 24 def inputs @inputs end |
#layers ⇒ Object
An array of layers
22 23 24 |
# File 'lib/network.rb', line 22 def layers @layers end |
#neuron_bias_initialization_function ⇒ Object
Returns the value of attribute neuron_bias_initialization_function.
29 30 31 |
# File 'lib/network.rb', line 29 def neuron_bias_initialization_function @neuron_bias_initialization_function end |
#normalization_function ⇒ Object
Returns the value of attribute normalization_function.
26 27 28 |
# File 'lib/network.rb', line 26 def normalization_function @normalization_function end |
Class Method Details
.deserialize(string) ⇒ Object
Deserialize a JSON neural network back into a Ruby object Note that the normalization function will need to be reset. Normalization function serialization in the future would be cool.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/network.rb', line 118 def self.deserialize(string) hash = JSON.parse(string) network = Network.new hash["layers"].each do |layer| neurons_array = layer["neurons"] layer = Layer.new(neurons_array.length, network) network.layers << layer layer.neurons.each_with_index do |neuron, index| neuron_hash = neurons_array[index] neuron.bias = neuron_hash["bias"].to_f neuron.edges = neuron_hash["edges"].map(&:to_f) end end network.layers.each_with_index do |layer, index| unless index == 0 layer.prev_layer = network.layers[index - 1] end layer.next_layer = network.layers[index + 1] end network end |
Instance Method Details
#clear_edge_caches ⇒ Object
91 92 93 94 95 |
# File 'lib/network.rb', line 91 def clear_edge_caches @layers.each do |layer| layer.clear_edge_cache end end |
#create_layer(neurons:) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/network.rb', line 71 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 prev_layer.initialize_neuron_edges else @layers << Layer.new(neurons, self) end end |
#input_size ⇒ Object
Returns the number of input nodes
62 63 64 |
# File 'lib/network.rb', line 62 def input_size @layers[0].size end |
#output_size ⇒ Object
Returns the number of output nodes
67 68 69 |
# File 'lib/network.rb', line 67 def output_size @layers[-1].size end |
#reset_normalization_function ⇒ Object
87 88 89 |
# File 'lib/network.rb', line 87 def reset_normalization_function @normalization_function = method(:default_normalization_function) end |
#run(inputs, skip_validation: false) ⇒ Object
Run an input set against the neural network. Accepts an array of input integers between 0 and 1 Input array length must be equal to the size of the first layer. Returns an array of outputs.
skip_validation: Skips validations that may be expensive for large sets
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/network.rb', line 46 def run(inputs, skip_validation: false) unless skip_validation unless inputs.size == input_size && inputs.all? { |input| input >= 0 && input <= 1 } raise InvalidInputError.new("Invalid input passed to Network#run") end end @inputs = inputs # Get output from last layer. It recursively depends on layers before it. @layers[-1].get_output.map do |output| (@normalization_function || method(:default_normalization_function)).call(output) end end |
#serialize ⇒ Object
Serializes the neural network into a JSON string. This can later be deserialized back into a Network object Useful for storing partially trained neural networks. Note: Currently does not serialize bias init function, edge init function, or normalization function
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/network.rb', line 100 def serialize { layers: layers.map do |layer| { neurons: layer.neurons.map do |neuron| { bias: neuron.bias.to_f, edges: neuron.edges.map(&:to_f) } end } end }.to_json end |