Class: SimpleNeuralNetwork::Layer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size, network) ⇒ Layer

Returns a new instance of Layer.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/layer.rb', line 17

def initialize(size, network)
  @size = size
  @neurons = []
  @network = network

  @prev_layer = nil
  @next_layer = nil

  populate_neurons
  edge_matrix # Caches edge matrix
end

Instance Attribute Details

#networkObject

Returns the value of attribute network.



15
16
17
# File 'lib/layer.rb', line 15

def network
  @network
end

#neuronsObject

List of ##size neurons



13
14
15
# File 'lib/layer.rb', line 13

def neurons
  @neurons
end

#next_layerObject

Returns the value of attribute next_layer.



10
11
12
# File 'lib/layer.rb', line 10

def next_layer
  @next_layer
end

#prev_layerObject

Returns the value of attribute prev_layer.



9
10
11
# File 'lib/layer.rb', line 9

def prev_layer
  @prev_layer
end

#sizeObject

Number of neurons



7
8
9
# File 'lib/layer.rb', line 7

def size
  @size
end

Instance Method Details

#clear_edge_cacheObject



70
71
72
# File 'lib/layer.rb', line 70

def clear_edge_cache
  @edge_matrix = nil
end

#edge_matrixObject



61
62
63
64
65
66
67
68
# File 'lib/layer.rb', line 61

def edge_matrix
  return unless prev_layer

  @edge_matrix ||= begin
    elements = prev_layer.neurons.map{|a| a.edges}
    NMatrix.new([elements.count, elements[0].count], elements.flatten, dtype: :float64).transpose
  end
end

#get_outputObject

The method that drives network output resolution. get_output calculates the array of neuron values for this layer. This is calculated by recursively fetching the output from the previous layer, then applying edge/node weight and bias rules. The first layer will fetch it’s values from @network.inputs



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/layer.rb', line 33

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_output = prev_layer.get_output
    prev_output_matrix = NMatrix.new([prev_output.length, 1], prev_output, dtype: :float64)

    result = (edge_matrix.dot(prev_output_matrix)).each_with_index.map do |val, i|
      val + @neurons[i].bias
    end
  end
end

#initialize_neuron_edgesObject



53
54
55
56
57
58
59
# File 'lib/layer.rb', line 53

def initialize_neuron_edges
  return unless @next_layer

  @neurons.each do |neuron|
    neuron.initialize_edges(@next_layer.size)
  end
end