Class: DNN::Layers::Dropout

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Layer

#build, #builded?, #prev_layer, #shape, #to_hash

Constructor Details

#initialize(dropout_ratio) ⇒ Dropout

Returns a new instance of Dropout.



423
424
425
426
# File 'lib/dnn/core/layers.rb', line 423

def initialize(dropout_ratio)
  @dropout_ratio = dropout_ratio
  @mask = nil
end

Class Method Details

.load(hash) ⇒ Object



428
429
430
# File 'lib/dnn/core/layers.rb', line 428

def self.load(hash)
  self.new(hash[:dropout_ratio])
end

Instance Method Details

#backward(dout) ⇒ Object



442
443
444
445
# File 'lib/dnn/core/layers.rb', line 442

def backward(dout)
  dout[@mask] = 0 if @model.training
  dout
end

#forward(x) ⇒ Object



432
433
434
435
436
437
438
439
440
# File 'lib/dnn/core/layers.rb', line 432

def forward(x)
  if @model.training
    @mask = SFloat.ones(*x.shape).rand < @dropout_ratio
    x[@mask] = 0
  else
    x *= (1 - @dropout_ratio)
  end
  x
end