Class: NeuralNetwork::Neuron

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

Direct Known Subclasses

BiasNeuron

Constant Summary collapse

LEARNING_RATE =

make this a variable in the future to be configurable

0.3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNeuron

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

#deltaObject

Returns the value of attribute delta.



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

def delta
  @delta
end

#incomingObject

Returns the value of attribute incoming.



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

def incoming
  @incoming
end

#inputObject

Returns the value of attribute input.



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

def input
  @input
end

#outgoingObject

Returns the value of attribute outgoing.



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

def outgoing
  @outgoing
end

#outputObject

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

Returns:

  • (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

Returns:

  • (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