Class: DNN::Layers::Sum

Inherits:
Layer
  • Object
show all
Includes:
LayerNode
Defined in:
lib/dnn/core/layers/math_layers.rb

Instance Attribute Summary

Attributes inherited from Layer

#input_shape, #output_shape

Instance Method Summary collapse

Methods included from LayerNode

#forward

Methods inherited from Layer

#build, #built?, #call, call, #clean, #compute_output_shape, #forward, from_hash, #load_hash, #to_hash

Constructor Details

#initialize(axis: 0) ⇒ Sum

Returns a new instance of Sum.



140
141
142
143
# File 'lib/dnn/core/layers/math_layers.rb', line 140

def initialize(axis: 0)
  super()
  @axis = axis
end

Instance Method Details

#backward_node(dy) ⇒ Object



151
152
153
154
155
156
157
158
# File 'lib/dnn/core/layers/math_layers.rb', line 151

def backward_node(dy)
  return dy if @x_shape == dy.shape
  dx = dy
  (@dim - 1).times do
    dx = dx.concatenate(dy, axis: @axis)
  end
  dx
end

#forward_node(x) ⇒ Object



145
146
147
148
149
# File 'lib/dnn/core/layers/math_layers.rb', line 145

def forward_node(x)
  @x_shape = x.shape
  @dim = x.shape[@axis]
  x.sum(axis: @axis, keepdims: true)
end