Mila
Deep Neural Network Library
|
Dropout regularization module for neural networks. More...
Public Types | |
using | ModuleBase = Module< TDeviceType, TInput, TOutput > |
Alias for base module type. | |
using | MR = std::conditional_t< TDeviceType==DeviceType::Cuda, CudaMemoryResource, CpuMemoryResource > |
Memory resource type used for tensors, selected based on device type. | |
![]() | |
using | MR = std::conditional_t< TDeviceType==DeviceType::Cuda, CudaMemoryResource, CpuMemoryResource > |
Public Member Functions | |
Dropout (const std::string &device_name, const DropoutConfig &config) | |
Constructs a new Dropout module with a device name. | |
Dropout (std::shared_ptr< DeviceContext > device_context, const DropoutConfig &config) | |
Constructs a new Dropout module with a provided device context. | |
void | backward (const Tensor< TInput, MR > &input, const Tensor< TOutput, MR > &output_grad, Tensor< TInput, MR > &input_grad) |
Performs the backward pass of the Dropout operation. | |
void | forward (const Tensor< TInput, MR > &input, Tensor< TOutput, MR > &output) |
Performs the forward pass of the Dropout operation. | |
float | getProbability () const |
Gets the dropout probability used by this module. | |
unsigned int | getSeed () const |
Gets the current random seed. | |
void | load (ModelArchive &archive) override |
Deserializes the module state from a ZIP archive. | |
size_t | parameterCount () const override |
Gets the number of trainable parameters in this module. | |
void | save (ModelArchive &archive) const override |
Serializes the module state to a ZIP archive. | |
void | setSeed (unsigned int seed) |
Sets the random seed for dropout mask generation. | |
std::string | toString () const override |
Generates a string representation of this module's configuration. | |
![]() | |
Module (const std::string &device_name, const ComponentConfig &config) | |
Constructor with device name. | |
Module (std::shared_ptr< DeviceContext > context, const ComponentConfig &config) | |
Constructor with a specific device context. | |
virtual | ~Module ()=default |
Virtual destructor for proper cleanup in derived classes. | |
std::shared_ptr< Compute::DeviceContext > | getDeviceContext () const |
Get the device context for this module. | |
Compute::DeviceType | getDeviceType () const |
Get the device type of the current device context. | |
std::string | getName () const |
Get the name of the module. | |
const auto & | getParameterTensors () const |
Get the parameter tensors of this module. | |
const ComputePrecision::Policy & | getPrecision () const |
const auto & | getStateTensors () const |
Get the state tensors of this module. | |
bool | isTraining () const |
Check if the module is in training mode. | |
virtual void | setTraining (bool is_training) |
Set the training mode of this module. | |
Private Member Functions | |
void | createOperation () |
Creates the appropriate Dropout operation based on the current device context. | |
void | generateMask (Tensor< TOutput, MR > &mask, const std::vector< size_t > &shape) |
Generates a new dropout mask for the given shape. | |
Private Attributes | |
DropoutConfig | config_ |
Configuration for the Dropout module. | |
std::shared_ptr< Tensor< TOutput, MR > > | mask_ { nullptr } |
The binary mask tensor for element selection. | |
std::shared_ptr< UnaryOperation< TDeviceType, TInput, TOutput > > | operation_ { nullptr } |
The operation that implements the dropout calculation. | |
std::vector< std::shared_ptr< Tensor< TOutput, MR > > > | output_state_ |
Collection of output state tensors for caching. | |
std::vector< std::shared_ptr< Tensor< TOutput, MR > > > | parameters_ |
Collection of parameters for this module (empty for Dropout). | |
OperationAttributes | properties_ |
Operation attributes and configuration. | |
std::mt19937 | rng_ |
Random number generator for mask generation. | |
unsigned int | seed_ { 0 } |
Random seed for reproducible dropout patterns. | |
Additional Inherited Members | |
![]() | |
const std::string | parametersToString () const |
Helper method to convert parameters to string representation. | |
const std::string | stateToString () const |
Helper method to convert state tensors to string representation. | |
![]() | |
std::unordered_map< std::string, std::shared_ptr< Tensor< TOutput, MR > > > | parameter_map_ = {} |
Map of parameter names to parameter tensors. | |
std::unordered_map< std::string, std::shared_ptr< Tensor< TOutput, MR > > > | state_map_ = {} |
Map of state names to state tensors. | |
Dropout regularization module for neural networks.
Dropout is a regularization technique that randomly zeroes elements of an input tensor during training with probability p, and optionally scales the remaining elements by 1/(1-p). During inference, dropout is typically disabled.
Dropout helps prevent overfitting by preventing co-adaptation of feature detectors. The technique effectively trains an ensemble of multiple networks sharing parameters, which improves generalization.
TDeviceType | The device type (CPU or CUDA) on which the module will operate. |
TInput | The data type of the input tensor elements. |
TOutput | The data type of the output tensor elements, defaults to TInput. |
|
export |
Alias for base module type.
|
export |
Memory resource type used for tensors, selected based on device type.
|
inlineexplicitexport |
Constructs a new Dropout module with a device name.
Creates a new DeviceContext internally using the provided device name. This constructor is useful for creating standalone modules without pre-existing device contexts.
device_name | The name of the device to use (e.g., "CPU", "CUDA:0"). |
config | Configuration parameters for the Dropout module. |
std::invalid_argument | If the device name is invalid or the configuration is invalid |
std::runtime_error | If device type doesn't match template parameter TDeviceType |
|
inlineexplicitexport |
Constructs a new Dropout module with a provided device context.
Uses a pre-existing DeviceContext instance. This constructor is useful when integrating the module into a larger network that shares device contexts across modules.
device_context | The device context to use for this module. |
config | Configuration parameters for the Dropout module. |
std::invalid_argument | If device_context is null or configuration is invalid |
std::runtime_error | If device context type doesn't match template parameter TDeviceType |
|
inlineexport |
Performs the backward pass of the Dropout operation.
Computes the gradient of the Dropout function with respect to its input. During training, gradients are only propagated for non-zeroed elements. The gradient computation is straightforward: multiply the output gradient by the same mask used in the forward pass.
input | The input tensor from the forward pass. |
output_grad | The gradient of loss with respect to the output. |
input_grad | The tensor to store gradients with respect to input. |
|
inlineexportprivate |
Creates the appropriate Dropout operation based on the current device context.
This method initializes the operation_ member with the appropriate implementation of the Dropout operation for either CPU or CUDA, based on the current device context. It also passes the config object to the operation.
|
inlineexport |
Performs the forward pass of the Dropout operation.
During training, randomly zeroes elements with probability p and scales remaining elements by 1/(1-p). During inference, performs identity operation or scaling based on configuration.
input | The input tensor. |
output | The output tensor where the results will be stored. |
|
inlineexportprivate |
Generates a new dropout mask for the given shape.
Creates a binary mask with elements set to 0 (drop) with probability p, and scale factor 1/(1-p) (keep) with probability (1-p).
mask | The tensor to populate with the mask values |
shape | The shape of the input/mask tensor |
|
inlineexport |
Gets the dropout probability used by this module.
|
inlineexport |
Gets the current random seed.
|
inlineoverrideexportvirtual |
Deserializes the module state from a ZIP archive.
Implementation of the Module interface for deserialization. Since Dropout has no learnable parameters, this is a no-op implementation.
zip | ZIP archive for deserialization |
Implements Mila::Dnn::Module< TDeviceType, TInput, TOutput >.
|
inlineoverrideexportvirtual |
Gets the number of trainable parameters in this module.
The Dropout module has no trainable parameters.
Implements Mila::Dnn::Module< TDeviceType, TInput, TOutput >.
|
inlineoverrideexportvirtual |
Serializes the module state to a ZIP archive.
Implementation of the Module interface for serialization. Since Dropout has no learnable parameters, this is a no-op implementation.
zip | ZIP archive for serialization |
Implements Mila::Dnn::Module< TDeviceType, TInput, TOutput >.
|
inlineexport |
Sets the random seed for dropout mask generation.
This allows for reproducible dropout patterns when needed.
seed | The random seed value |
|
inlineoverrideexportvirtual |
Generates a string representation of this module's configuration.
Implements Mila::Dnn::Module< TDeviceType, TInput, TOutput >.
|
exportprivate |
Configuration for the Dropout module.
|
exportprivate |
The binary mask tensor for element selection.
Contains 0 for dropped elements and scale factor for kept elements.
|
exportprivate |
The operation that implements the dropout calculation.
|
exportprivate |
Collection of output state tensors for caching.
|
exportprivate |
Collection of parameters for this module (empty for Dropout).
|
exportprivate |
Operation attributes and configuration.
|
exportprivate |
Random number generator for mask generation.
|
exportprivate |
Random seed for reproducible dropout patterns.