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.



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

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.



421
422
423
# File 'lib/dnn/core/layers/basic_layers.rb', line 421

def dropout_ratio
  @dropout_ratio
end

#use_scaleObject (readonly)

Returns the value of attribute use_scale.



422
423
424
# File 'lib/dnn/core/layers/basic_layers.rb', line 422

def use_scale
  @use_scale
end

Instance Method Details

#backward_node(dy) ⇒ Object



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

def backward_node(dy)
  dy * @mask
end

#forward_node(x) ⇒ Object



436
437
438
439
440
441
442
443
444
445
# File 'lib/dnn/core/layers/basic_layers.rb', line 436

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

#load_hash(hash) ⇒ Object



455
456
457
# File 'lib/dnn/core/layers/basic_layers.rb', line 455

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

#to_hashObject



451
452
453
# File 'lib/dnn/core/layers/basic_layers.rb', line 451

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