Class: NeuralNetwork::Layer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ Layer

Returns a new instance of Layer.



5
6
7
# File 'lib/neural_network/layer.rb', line 5

def initialize(size)
  @neurons = Array.new(size) { Neuron.new }
end

Instance Attribute Details

#neuronsObject

Returns the value of attribute neurons.



3
4
5
# File 'lib/neural_network/layer.rb', line 3

def neurons
  @neurons
end

Instance Method Details

#activate(values = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/neural_network/layer.rb', line 21

def activate(values = nil)
  values = Array(values) # coerce it to an array if nil

  @neurons.each_with_index do |neuron, index|
    neuron.activate(values[index])
  end

  # optional: return mapping of neuron outputs
  @neurons.map { |n| n.output }
end

#connect(target_layer) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/neural_network/layer.rb', line 9

def connect(target_layer)
  unless @neurons.any? {|neuron| neuron.bias? }
    @neurons << NeuralNetwork::BiasNeuron.new
  end

  @neurons.each do |source_neuron|
    target_layer.neurons.each do |target_neuron|
      source_neuron.connect(target_neuron)
    end
  end
end

#train(target_outputs = []) ⇒ Object



33
34
35
36
37
# File 'lib/neural_network/layer.rb', line 33

def train(target_outputs = [])
  @neurons.each_with_index do |neuron, index|
    neuron.train(target_outputs[index])
  end
end