Class: GRX::NN::Dropout

Inherits:
Module
  • Object
show all
Defined in:
lib/grx/nn.rb

Overview

================================================================

Dropout — Random feature dropout during training mode

Instance Method Summary collapse

Methods inherited from Module

#call, #load_weights, #parameters, #save_weights, #zero_grad

Constructor Details

#initialize(p = 0.5) ⇒ Dropout

Returns a new instance of Dropout.



178
179
180
181
# File 'lib/grx/nn.rb', line 178

def initialize(p = 0.5)
  @p        = p
  @training = true
end

Instance Method Details

#eval!Object



184
# File 'lib/grx/nn.rb', line 184

def eval!;   @training = false; self; end

#forward(x) ⇒ Object



186
187
188
189
190
191
192
193
# File 'lib/grx/nn.rb', line 186

def forward(x)
  return x unless @training && @p > 0

  scale = 1.0 / (1.0 - @p)
  mask_data = x.to_a.map { rand > @p ? scale : 0.0 }
  mask = Tensor.create(mask_data, x.shape)
  x * mask
end

#to_sObject



195
# File 'lib/grx/nn.rb', line 195

def to_s = "Dropout(p=#{@p})"

#train!Object



183
# File 'lib/grx/nn.rb', line 183

def train!;  @training = true;  self; end