Class: DNN::Layers::Connection

Inherits:
TrainableLayer show all
Defined in:
lib/dnn/core/layers/basic_layers.rb

Overview

It is a superclass of all connection layers.

Direct Known Subclasses

Conv2D, Conv2DTranspose, Dense, RNN

Instance Attribute Summary collapse

Attributes inherited from TrainableLayer

#trainable

Attributes inherited from Layer

#input_shape, #output_shape

Instance Method Summary collapse

Methods inherited from TrainableLayer

#clean

Methods inherited from Layer

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

Constructor Details

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

Returns a new instance of Connection.

Parameters:



186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/dnn/core/layers/basic_layers.rb', line 186

def initialize(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
  @weight = Param.new(nil, Xumo::SFloat[0])
  @bias = use_bias ? Param.new(nil, Xumo::SFloat[0]) : nil
end

Instance Attribute Details

#biasObject (readonly)

Returns the value of attribute bias.



175
176
177
# File 'lib/dnn/core/layers/basic_layers.rb', line 175

def bias
  @bias
end

#bias_initializerObject (readonly)

Returns the value of attribute bias_initializer.



177
178
179
# File 'lib/dnn/core/layers/basic_layers.rb', line 177

def bias_initializer
  @bias_initializer
end

#bias_regularizerObject (readonly)

Returns the value of attribute bias_regularizer.



179
180
181
# File 'lib/dnn/core/layers/basic_layers.rb', line 179

def bias_regularizer
  @bias_regularizer
end

#weightObject (readonly)

Returns the value of attribute weight.



174
175
176
# File 'lib/dnn/core/layers/basic_layers.rb', line 174

def weight
  @weight
end

#weight_initializerObject (readonly)

Returns the value of attribute weight_initializer.



176
177
178
# File 'lib/dnn/core/layers/basic_layers.rb', line 176

def weight_initializer
  @weight_initializer
end

#weight_regularizerObject (readonly)

Returns the value of attribute weight_regularizer.



178
179
180
# File 'lib/dnn/core/layers/basic_layers.rb', line 178

def weight_regularizer
  @weight_regularizer
end

Instance Method Details

#get_paramsObject



220
221
222
# File 'lib/dnn/core/layers/basic_layers.rb', line 220

def get_params
  { weight: @weight, bias: @bias }
end

#regularizersObject



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

def regularizers
  regularizers = []
  regularizers << @weight_regularizer if @weight_regularizer
  regularizers << @bias_regularizer if @bias_regularizer
  regularizers
end

#to_hash(merge_hash) ⇒ Object



212
213
214
215
216
217
218
# File 'lib/dnn/core/layers/basic_layers.rb', line 212

def to_hash(merge_hash)
  super({ weight_initializer: @weight_initializer.to_hash,
          bias_initializer: @bias_initializer.to_hash,
          weight_regularizer: @weight_regularizer&.to_hash,
          bias_regularizer: @bias_regularizer&.to_hash,
          use_bias: use_bias }.merge(merge_hash))
end

#use_biasBoolean

Return whether to use bias.

Returns:

  • (Boolean)

    Return whether to use bias.



208
209
210
# File 'lib/dnn/core/layers/basic_layers.rb', line 208

def use_bias
  @bias ? true : false
end