Class: Neuronet::Neuron

Inherits:
Node
  • Object
show all
Defined in:
lib/neuronet.rb

Overview

A Neuron is a Node with some extra features. It adds two attributes: connections, and bias. The connections attribute is a list of the neuron’s connections to other neurons (or nodes). A neuron’s bias is it’s kicker (or deduction) to it’s activation value, a sum of its connections values.

Instance Attribute Summary collapse

Attributes inherited from Node

#activation

Instance Method Summary collapse

Methods inherited from Node

#value, #value=

Constructor Details

#initialize(bias = 0.0) ⇒ Neuron

Returns a new instance of Neuron.



115
116
117
118
119
# File 'lib/neuronet.rb', line 115

def initialize(bias=0.0)
  super(bias)
  @connections = []
  @bias = bias
end

Instance Attribute Details

#biasObject

Returns the value of attribute bias.



114
115
116
# File 'lib/neuronet.rb', line 114

def bias
  @bias
end

#connectionsObject (readonly)

Returns the value of attribute connections.



113
114
115
# File 'lib/neuronet.rb', line 113

def connections
  @connections
end

Instance Method Details

#backpropagate(error) ⇒ Object

The backpropagate method modifies the neuron’s bias in proportion to the given error and passes on this error to each of its connection’s backpropagate method. While updates flows from input to output, back-propagation of errors flows from output to input.



145
146
147
148
149
150
# File 'lib/neuronet.rb', line 145

def backpropagate(error)
  # Adjusts bias according to error and...
  @bias += error * Neuronet.noise
  # backpropagates the error to the connections.
  @connections.each{|connection| connection.backpropagate(error)}
end

#connect(node, weight = 0.0) ⇒ Object

Connects the neuron to another node. Updates the activation with the new connection. The default weight=0 means there is no initial association. The connect method is how the implementation adds a connection, the way to connect the neuron to another. To connect neuron out to neuron in, for example, it is: in = Neuronet::Neuron.new out = Neuronet::Neuron.new out.connect(in) Think output connects to input.



162
163
164
165
# File 'lib/neuronet.rb', line 162

def connect(node, weight=0.0)
  @connections.push(Connection.new(node,weight))
  update
end

#partialObject

For when connections are already updated, Neuron#partial updates the activation with the current values of bias and connections. It is not always necessary to burrow all the way down to the terminal input node to update the current neuron if it’s connected neurons have all been updated. The implementation should set it’s algorithm to use partial instead of update as update will most likely needlessly update previously updated neurons.



136
137
138
# File 'lib/neuronet.rb', line 136

def partial
  self.value = @bias + @connections.inject(0.0){|sum,connection| sum + connection.value}
end

#updateObject

Updates the activation with the current value of bias and updated values of connections. If you’re not familiar with ruby’s Array::inject method, it is a Ruby way of doing summations. Checkout: [Jay Field’s Thoughts on Ruby: inject](blog.jayfields.com/2008/03/ruby-inject.html) [Induction ( for_all )](carlosjhr64.blogspot.com/2011/02/induction.html)



126
127
128
# File 'lib/neuronet.rb', line 126

def update
  self.value = @bias + @connections.inject(0.0){|sum,connection| sum + connection.update}
end