Skip to main content

Overview

The utils module provides various utility functions for common tasks in deep learning, including seeding, data conversion, model management, and learning rate scheduling.

Random Seed Management

seed_everything

Set random seed for all random number generators to ensure reproducibility.
int
required
Random seed value.

Data Conversion

to_numpy

Convert a tensor to a numpy array.
Union[Tensor, np.ndarray]
required
Tensor or numpy array to convert.
np.ndarray
NumPy array.

to_tensor

Convert data to a Neurenix tensor.
Union[Tensor, np.ndarray, List, Tuple]
required
Data to convert.
Optional[Union[str, Device]]
Device to store the tensor on.
Tensor
Neurenix tensor.

one_hot

Convert indices to one-hot encoding.
Union[Tensor, np.ndarray, List[int]]
required
Class indices.
int
required
Number of classes.
Tensor
One-hot encoded tensor.

Model Management

get_module_device

Get the device of a module by checking its parameters.
Module
required
Module to check.
Optional[Device]
Device of the module or None if the module has no parameters.

move_module_to_device

Move a module to a device.
Module
required
Module to move.
Union[str, Device]
required
Device to move the module to.

count_parameters

Count the number of trainable parameters in a module.
Module
required
Module to count parameters for.
int
Number of trainable parameters.

model_summary

Get a summary of a module.
Module
required
Module to summarize.
str
Summary string.

Optimizer Utilities

get_learning_rate

Get the learning rate of an optimizer.
Optimizer
required
Optimizer to get learning rate from.
float
Learning rate.

set_learning_rate

Set the learning rate of an optimizer.
Optimizer
required
Optimizer to set learning rate for.
float
required
Learning rate.

Learning Rate Schedulers

StepLR

Decays the learning rate by gamma every step_size epochs.
Optimizer
required
Optimizer to schedule learning rate for.
int
required
Period of learning rate decay.
float
default:"0.1"
Multiplicative factor of learning rate decay.

Methods

Step the learning rate scheduler.

ExponentialLR

Decays the learning rate by gamma every epoch.

ReduceLROnPlateau

Reduce learning rate when a metric has stopped improving.
str
default:"min"
One of ‘min’ or ‘max’. In ‘min’ mode, lr will be reduced when the quantity monitored has stopped decreasing.
float
default:"0.1"
Factor by which the learning rate will be reduced.
int
default:"10"
Number of epochs with no improvement after which learning rate will be reduced.

Methods

Step the scheduler based on the validation metric.

Decorators

timeit

Decorator to measure the execution time of a function.

Example Usage

Best Practices

Always seed: Call seed_everything() at the start of your script for reproducible results.
Monitor parameters: Use count_parameters() to understand your model size and memory requirements.
Learning rate scheduling: Start with ReduceLROnPlateau for adaptive learning rate adjustment based on validation metrics.
Profile code: Use the @timeit decorator to identify performance bottlenecks.