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 = 0.5) ⇒ Dropout

Returns a new instance of Dropout.



233
234
235
236
237
# File 'lib/dnn/core/layers.rb', line 233

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

Instance Attribute Details

#dropout_ratio ⇒ Object (readonly)

Returns the value of attribute dropout_ratio.



227
228
229
# File 'lib/dnn/core/layers.rb', line 227

def dropout_ratio
  @dropout_ratio
end

Class Method Details

.load_hash(hash) ⇒ Object



229
230
231
# File 'lib/dnn/core/layers.rb', line 229

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

Instance Method Details

#backward(dout) ⇒ Object



249
250
251
252
# File 'lib/dnn/core/layers.rb', line 249

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

#forward(x) ⇒ Object



239
240
241
242
243
244
245
246
247
# File 'lib/dnn/core/layers.rb', line 239

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



254
255
256
# File 'lib/dnn/core/layers.rb', line 254

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