Class: DNN::Layers::Dense

Inherits:
Connection show all
Includes:
LayerNode
Defined in:
lib/dnn/core/layers/basic_layers.rb

Instance Attribute Summary collapse

Attributes inherited from Connection

#bias, #bias_initializer, #bias_regularizer, #weight, #weight_initializer, #weight_regularizer

Attributes inherited from TrainableLayer

#trainable

Attributes inherited from Layer

#input_shape, #output_shape

Instance Method Summary collapse

Methods included from LayerNode

#forward

Methods inherited from Connection

#get_params, #regularizers, #use_bias

Methods inherited from TrainableLayer

#clean, #get_params

Methods inherited from Layer

#<<, #built?, #call, call, #clean, #forward, from_hash

Constructor Details

#initialize(num_units, weight_initializer: Initializers::RandomNormal.new, bias_initializer: Initializers::Zeros.new, weight_regularizer: nil, bias_regularizer: nil, use_bias: true) ⇒ Dense

Returns a new instance of Dense.

Parameters:

  • num_units (Integer)

    Number of nodes.



240
241
242
243
244
245
246
247
248
249
# File 'lib/dnn/core/layers/basic_layers.rb', line 240

def initialize(num_units,
               weight_initializer: Initializers::RandomNormal.new,
               bias_initializer: Initializers::Zeros.new,
               weight_regularizer: nil,
               bias_regularizer: nil,
               use_bias: true)
  super(weight_initializer: weight_initializer, bias_initializer: bias_initializer,
        weight_regularizer: weight_regularizer, bias_regularizer: bias_regularizer, use_bias: use_bias)
  @num_units = num_units
end

Instance Attribute Details

#num_unitsObject (readonly)

Returns the value of attribute num_units.



237
238
239
# File 'lib/dnn/core/layers/basic_layers.rb', line 237

def num_units
  @num_units
end

Instance Method Details

#backward_node(dy) ⇒ Object



269
270
271
272
273
274
275
# File 'lib/dnn/core/layers/basic_layers.rb', line 269

def backward_node(dy)
  if @trainable
    @weight.grad += @x.transpose.dot(dy)
    @bias.grad += dy.sum(0) if @bias
  end
  dy.dot(@weight.data.transpose)
end

#build(input_shape) ⇒ Object



251
252
253
254
255
256
257
258
259
260
# File 'lib/dnn/core/layers/basic_layers.rb', line 251

def build(input_shape)
  unless input_shape.length == 1
    raise DNNShapeError, "Input shape is #{input_shape}. But input shape must be 1 dimensional."
  end
  super
  num_prev_units = input_shape[0]
  @weight.data = Xumo::SFloat.new(num_prev_units, @num_units)
  @bias.data = Xumo::SFloat.new(@num_units) if @bias
  init_weight_and_bias
end

#compute_output_shapeObject



277
278
279
# File 'lib/dnn/core/layers/basic_layers.rb', line 277

def compute_output_shape
  [@num_units]
end

#forward_node(x) ⇒ Object



262
263
264
265
266
267
# File 'lib/dnn/core/layers/basic_layers.rb', line 262

def forward_node(x)
  @x = x
  y = x.dot(@weight.data)
  y += @bias.data if @bias
  y
end

#load_hash(hash) ⇒ Object



285
286
287
288
289
290
291
292
# File 'lib/dnn/core/layers/basic_layers.rb', line 285

def load_hash(hash)
  initialize(hash[:num_units],
             weight_initializer: Initializers::Initializer.from_hash(hash[:weight_initializer]),
             bias_initializer: Initializers::Initializer.from_hash(hash[:bias_initializer]),
             weight_regularizer: Regularizers::Regularizer.from_hash(hash[:weight_regularizer]),
             bias_regularizer: Regularizers::Regularizer.from_hash(hash[:bias_regularizer]),
             use_bias: hash[:use_bias])
end

#to_hashObject



281
282
283
# File 'lib/dnn/core/layers/basic_layers.rb', line 281

def to_hash
  super(num_units: @num_units)
end