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, #built?, #prev_layer, #shape

Constructor Details

#initialize(dropout_ratio) ⇒ Dropout

Returns a new instance of Dropout.



479
480
481
482
483
# File 'lib/dnn/core/layers.rb', line 479

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

Class Method Details

.load(hash) ⇒ Object



489
490
491
# File 'lib/dnn/core/layers.rb', line 489

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

.load_hash(hash) ⇒ Object



485
486
487
# File 'lib/dnn/core/layers.rb', line 485

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

Instance Method Details

#backward(dout) ⇒ Object



503
504
505
506
# File 'lib/dnn/core/layers.rb', line 503

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

#forward(x) ⇒ Object



493
494
495
496
497
498
499
500
501
# File 'lib/dnn/core/layers.rb', line 493

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

#to_hash ⇒ Object



508
509
510
# File 'lib/dnn/core/layers.rb', line 508

def to_hash
  {name: self.class.name, dropout_ratio: @dropout_ratio}
end