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, #name

Instance Method Summary collapse

Methods inherited from Layer

#build, #built?, #call, call, from_hash, #output_shape

Constructor Details

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

Returns a new instance of Dropout.

Parameters:

  • dropout_ratio (Float) (defaults to: 0.5)

    Nodes dropout ratio.

  • seed (Integer) (defaults to: rand(1 << 31))

    Seed of random number used for masking.

  • use_scale (Boolean) (defaults to: true)

    Set to true to scale the output according to the dropout ratio.



332
333
334
335
336
337
338
339
# File 'lib/dnn/core/layers.rb', line 332

def initialize(dropout_ratio = 0.5, seed: rand(1 << 31), use_scale: true)
  super()
  @dropout_ratio = dropout_ratio
  @seed = seed
  @use_scale = use_scale
  @mask = nil
  @rnd = Random.new(@seed)
end

Instance Attribute Details

#dropout_ratioObject

Returns the value of attribute dropout_ratio.



326
327
328
# File 'lib/dnn/core/layers.rb', line 326

def dropout_ratio
  @dropout_ratio
end

#use_scaleObject (readonly)

Returns the value of attribute use_scale.



327
328
329
# File 'lib/dnn/core/layers.rb', line 327

def use_scale
  @use_scale
end

Instance Method Details

#backward(dy) ⇒ Object



352
353
354
355
# File 'lib/dnn/core/layers.rb', line 352

def backward(dy)
  dy[@mask] = 0
  dy
end

#forward(x) ⇒ Object



341
342
343
344
345
346
347
348
349
350
# File 'lib/dnn/core/layers.rb', line 341

def forward(x)
  if DNN.learning_phase
    Xumo::SFloat.srand(@rnd.rand(1 << 31))
    @mask = Xumo::SFloat.new(*x.shape).rand < @dropout_ratio
    x[@mask] = 0
  elsif @use_scale
    x *= (1 - @dropout_ratio)
  end
  x
end

#load_hash(hash) ⇒ Object



361
362
363
# File 'lib/dnn/core/layers.rb', line 361

def load_hash(hash)
  initialize(hash[:dropout_ratio], seed: hash[:seed], use_scale: hash[:use_scale])
end

#to_hashObject



357
358
359
# File 'lib/dnn/core/layers.rb', line 357

def to_hash
  super(dropout_ratio: @dropout_ratio, seed: @seed, use_scale: @use_scale)
end