Class: NeuralNetwork::Neuron
- Inherits:
-
Object
- Object
- NeuralNetwork::Neuron
- Defined in:
- lib/neural_network/neuron.rb
Direct Known Subclasses
Constant Summary collapse
- LEARNING_RATE =
make this a variable in the future to be configurable
0.3
Instance Attribute Summary collapse
-
#delta ⇒ Object
Returns the value of attribute delta.
-
#incoming ⇒ Object
Returns the value of attribute incoming.
-
#input ⇒ Object
Returns the value of attribute input.
-
#outgoing ⇒ Object
Returns the value of attribute outgoing.
-
#output ⇒ Object
Returns the value of attribute output.
Instance Method Summary collapse
- #activate(value = nil) ⇒ Object
- #bias? ⇒ Boolean
- #connect(target) ⇒ Object
-
#initialize ⇒ Neuron
constructor
A new instance of Neuron.
- #input? ⇒ Boolean
- #train(target_output = nil) ⇒ Object
Constructor Details
#initialize ⇒ Neuron
Returns a new instance of Neuron.
9 10 11 12 |
# File 'lib/neural_network/neuron.rb', line 9 def initialize @incoming = [] @outgoing = [] end |
Instance Attribute Details
#delta ⇒ Object
Returns the value of attribute delta.
3 4 5 |
# File 'lib/neural_network/neuron.rb', line 3 def delta @delta end |
#incoming ⇒ Object
Returns the value of attribute incoming.
3 4 5 |
# File 'lib/neural_network/neuron.rb', line 3 def incoming @incoming end |
#input ⇒ Object
Returns the value of attribute input.
3 4 5 |
# File 'lib/neural_network/neuron.rb', line 3 def input @input end |
#outgoing ⇒ Object
Returns the value of attribute outgoing.
3 4 5 |
# File 'lib/neural_network/neuron.rb', line 3 def outgoing @outgoing end |
#output ⇒ Object
Returns the value of attribute output.
3 4 5 |
# File 'lib/neural_network/neuron.rb', line 3 def output @output end |
Instance Method Details
#activate(value = nil) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/neural_network/neuron.rb', line 14 def activate(value = nil) return @output = 1 if bias? @input = value || incoming.reduce(0) do |sum, connection| sum + connection.source.output * connection.weight end @output = activation_function(input) end |
#bias? ⇒ Boolean
49 50 51 |
# File 'lib/neural_network/neuron.rb', line 49 def bias? false end |
#connect(target) ⇒ Object
24 25 26 27 28 |
# File 'lib/neural_network/neuron.rb', line 24 def connect(target) connection = Connection.new(self, target) outgoing << connection target.incoming << connection end |
#input? ⇒ Boolean
30 31 32 |
# File 'lib/neural_network/neuron.rb', line 30 def input? incoming.empty? end |
#train(target_output = nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/neural_network/neuron.rb', line 34 def train(target_output = nil) if !bias? && !input? if output? # this is the derivative of the error function # not simply difference in output # http://whiteboard.ping.se/MachineLearning/BackProp @delta = @output - target_output else calculate_outgoing_delta end end update_weights end |