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.



32
33
34
35
36
37
38
39
40
41
# File 'lib/network.rb', line 32

def initialize
  @layers = []
  @inputs = []

  @hidden_layer_normalization_function = method(:default_hidden_layer_normalization_function)
  @output_normalization_function = method(:default_output_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.



29
30
31
# File 'lib/network.rb', line 29

def edge_initialization_function
  @edge_initialization_function
end

#hidden_layer_normalization_functionObject

Returns the value of attribute hidden_layer_normalization_function.



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

def hidden_layer_normalization_function
  @hidden_layer_normalization_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.



30
31
32
# File 'lib/network.rb', line 30

def neuron_bias_initialization_function
  @neuron_bias_initialization_function
end

#output_normalization_functionObject

Returns the value of attribute output_normalization_function.



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

def output_normalization_function
  @output_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.



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
146
147
# File 'lib/network.rb', line 120

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_cachesObject



93
94
95
96
97
# File 'lib/network.rb', line 93

def clear_edge_caches
  @layers.each do |layer|
    layer.clear_edge_cache
  end
end

#create_layer(neurons:) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/network.rb', line 72

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



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

def input_size
  @layers[0].size
end

#output_sizeObject

Returns the number of output nodes



68
69
70
# File 'lib/network.rb', line 68

def output_size
  @layers[-1].size
end

#reset_normalization_functionsObject



88
89
90
91
# File 'lib/network.rb', line 88

def reset_normalization_functions
  @output_normalization_function = method(:default_output_normalization_function)
  @hidden_layer_normalization_function = method(:default_hidden_layer_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



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/network.rb', line 49

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(normalize: output_normalization_function)
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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/network.rb', line 102

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