class Chainer::Functions::Noise::Dropout

Attributes

mask[R]

Public Class Methods

dropout(x, ratio: 0.5) click to toggle source

Drops elements of input variable randomly.

This function drops input elements randomly with probability `ratio` and scales the remaining elements by factor `1 / (1 - ratio)`. In testing mode, it does nothing and just returns `x`.

@param [Chainer::Variable] x Input variable. @param [float] ratio Dropout ratio. The “ratio“ must be `0.0 <= ratio < 1.0`. @return [Chainer::Variable] Output variable.

# File lib/chainer/functions/noise/dropout.rb, line 15
def self.dropout(x, ratio: 0.5)
  Chainer.configuration.train ? self.new(ratio).apply([x])[0] : Chainer::Variable.as_variable(x)
end
new(dropout_ratio) click to toggle source
# File lib/chainer/functions/noise/dropout.rb, line 19
def initialize(dropout_ratio)
  if dropout_ratio < 0 || dropout_ratio >= 1.0
    raise 'dropout_ratio must be in the range [0, 1)'
  end
  @dropout_ratio = dropout_ratio
end

Public Instance Methods

backward(x, gy) click to toggle source
# File lib/chainer/functions/noise/dropout.rb, line 38
def backward(x, gy)
  DropoutGrad.new(@mask).apply(gy)
end
forward(x) click to toggle source
# File lib/chainer/functions/noise/dropout.rb, line 26
def forward(x)
  unless self.instance_variable_defined?(:@mask)
    scale = x[0].class[*[1.0 / (1 - @dropout_ratio)]][0]
    flag = x[0].class.new(*x[0].shape).rand >= @dropout_ratio

    @mask = x[0].class.zeros(*x[0].shape)
    @mask[flag] = 1
    @mask *= scale
  end
  [x[0] * @mask]
end