Class: DNN::Layers::Dropout

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

Instance Attribute Summary collapse

Attributes inherited from Layer

#input_shape, #output_shape

Instance Method Summary collapse

Methods included from LayerNode

#forward

Methods inherited from Layer

#build, #built?, #call, call, #clean, #compute_output_shape, #forward, from_hash

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.



417
418
419
420
421
422
423
424
# File 'lib/dnn/core/layers/basic_layers.rb', line 417

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.



411
412
413
# File 'lib/dnn/core/layers/basic_layers.rb', line 411

def dropout_ratio
  @dropout_ratio
end

#use_scaleObject (readonly)

Returns the value of attribute use_scale.



412
413
414
# File 'lib/dnn/core/layers/basic_layers.rb', line 412

def use_scale
  @use_scale
end

Instance Method Details

#backward_node(dy) ⇒ Object



437
438
439
440
# File 'lib/dnn/core/layers/basic_layers.rb', line 437

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

#forward_node(x) ⇒ Object



426
427
428
429
430
431
432
433
434
435
# File 'lib/dnn/core/layers/basic_layers.rb', line 426

def forward_node(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



446
447
448
# File 'lib/dnn/core/layers/basic_layers.rb', line 446

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

#to_hashObject



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

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