Class: DNN::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/dnn/core/link.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prevs: nil, layer_node: nil, num_outputs: 1) ⇒ Link

Returns a new instance of Link.



8
9
10
11
12
13
14
# File 'lib/dnn/core/link.rb', line 8

def initialize(prevs: nil, layer_node: nil, num_outputs: 1)
  @prevs = prevs
  @layer_node = layer_node
  @num_outputs = num_outputs
  @next = nil
  @hold = []
end

Instance Attribute Details

#layer_nodeObject

Returns the value of attribute layer_node.



5
6
7
# File 'lib/dnn/core/link.rb', line 5

def layer_node
  @layer_node
end

#nextObject

Returns the value of attribute next.



4
5
6
# File 'lib/dnn/core/link.rb', line 4

def next
  @next
end

#num_outputsObject (readonly)

Returns the value of attribute num_outputs.



6
7
8
# File 'lib/dnn/core/link.rb', line 6

def num_outputs
  @num_outputs
end

#prevsObject

Returns the value of attribute prevs.



3
4
5
# File 'lib/dnn/core/link.rb', line 3

def prevs
  @prevs
end

Instance Method Details

#backward(dy = ) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dnn/core/link.rb', line 24

def backward(dy = Xumo::SFloat[1])
  @hold << dy
  return if @hold.length < @num_outputs
  dys = @layer_node.backward_node(*@hold)
  @hold = []
  if dys.is_a?(Array)
    dys.each.with_index do |dy, i|
      @prevs[i]&.backward(dy)
    end
  else
    @prevs.first&.backward(dys)
  end
end

#forward(x) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/dnn/core/link.rb', line 16

def forward(x)
  @hold << x
  return if @hold.length < @prevs.length
  x = @layer_node.(*@hold)
  @hold = []
  @next ? @next.forward(x) : x
end