Class: DNN::Layers::Dropout

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

Instance Attribute Summary collapse

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.



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

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

Instance Attribute Details

#dropoit_ratioObject (readonly)

Returns the value of attribute dropoit_ratio.



478
479
480
# File 'lib/dnn/core/layers.rb', line 478

def dropoit_ratio
  @dropoit_ratio
end

Class Method Details

.load(hash) ⇒ Object



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

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

.load_hash(hash) ⇒ Object



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

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

Instance Method Details

#backward(dout) ⇒ Object



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

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

#forward(x) ⇒ Object



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

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_hashObject



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

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