Class: GRX::NN::Dropout

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

Overview

Dropout — regularización durante entrenamiento

Instance Method Summary collapse

Methods inherited from Module

#call, #parameters, #zero_grad

Constructor Details

#initialize(p = 0.5) ⇒ Dropout

Returns a new instance of Dropout.



170
171
172
173
# File 'lib/grx/nn.rb', line 170

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

Instance Method Details

#eval!Object



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

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

#forward(x) ⇒ Object



178
179
180
181
182
183
184
185
186
187
# File 'lib/grx/nn.rb', line 178

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

  # Máscara binaria: 1 con prob (1-p), 0 con prob p
  # Escalamos por 1/(1-p) para mantener la esperanza (inverted dropout)
  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



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

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

#train!Object



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

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