Class: Ai::Neat::Layer

Inherits:
Object
  • Object
show all
Defined in:
lib/ai/neat/layer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node_count, node_type, activationfunc) ⇒ Layer

Returns a new instance of Layer.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ai/neat/layer.rb', line 8

def initialize(node_count, node_type, activationfunc)
  @nodes = []
  @bias = nil
  @node_type = node_type
  @activationfunc = activationfunc

  (1..node_count).each do |_i|
    @nodes.push(Node.new)
  end

  @bias = Node.new if @node_type != :output
end

Instance Attribute Details

#activationfuncObject

Returns the value of attribute activationfunc.



6
7
8
# File 'lib/ai/neat/layer.rb', line 6

def activationfunc
  @activationfunc
end

#biasObject

Returns the value of attribute bias.



6
7
8
# File 'lib/ai/neat/layer.rb', line 6

def bias
  @bias
end

#node_typeObject

Returns the value of attribute node_type.



6
7
8
# File 'lib/ai/neat/layer.rb', line 6

def node_type
  @node_type
end

#nodesObject

Returns the value of attribute nodes.



6
7
8
# File 'lib/ai/neat/layer.rb', line 6

def nodes
  @nodes
end

Instance Method Details

#connect(count) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/ai/neat/layer.rb', line 21

def connect(count)
  @nodes.each do |node|
    node.initWeights(count)
  end

  @bias&.initWeights(count)
end

#feed_forward(layer) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ai/neat/layer.rb', line 29

def feed_forward(layer)
  (0..(@bias.weights.count - 1)).each do |i|
    layer.nodes[i].value = 0
  end

  @nodes.each do |node|
    (0..(node.weights.count - 1)).each do |w|
      layer.nodes[w].value += node.value * node.weights[w]
    end
  end

  (0..(@bias.weights.count - 1)).each do |w|
    layer.nodes[w].value += @bias.weights[w]
  end

  if layer.activationfunc != :softmax
    layer.nodes.each do |node|
      node.value = Ai::Neat.activationfunc(layer.activationfunc, node.value)
    end
  else
    layer.values = Ai::Neat.activationfunc(layer.activationfunc, layer.values)
  end
end

#valuesObject



53
54
55
56
57
58
59
60
61
# File 'lib/ai/neat/layer.rb', line 53

def values
  result = []

  @nodes.each do |node|
    result.push(node.value)
  end

  result
end

#values=(values) ⇒ Object



63
64
65
66
67
# File 'lib/ai/neat/layer.rb', line 63

def values=(values)
  (0..(@nodes.count - 1)).each do |i|
    @nodes[i].value = values[i]
  end
end