Class: Brainz::Neuron

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#activationObject

Returns the value of attribute activation.



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

def activation
  @activation
end

#axon_synapsesObject (readonly)

Returns the value of attribute axon_synapses.



4
5
6
# File 'lib/brainz/neuron.rb', line 4

def axon_synapses
  @axon_synapses
end

#deltaObject

Returns the value of attribute delta.



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

def delta
  @delta
end

#dendritesObject (readonly)

Returns the value of attribute dendrites.



4
5
6
# File 'lib/brainz/neuron.rb', line 4

def dendrites
  @dendrites
end

#layerObject (readonly)

Returns the value of attribute layer.



4
5
6
# File 'lib/brainz/neuron.rb', line 4

def layer
  @layer
end

#output_changeObject

Returns the value of attribute output_change.



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

def output_change
  @output_change
end

#sumObject (readonly)

Returns the value of attribute sum.



4
5
6
# File 'lib/brainz/neuron.rb', line 4

def sum
  @sum
end

Instance Method Details

#activateObject



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_weightsObject



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_rateObject



15
16
17
# File 'lib/brainz/neuron.rb', line 15

def learning_rate
  layer.learning_rate
end

#momentumObject



19
20
21
# File 'lib/brainz/neuron.rb', line 19

def momentum
  layer.momentum
end

#resetObject



23
24
25
# File 'lib/brainz/neuron.rb', line 23

def reset
  @sum = 0
end

#send_signalsObject



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