MLTests
TorchBlaze comes with a PyTorch-based model testing suite, which is essentially a set of methods that can be used to perform a variety of integrity checks on your PyTorch models before and during the model training process in a bit to ensure that no unexpected results show up in your model outputs.
Getting Started with MLTests#
For using the set of automated model-testing methods available in the mltests module, the first step is to import it. This can be achieved with this simple command:
To get a list of all the methods available in the module, you can use the following command:
Here's the list of methods, along with their usage, provided in the mltests package.
Model Tests#
# mls.get_params#
Retrieves the list of all the named parameters associated with a model.
Arguments: model::torch.nn.Module- Ideally the deep learning model, but can be a singel model layer/set of layers too.
Returns: param_list::list- List of all the named parameters in the model.
Usage:#
Each element in the param_list will be a tuple, the first element of which is the parameter name, and the second element is the tensor of parameters associated with the named parameter.
# mls.check_cuda#
Checks if the training device is of type CUDA or not.
Arguments: params::torch.Tensor- The parameters associated with a model layer.
Returns: None: Throws an exception if the training device is not CUDA-enabled.
Usage:#
Here is a an example of how you can implement this test. In order to use it as a standalone test, you need to provide the method, as an argument, a tensor of named parameters associated with the model. It can be any named parameter associated with the model.
In case the model is not training on a CUDA-enabled device, you will get a DeviceNotCudaException exception.
# mls.check_nan#
Tests for the presence of NaN values [torch.tensor(float("nan"))] in the given tensor of model parameter.
Arguments: name::str- Name of the parameter params::torch.Tensor- Trainable named parameters associated with a layer
Returns: None- Throws an exception in case any parameter is a NaN value.
Usage:#
In case any model parameter is a NaN value you will get a NaNParamsException exception.
# mls.check_infinite#
Tests for the presence of infinite values [torch.tensor(float("Inf"))] in the given tensor of model parameter.
Arguments: name::str- Name of the parameter params::torch.Tensor- Trainable named parameters associated with a layer
Returns: None- Throws an exception in case any parameter is a Inf value.
Usage:#
In case any model parameter is a infinite value you will get a InfParamsException exception.
# mls.check_smaller#
Tests if the absolute value of any parameter exceeds a certain threshold.
Arguments: name::str- Name of the parameter params::torch.Tensor- Trainable named parameters associated with a layer upper_limit::float- The threshold value every parameter should be smaller than in terms of its absolute value.
Returns: None- Throws an exception in case any parameter is a exceeds threshold value.
Usage:#
In case any parameter exceeds a upper_limit threshold value you will get a ParamsTooLargeException exception.
# mls.check_greater#
Tests if the absolute value of any parameter falls below a certain threshold.
Arguments: name::str- Name of the parameter params::torch.Tensor- Trainable named parameters associated with a layer lower_limit::float- The threshold value every parameter should be greater than in terms of its absolute value.
Returns: None- Throws an exception in case any parameter is a NaN value.
Usage:#
In case any parameter falls below the lower_limit threshold value you will get a ParamsTooSmallException exception.
# mls.check_gradient_smaller#
Tests if the absolute gradient value for any parameter exceed a certain threshold. Can be used to check for gradient explosion.
Arguments: name::str- Name of the parameter params::torch.Tensor- Trainable named parameters associated with a layer grad_limit::float- A threshold value, such that, |params.grad| < grad_limit
Returns: None- Throws an exception in case the gradient for any parameter exceeds the threshold value (grad_limit) OR None- Throws an exception in case the method is used without running the loss.backward() method for backprop.
Usage:#
caution
Running this method for a model whose gradients have not been initialized (i.e., a model that has not gone training) will result in GradientsUninitializedException.
In case any parameter's gradient value exceeds the grad_limit threshold value, you will get a GradientAboveThresholdException exception.
# mls.check_params_changing#
Tests if the parameters in the model/certain layer are changing after a training cycle.
Arguments: name::str- Name of the parameter params_list_old::list- List of trainable model parameters BEFORE training cycle. params_list_new::list- List of trainable model parameters AFTER training cycle.
Returns: None- Throws an exception in case the parameters are not changing
Usage:#
In case none of the model parameters change after training the model, you get a ParamsNotChangingException exception.
Automated Test#
While the methods given above can be used to define your own model tests, the mltests module comes with an automated test too.
# mls.model_test#
Executes a suite of automated tests on the ML model. Set <test_name> = False if you want to exclude a certain test from the test suite.
Arguments: model::nn.Module- The model that you want to test.
Usage:#
In the example given above, we assume that the model class object is stored under that variable name net.
x: A batch of training features
y: A batch of training lables, corresponding to the feature batch x.