Class: Brainz::Layer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size, network) ⇒ Layer

Returns a new instance of Layer.



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

def initialize(size, network)
  @network = network
  @neurons = Array.new(size) { Neuron.new(self) }
  @mse = 0
end

Instance Attribute Details

#mseObject

Returns the value of attribute mse.



4
5
6
# File 'lib/brainz/layer.rb', line 4

def mse
  @mse
end

#networkObject (readonly)

Returns the value of attribute network.



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

def network
  @network
end

#neuronsObject (readonly)

Returns the value of attribute neurons.



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

def neurons
  @neurons
end

#next_layerObject (readonly)

Returns the value of attribute next_layer.



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

def next_layer
  @next_layer
end

#prev_layerObject (readonly)

Returns the value of attribute prev_layer.



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

def prev_layer
  @prev_layer
end

Instance Method Details

#activateObject



44
45
46
# File 'lib/brainz/layer.rb', line 44

def activate
  neurons.each(&:activate)
end

#adjust_weightsObject



65
66
67
68
# File 'lib/brainz/layer.rb', line 65

def adjust_weights
  neurons.each(&:adjust_weights)
  prev_layer.adjust_weights if prev_layer
end


31
32
33
# File 'lib/brainz/layer.rb', line 31

def back_link(layer)
  @prev_layer = layer
end

#calculate_deltasObject



48
49
50
51
52
53
# File 'lib/brainz/layer.rb', line 48

def calculate_deltas
  if prev_layer
    neurons.each(&:calculate_delta)
    prev_layer.calculate_deltas
  end
end

#calculate_mse(targets) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/brainz/layer.rb', line 70

def calculate_mse(targets)
  mse = 0
  neurons.each_with_index do |neuron, index|
    mse += 0.5 * (targets[index] - neuron.activation) ** 2
  end
  mse
end

#learning_rateObject



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

def learning_rate
  network.learning_rate
end


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

def link_to(layer)
  @next_layer = layer
  neurons.each do |my_neuron|
    layer.neurons.each do |next_neuron|
      ::Brainz::Synapse.link(my_neuron, next_neuron)
    end
  end

  layer.back_link(self)
end

#momentumObject



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

def momentum
  network.momentum
end

#resetObject



55
56
57
# File 'lib/brainz/layer.rb', line 55

def reset
  neurons.each(&:reset)
end

#update(*values) ⇒ Object



59
60
61
62
63
# File 'lib/brainz/layer.rb', line 59

def update(*values)
  values.each_with_index do |value, index|
    neurons[index].output = value
  end
end

#update_forwardObject



35
36
37
38
39
40
41
42
# File 'lib/brainz/layer.rb', line 35

def update_forward
  if next_layer
    next_layer.reset
    neurons.each(&:send_signals)
    next_layer.activate
    next_layer.update_forward
  end
end