Class: SimpleNeuralNetwork::Network

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

Defined Under Namespace

Classes: InvalidInputError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNetwork

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_functionObject

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

#inputsObject

Returns the value of attribute inputs.



24
25
26
# File 'lib/network.rb', line 24

def inputs
  @inputs
end

#layersObject

An array of layers



22
23
24
# File 'lib/network.rb', line 22

def layers
  @layers
end

#neuron_bias_initialization_functionObject

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=(value) ⇒ Object (writeonly)

Sets the attribute normalization_function

Parameters:

  • value

    the value to set the attribute normalization_function to.



26
27
28
# File 'lib/network.rb', line 26

def normalization_function=(value)
  @normalization_function = value
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.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/network.rb', line 108

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

#create_layer(neurons:) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/network.rb', line 67

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_sizeObject

Returns the number of input nodes



58
59
60
# File 'lib/network.rb', line 58

def input_size
  @layers[0].size
end

#output_sizeObject

Returns the number of output nodes



63
64
65
# File 'lib/network.rb', line 63

def output_size
  @layers[-1].size
end

#reset_normalization_functionObject



83
84
85
# File 'lib/network.rb', line 83

def reset_normalization_function
  @normalization_function = method(:default_normalization_function)
end

#run(inputs) ⇒ 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.



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/network.rb', line 44

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.map do |output|
    @normalization_function.call(output)
  end
end

#serializeObject

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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/network.rb', line 90

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