Class: DNN::Layers::Dropout

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

Instance Attribute Summary collapse

Attributes inherited from Layer

#input_shape

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Layer

#build, #built?, #output_shape

Constructor Details

#initialize(dropout_ratio = 0.5, seed = rand(1 << 31)) ⇒ Dropout

Returns a new instance of Dropout.



275
276
277
278
279
280
# File 'lib/dnn/core/layers.rb', line 275

def initialize(dropout_ratio = 0.5, seed = rand(1 << 31))
  super()
  @dropout_ratio = dropout_ratio
  @seed = seed
  @mask = nil
end

Instance Attribute Details

#dropout_ratioObject (readonly)

Returns the value of attribute dropout_ratio.



269
270
271
# File 'lib/dnn/core/layers.rb', line 269

def dropout_ratio
  @dropout_ratio
end

Class Method Details

.load_hash(hash) ⇒ Object



271
272
273
# File 'lib/dnn/core/layers.rb', line 271

def self.load_hash(hash)
  self.new(hash[:dropout_ratio], hash[:seed])
end

Instance Method Details

#backward(dout, learning_phase) ⇒ Object



293
294
295
296
# File 'lib/dnn/core/layers.rb', line 293

def backward(dout, learning_phase)
  dout[@mask] = 0 if learning_phase
  dout
end

#forward(x, learning_phase) ⇒ Object



282
283
284
285
286
287
288
289
290
291
# File 'lib/dnn/core/layers.rb', line 282

def forward(x, learning_phase)
  if learning_phase
    Xumo::SFloat.srand(@seed)
    @mask = Xumo::SFloat.ones(*x.shape).rand < @dropout_ratio
    x[@mask] = 0
  else
    x *= (1 - @dropout_ratio)
  end
  x
end

#to_hashObject



298
299
300
# File 'lib/dnn/core/layers.rb', line 298

def to_hash
  super({dropout_ratio: @dropout_ratio, seed: @seed})
end