Class: Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sigma) ⇒ Node

Returns a new instance of Node.



4
5
6
7
8
9
10
# File 'lib/node.rb', line 4

def initialize sigma
  @input_value = 0
  @forward_nodes = {}
  @weights =[]
  @bias = 0
  @sigma = sigma
end

Instance Attribute Details

#biasObject

Returns the value of attribute bias.



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

def bias
  @bias
end

#forward_nodesObject (readonly)

Returns the value of attribute forward_nodes.



2
3
4
# File 'lib/node.rb', line 2

def forward_nodes
  @forward_nodes
end

#input_valueObject

Returns the value of attribute input_value.



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

def input_value
  @input_value
end

#output_valueObject (readonly)

Returns the value of attribute output_value.



2
3
4
# File 'lib/node.rb', line 2

def output_value
  @output_value
end

#sigmaObject

Returns the value of attribute sigma.



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

def sigma
  @sigma
end

#weightsObject

Returns the value of attribute weights.



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

def weights
  @weights
end

Instance Method Details

#attach_forward_node(node, sequence) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/node.rb', line 25

def attach_forward_node node, sequence
  if @weights.count <= sequence
    @weights << ((rand * 0.4 ) - 0.2) #sampled from a uniform distribution in range ± 0.2
  end
  
  @forward_nodes[node] = @weights[sequence]
end

#cloneObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/node.rb', line 57

def clone
  clone = Node.new(self.sigma)
  
  clone.sigma = self.sigma
  clone.bias = self.bias
  
  @weights.each do |weight|
    clone.weights << weight
  end
  
  #being pure we clone the forward node refs as well
  #although in practice these are about to be updated with new nodes
  self.forward_nodes.each do |key,value| 
    clone.forward_nodes[key] = value
  end
  
  clone
end

#detatch_all_forward_nodesObject



33
34
35
# File 'lib/node.rb', line 33

def detatch_all_forward_nodes
  @forward_nodes = {}
end

#evaluateObject

take our input value (sum of weighted outputs of backward connected nodes) , subtract the bias and pass the result through our sigmoid function (tanh) finally then pass on our output value to each of our forward nodes



14
15
16
17
18
19
20
21
22
23
# File 'lib/node.rb', line 14

def evaluate
  @output_value = Math.tanh(@input_value - @bias)
  #p "output value #{@output_value}"
  @forward_nodes.each do |node, weight|
    #p "weight #{weight} old input #{node.input_value}"
    node.input_value += @output_value * weight
    #p "new input #{node.input_value}"
  end
  @input_value = 0
end

#fingerprintObject



76
77
78
79
80
81
82
# File 'lib/node.rb', line 76

def fingerprint
  fingerprint = "#{@sigma.to_s}:#{@bias.to_s}:"
  @weights.each do |weight|
    fingerprint += "#{weight.to_s},"
  end
  fingerprint = fingerprint.chop + "\n"
end

#mutateObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/node.rb', line 37

def mutate
  new_weights = []
  @weights.each do |weight|
    new_weights << weight + @sigma * gaussian_random # new_random_number = average + standard_deviation  * gaussian_rand
  end
  @weights = new_weights
  if @forward_values
    i = 0
    @forward_values.each do |key,value|
      @forward_values[key] = @weights[i]
      i += 1
    end
  end
  
  #mutate the bias
  @bias = @bias + gaussian_random * @sigma

  self
end

#reload(fingerprint) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/node.rb', line 84

def reload fingerprint
  self.detatch_all_forward_nodes
  @weights = []
  self.sigma = fingerprint.split(':')[0].to_f
  self.bias = fingerprint.split(':')[1].to_f
  if fingerprint.split(":").count == 3
    fingerprint.split(":")[2].split(',').each do |weight|
      @weights << weight.to_f
    end
  end
end