Class: Brainz::Layer
- Inherits:
-
Object
- Object
- Brainz::Layer
- Defined in:
- lib/brainz/layer.rb
Instance Attribute Summary collapse
-
#mse ⇒ Object
Returns the value of attribute mse.
-
#network ⇒ Object
readonly
Returns the value of attribute network.
-
#neurons ⇒ Object
readonly
Returns the value of attribute neurons.
-
#next_layer ⇒ Object
readonly
Returns the value of attribute next_layer.
-
#prev_layer ⇒ Object
readonly
Returns the value of attribute prev_layer.
Instance Method Summary collapse
- #activate ⇒ Object
- #adjust_weights ⇒ Object
- #back_link(layer) ⇒ Object
- #calculate_deltas ⇒ Object
- #calculate_mse(targets) ⇒ Object
-
#initialize(size, network) ⇒ Layer
constructor
A new instance of Layer.
- #learning_rate ⇒ Object
- #link_to(layer) ⇒ Object
- #momentum ⇒ Object
- #reset ⇒ Object
- #update(*values) ⇒ Object
- #update_forward ⇒ Object
Constructor Details
Instance Attribute Details
#mse ⇒ Object
Returns the value of attribute mse.
4 5 6 |
# File 'lib/brainz/layer.rb', line 4 def mse @mse end |
#network ⇒ Object (readonly)
Returns the value of attribute network.
3 4 5 |
# File 'lib/brainz/layer.rb', line 3 def network @network end |
#neurons ⇒ Object (readonly)
Returns the value of attribute neurons.
3 4 5 |
# File 'lib/brainz/layer.rb', line 3 def neurons @neurons end |
#next_layer ⇒ Object (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_layer ⇒ Object (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
#activate ⇒ Object
44 45 46 |
# File 'lib/brainz/layer.rb', line 44 def activate neurons.each(&:activate) end |
#adjust_weights ⇒ Object
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 |
#back_link(layer) ⇒ Object
31 32 33 |
# File 'lib/brainz/layer.rb', line 31 def back_link(layer) @prev_layer = layer end |
#calculate_deltas ⇒ Object
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_rate ⇒ Object
16 17 18 |
# File 'lib/brainz/layer.rb', line 16 def learning_rate network.learning_rate end |
#link_to(layer) ⇒ Object
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 |
#momentum ⇒ Object
12 13 14 |
# File 'lib/brainz/layer.rb', line 12 def momentum network.momentum end |
#reset ⇒ Object
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_forward ⇒ Object
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 |