Class: Chainer::Functions::Noise::Dropout
- Inherits:
-
Chainer::Function
- Object
- Chainer::Function
- Chainer::Functions::Noise::Dropout
- Defined in:
- lib/chainer/functions/noise/dropout.rb
Instance Attribute Summary
Attributes inherited from Chainer::Function
#inputs, #output_data, #outputs, #rank, #retain_after_backward
Class Method Summary collapse
-
.dropout(x, ratio: 0.5) ⇒ Chainer::Variable
Drops elements of input variable randomly.
Instance Method Summary collapse
- #backward(x, gy) ⇒ Object
- #forward(x) ⇒ Object
-
#initialize(dropout_ratio) ⇒ Dropout
constructor
A new instance of Dropout.
Methods inherited from Chainer::Function
#call, #forward_cpu, #retain_inputs, #retain_outputs
Constructor Details
#initialize(dropout_ratio) ⇒ Dropout
Returns a new instance of Dropout.
18 19 20 21 22 23 |
# File 'lib/chainer/functions/noise/dropout.rb', line 18 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 |
Class Method Details
.dropout(x, ratio: 0.5) ⇒ Chainer::Variable
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`.
14 15 16 |
# File 'lib/chainer/functions/noise/dropout.rb', line 14 def self.dropout(x, ratio: 0.5) Chainer.configuration.train ? self.new(ratio).(x) : x end |
Instance Method Details
#backward(x, gy) ⇒ Object
38 39 40 |
# File 'lib/chainer/functions/noise/dropout.rb', line 38 def backward(x, gy) [gy[0] * @mask] end |
#forward(x) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/chainer/functions/noise/dropout.rb', line 25 def forward(x) retain_inputs([]) 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 |