Class: DNN::Layers::Sum

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

Instance Attribute Summary collapse

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

Constructor Details

#initialize(axis: 0, keepdims: true) ⇒ Sum

Returns a new instance of Sum.



200
201
202
203
204
# File 'lib/dnn/core/layers/math_layers.rb', line 200

def initialize(axis: 0, keepdims: true)
  super()
  @axis = axis
  @keepdims = keepdims
end

Instance Attribute Details

#axisObject (readonly)

Returns the value of attribute axis.



197
198
199
# File 'lib/dnn/core/layers/math_layers.rb', line 197

def axis
  @axis
end

#keepdimsObject (readonly)

Returns the value of attribute keepdims.



198
199
200
# File 'lib/dnn/core/layers/math_layers.rb', line 198

def keepdims
  @keepdims
end

Instance Method Details

#backward_node(dy) ⇒ Object



215
216
217
# File 'lib/dnn/core/layers/math_layers.rb', line 215

def backward_node(dy)
  MathUtils.broadcast_to(dy, @x_shape)
end

#forward_node(x) ⇒ Object



206
207
208
209
210
211
212
213
# File 'lib/dnn/core/layers/math_layers.rb', line 206

def forward_node(x)
  @x_shape = x.shape
  if @axis
    x.sum(axis: @axis, keepdims: true)
  else
    x.sum
  end
end

#load_hash(hash) ⇒ Object



223
224
225
# File 'lib/dnn/core/layers/math_layers.rb', line 223

def load_hash(hash)
  initialize(axis: hash[:axis], keepdims: hash[:keepdims])
end

#to_hashObject



219
220
221
# File 'lib/dnn/core/layers/math_layers.rb', line 219

def to_hash
  super(axis: @axis, keepdims: @keepdims)
end