Mila
Deep Neural Network Library
Loading...
Searching...
No Matches
Mila::Dnn::Dropout< TDeviceType, TInput, TOutput > Class Template Referenceexport

Dropout regularization module for neural networks. More...

Inheritance diagram for Mila::Dnn::Dropout< TDeviceType, TInput, TOutput >:
Collaboration diagram for Mila::Dnn::Dropout< TDeviceType, TInput, TOutput >:

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.
 
- Public Types inherited from Mila::Dnn::Module< TDeviceType, TInput, TOutput >
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.
 
- Public Member Functions inherited from Mila::Dnn::Module< TDeviceType, TInput, TOutput >
 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::DeviceContextgetDeviceContext () 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::PolicygetPrecision () 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

- Protected Member Functions inherited from Mila::Dnn::Module< TDeviceType, TInput, TOutput >
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.
 
- Protected Attributes inherited from Mila::Dnn::Module< TDeviceType, TInput, TOutput >
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.
 

Detailed Description

template<DeviceType TDeviceType = DeviceType::Cuda, typename TInput = float, typename TOutput = TInput>
requires ValidFloatTensorTypes<TInput, TOutput>
class Mila::Dnn::Dropout< TDeviceType, TInput, TOutput >

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.

Template Parameters
TDeviceTypeThe device type (CPU or CUDA) on which the module will operate.
TInputThe data type of the input tensor elements.
TOutputThe data type of the output tensor elements, defaults to TInput.

Member Typedef Documentation

◆ ModuleBase

template<DeviceType TDeviceType = DeviceType::Cuda, typename TInput = float, typename TOutput = TInput>
using Mila::Dnn::Dropout< TDeviceType, TInput, TOutput >::ModuleBase = Module<TDeviceType, TInput, TOutput>
export

Alias for base module type.

◆ MR

template<DeviceType TDeviceType = DeviceType::Cuda, typename TInput = float, typename TOutput = TInput>
using Mila::Dnn::Dropout< TDeviceType, TInput, TOutput >::MR = std::conditional_t<TDeviceType == DeviceType::Cuda, CudaMemoryResource, CpuMemoryResource>
export

Memory resource type used for tensors, selected based on device type.

Constructor & Destructor Documentation

◆ Dropout() [1/2]

template<DeviceType TDeviceType = DeviceType::Cuda, typename TInput = float, typename TOutput = TInput>
Mila::Dnn::Dropout< TDeviceType, TInput, TOutput >::Dropout ( const std::string &  device_name,
const DropoutConfig config 
)
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.

Parameters
device_nameThe name of the device to use (e.g., "CPU", "CUDA:0").
configConfiguration parameters for the Dropout module.
Exceptions
std::invalid_argumentIf the device name is invalid or the configuration is invalid
std::runtime_errorIf device type doesn't match template parameter TDeviceType
Here is the call graph for this function:

◆ Dropout() [2/2]

template<DeviceType TDeviceType = DeviceType::Cuda, typename TInput = float, typename TOutput = TInput>
Mila::Dnn::Dropout< TDeviceType, TInput, TOutput >::Dropout ( std::shared_ptr< DeviceContext device_context,
const DropoutConfig config 
)
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.

Parameters
device_contextThe device context to use for this module.
configConfiguration parameters for the Dropout module.
Exceptions
std::invalid_argumentIf device_context is null or configuration is invalid
std::runtime_errorIf device context type doesn't match template parameter TDeviceType
Here is the call graph for this function:

Member Function Documentation

◆ backward()

template<DeviceType TDeviceType = DeviceType::Cuda, typename TInput = float, typename TOutput = TInput>
void Mila::Dnn::Dropout< TDeviceType, TInput, TOutput >::backward ( const Tensor< TInput, MR > &  input,
const Tensor< TOutput, MR > &  output_grad,
Tensor< TInput, MR > &  input_grad 
)
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.

Parameters
inputThe input tensor from the forward pass.
output_gradThe gradient of loss with respect to the output.
input_gradThe tensor to store gradients with respect to input.

◆ createOperation()

template<DeviceType TDeviceType = DeviceType::Cuda, typename TInput = float, typename TOutput = TInput>
void Mila::Dnn::Dropout< TDeviceType, TInput, TOutput >::createOperation ( )
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.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ forward()

template<DeviceType TDeviceType = DeviceType::Cuda, typename TInput = float, typename TOutput = TInput>
void Mila::Dnn::Dropout< TDeviceType, TInput, TOutput >::forward ( const Tensor< TInput, MR > &  input,
Tensor< TOutput, MR > &  output 
)
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.

Parameters
inputThe input tensor.
outputThe output tensor where the results will be stored.
Here is the call graph for this function:

◆ generateMask()

template<DeviceType TDeviceType = DeviceType::Cuda, typename TInput = float, typename TOutput = TInput>
void Mila::Dnn::Dropout< TDeviceType, TInput, TOutput >::generateMask ( Tensor< TOutput, MR > &  mask,
const std::vector< size_t > &  shape 
)
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).

Parameters
maskThe tensor to populate with the mask values
shapeThe shape of the input/mask tensor
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getProbability()

template<DeviceType TDeviceType = DeviceType::Cuda, typename TInput = float, typename TOutput = TInput>
float Mila::Dnn::Dropout< TDeviceType, TInput, TOutput >::getProbability ( ) const
inlineexport

Gets the dropout probability used by this module.

Returns
float The probability of zeroing elements
Here is the call graph for this function:

◆ getSeed()

template<DeviceType TDeviceType = DeviceType::Cuda, typename TInput = float, typename TOutput = TInput>
unsigned int Mila::Dnn::Dropout< TDeviceType, TInput, TOutput >::getSeed ( ) const
inlineexport

Gets the current random seed.

Returns
unsigned int The current random seed

◆ load()

template<DeviceType TDeviceType = DeviceType::Cuda, typename TInput = float, typename TOutput = TInput>
void Mila::Dnn::Dropout< TDeviceType, TInput, TOutput >::load ( ModelArchive archive)
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.

Parameters
zipZIP archive for deserialization

Implements Mila::Dnn::Module< TDeviceType, TInput, TOutput >.

◆ parameterCount()

template<DeviceType TDeviceType = DeviceType::Cuda, typename TInput = float, typename TOutput = TInput>
size_t Mila::Dnn::Dropout< TDeviceType, TInput, TOutput >::parameterCount ( ) const
inlineoverrideexportvirtual

Gets the number of trainable parameters in this module.

The Dropout module has no trainable parameters.

Returns
size_t Always returns 0 for Dropout.

Implements Mila::Dnn::Module< TDeviceType, TInput, TOutput >.

◆ save()

template<DeviceType TDeviceType = DeviceType::Cuda, typename TInput = float, typename TOutput = TInput>
void Mila::Dnn::Dropout< TDeviceType, TInput, TOutput >::save ( ModelArchive archive) const
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.

Parameters
zipZIP archive for serialization

Implements Mila::Dnn::Module< TDeviceType, TInput, TOutput >.

◆ setSeed()

template<DeviceType TDeviceType = DeviceType::Cuda, typename TInput = float, typename TOutput = TInput>
void Mila::Dnn::Dropout< TDeviceType, TInput, TOutput >::setSeed ( unsigned int  seed)
inlineexport

Sets the random seed for dropout mask generation.

This allows for reproducible dropout patterns when needed.

Parameters
seedThe random seed value

◆ toString()

template<DeviceType TDeviceType = DeviceType::Cuda, typename TInput = float, typename TOutput = TInput>
std::string Mila::Dnn::Dropout< TDeviceType, TInput, TOutput >::toString ( ) const
inlineoverrideexportvirtual

Generates a string representation of this module's configuration.

Returns
std::string A formatted string with module information

Implements Mila::Dnn::Module< TDeviceType, TInput, TOutput >.

Here is the call graph for this function:

Member Data Documentation

◆ config_

template<DeviceType TDeviceType = DeviceType::Cuda, typename TInput = float, typename TOutput = TInput>
DropoutConfig Mila::Dnn::Dropout< TDeviceType, TInput, TOutput >::config_
exportprivate

Configuration for the Dropout module.

◆ mask_

template<DeviceType TDeviceType = DeviceType::Cuda, typename TInput = float, typename TOutput = TInput>
std::shared_ptr<Tensor<TOutput, MR> > Mila::Dnn::Dropout< TDeviceType, TInput, TOutput >::mask_ { nullptr }
exportprivate

The binary mask tensor for element selection.

Contains 0 for dropped elements and scale factor for kept elements.

◆ operation_

template<DeviceType TDeviceType = DeviceType::Cuda, typename TInput = float, typename TOutput = TInput>
std::shared_ptr<UnaryOperation<TDeviceType, TInput, TOutput> > Mila::Dnn::Dropout< TDeviceType, TInput, TOutput >::operation_ { nullptr }
exportprivate

The operation that implements the dropout calculation.

◆ output_state_

template<DeviceType TDeviceType = DeviceType::Cuda, typename TInput = float, typename TOutput = TInput>
std::vector<std::shared_ptr<Tensor<TOutput, MR> > > Mila::Dnn::Dropout< TDeviceType, TInput, TOutput >::output_state_
exportprivate

Collection of output state tensors for caching.

◆ parameters_

template<DeviceType TDeviceType = DeviceType::Cuda, typename TInput = float, typename TOutput = TInput>
std::vector<std::shared_ptr<Tensor<TOutput, MR> > > Mila::Dnn::Dropout< TDeviceType, TInput, TOutput >::parameters_
exportprivate

Collection of parameters for this module (empty for Dropout).

◆ properties_

template<DeviceType TDeviceType = DeviceType::Cuda, typename TInput = float, typename TOutput = TInput>
OperationAttributes Mila::Dnn::Dropout< TDeviceType, TInput, TOutput >::properties_
exportprivate

Operation attributes and configuration.

◆ rng_

template<DeviceType TDeviceType = DeviceType::Cuda, typename TInput = float, typename TOutput = TInput>
std::mt19937 Mila::Dnn::Dropout< TDeviceType, TInput, TOutput >::rng_
exportprivate

Random number generator for mask generation.

◆ seed_

template<DeviceType TDeviceType = DeviceType::Cuda, typename TInput = float, typename TOutput = TInput>
unsigned int Mila::Dnn::Dropout< TDeviceType, TInput, TOutput >::seed_ { 0 }
exportprivate

Random seed for reproducible dropout patterns.


The documentation for this class was generated from the following file: