Class: Brainz::Neuron
- Inherits:
-
Object
- Object
- Brainz::Neuron
- Defined in:
- lib/brainz/neuron.rb
Instance Attribute Summary collapse
-
#activation ⇒ Object
Returns the value of attribute activation.
-
#axon_synapses ⇒ Object
readonly
Returns the value of attribute axon_synapses.
-
#delta ⇒ Object
Returns the value of attribute delta.
-
#dendrites ⇒ Object
readonly
Returns the value of attribute dendrites.
-
#layer ⇒ Object
readonly
Returns the value of attribute layer.
-
#output_change ⇒ Object
Returns the value of attribute output_change.
-
#sum ⇒ Object
readonly
Returns the value of attribute sum.
Instance Method Summary collapse
- #activate ⇒ Object
- #add(value) ⇒ Object
- #adjust_weights ⇒ Object
- #calculate_delta(target = nil) ⇒ Object
- #d_sigmoid(y) ⇒ Object
- #fi(x) ⇒ Object
-
#initialize(layer) ⇒ Neuron
constructor
A new instance of Neuron.
- #learning_rate ⇒ Object
- #momentum ⇒ Object
- #reset ⇒ Object
- #send_signals ⇒ Object
Constructor Details
#initialize(layer) ⇒ Neuron
Returns a new instance of Neuron.
6 7 8 9 10 11 12 13 |
# File 'lib/brainz/neuron.rb', line 6 def initialize(layer) @layer = layer @dendrites = [] @axon_synapses = [] self.output_change = 0 self.activation = 0 reset end |
Instance Attribute Details
#activation ⇒ Object
Returns the value of attribute activation.
3 4 5 |
# File 'lib/brainz/neuron.rb', line 3 def activation @activation end |
#axon_synapses ⇒ Object (readonly)
Returns the value of attribute axon_synapses.
4 5 6 |
# File 'lib/brainz/neuron.rb', line 4 def axon_synapses @axon_synapses end |
#delta ⇒ Object
Returns the value of attribute delta.
3 4 5 |
# File 'lib/brainz/neuron.rb', line 3 def delta @delta end |
#dendrites ⇒ Object (readonly)
Returns the value of attribute dendrites.
4 5 6 |
# File 'lib/brainz/neuron.rb', line 4 def dendrites @dendrites end |
#layer ⇒ Object (readonly)
Returns the value of attribute layer.
4 5 6 |
# File 'lib/brainz/neuron.rb', line 4 def layer @layer end |
#output_change ⇒ Object
Returns the value of attribute output_change.
3 4 5 |
# File 'lib/brainz/neuron.rb', line 3 def output_change @output_change end |
#sum ⇒ Object (readonly)
Returns the value of attribute sum.
4 5 6 |
# File 'lib/brainz/neuron.rb', line 4 def sum @sum end |
Instance Method Details
#activate ⇒ Object
31 32 33 |
# File 'lib/brainz/neuron.rb', line 31 def activate @activation = fi(@sum) end |
#add(value) ⇒ Object
27 28 29 |
# File 'lib/brainz/neuron.rb', line 27 def add(value) @sum += value end |
#adjust_weights ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/brainz/neuron.rb', line 51 def adjust_weights dendrites.each do |synapse| change = delta * synapse.from.activation synapse.adjust(learning_rate * change + momentum * synapse.change) synapse.change = change end end |
#calculate_delta(target = nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/brainz/neuron.rb', line 40 def calculate_delta(target = nil) if target # output layer @delta = d_sigmoid(activation) * (target - activation) else # middle layer error = axon_synapses.collect { |synapse| synapse.to.delta * synapse.weight }.inject(:+) @delta = d_sigmoid(activation) * error end end |
#d_sigmoid(y) ⇒ Object
63 64 65 |
# File 'lib/brainz/neuron.rb', line 63 def d_sigmoid(y) 1.0 - y ** 2 end |
#fi(x) ⇒ Object
59 60 61 |
# File 'lib/brainz/neuron.rb', line 59 def fi(x) Math.tanh(x) end |
#learning_rate ⇒ Object
15 16 17 |
# File 'lib/brainz/neuron.rb', line 15 def learning_rate layer.learning_rate end |
#momentum ⇒ Object
19 20 21 |
# File 'lib/brainz/neuron.rb', line 19 def momentum layer.momentum end |
#reset ⇒ Object
23 24 25 |
# File 'lib/brainz/neuron.rb', line 23 def reset @sum = 0 end |
#send_signals ⇒ Object
35 36 37 38 |
# File 'lib/brainz/neuron.rb', line 35 def send_signals axon_synapses.each { |s| s.to.add(@activation * s.weight) } end |