Gradient Checker for Custom built PyTorch Models
Project description
Build your deep learning models with confidence
Gradients provide a self consistency test function to perform gradient checking on your deep learning models. It uses centered finite difference approximation method to check the difference between analytical and numerical gradients and report if the check fails on any parameters of your model. Currently the library supports only PyTorch models built with custom layers, custom loss functions, activation functions and any neural network function subclassing AutoGrad
.
Installation
pip install gradients
Package Overview
Optimizing deep learning models is a two step process:
-
Compute gradients with respect to parameters
-
Update the parameters given the gradients
In PyTorch, step 1 is done by the type-based automatic differentiation system torch.nn.autograd
and 2 by the package implementing optimization algorithms torch.optim
. Using them, we can develop fully customized deep learning models with torch.nn.Module
and test them using Gradient
as follows;
Activation function with backward
class MySigmoid(torch.autograd.Function):
@staticmethod
def forward(ctx, input):
output = 1/(1+torch.exp(-input))
ctx.save_for_backward(output)
return output
@staticmethod
def backward(ctx, grad_output):
input, = ctx.saved_tensors
return grad_output*input*(1-input)
Loss function with backward
class MSELoss(torch.autograd.Function):
@staticmethod
def forward(ctx, y_pred, y):
ctx.save_for_backward(y_pred, y)
return ((y_pred-y)**2).sum()/y_pred.shape[0]
@staticmethod
def backward(ctx, grad_output):
y_pred, y = ctx.saved_tensors
grad_input = 2 * (y_pred-y)/y_pred.shape[0]
return grad_input, None
Pytorch Model
class MyModel(torch.nn.Module):
def __init__(self,D_in, D_out):
super(MyModel,self).__init__()
self.w1 = torch.nn.Parameter(torch.randn(D_in, D_out), requires_grad=True)
self.sigmoid = MySigmoid.apply
def forward(self,x):
y_pred = self.sigmoid(x.mm(self.w1))
return y_pred
Check your implementation using Gradient
import torch
from gradients import Gradient
N, D_in, D_out = 10, 4, 3
# Create random Tensors to hold inputs and outputs
x = torch.randn(N, D_in)
y = torch.randn(N, D_out)
# Construct model by instantiating the class defined above
mymodel = MyModel(D_in, D_out)
criterion = MSELoss.apply
# Test custom build model
Gradient(mymodel,x,y,criterion,eps=1e-8)
Citing Gradients
@software{nambusubramaniyan_saranraj_2021_5176243,
author = {Nambusubramaniyan, Saranraj},
title = {gradients},
month = aug,
year = 2021,
publisher = {Zenodo},
version = {1.0.0},
doi = {10.5281/zenodo.5176243},
url = {https://doi.org/10.5281/zenodo.5176243}
}```
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file gradients-0.0.3.tar.gz
.
File metadata
- Download URL: gradients-0.0.3.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0.post20200210 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.6.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f5b4faeb19a7d8ba99be2d0d6fb3e2a9b233a2d673de9ed47afad56ae6bb7954 |
|
MD5 | b0b9b3eb96b6e5f191b148ab66f10fa9 |
|
BLAKE2b-256 | 085e6f5269cdd6b79e30c6a71a0ebace8e44c7bccfb9849174b0ce48e7af10e7 |
File details
Details for the file gradients-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: gradients-0.0.3-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0.post20200210 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.6.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d79b31cd1982c7b595bed64868fd4050da456ca527e94770bafabd19eba0fc0e |
|
MD5 | e336b0b979dadcf978c4800d5cb54538 |
|
BLAKE2b-256 | 22ef1db37f292a33ae919bae6f5c72a8e90cac61d7daf56e6558bd8c9f9c7e28 |