Skip to main content

Gradient Checker for Custom built PyTorch Models

Project description

Build your deep learning models with confidence

Medium article is under work

Build Status codecov PyPI version Code style: black PyPI - Downloads License

Gradients provide a unit test function to perform gradient checking on your deep learning models. It uses centered finite difference approximation 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.

Optimizing deep learning models is a two step process:

  1. Compute gradients with respect to parameters

  2. 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 Gradients as follows;

Activation function with backward

class MySigmoid(torch.autograd.Function):

    @staticmethod
    def forward(ctx, input):
        output = ctx.save_for_backward(output)
        ctx.save_for_backward(output)
        return output

    @staticmethod
    def backward(ctx, grad_output):
        input, = ctx.saved_tensors
        return input*(1-input)*grad_output

Loss function with autograd 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)
    def forward(self,x):
        y_pred = mysigmoid(x.mm(self.w1))
        return y_pred

Optimizer

class SGD(torch.optim.Optimizer):
    """Reference: http://pytorch.org/docs/master/_modules/torch/optim/sgd.html#SGD"""
    def __init__(self, params, lr=1e-3):
        defaults = dict(lr=lr)
        super(SGD,self).__init__(params,defaults)

    def __setstate__(self, state):
        super(SGD, self).__setstate__(state)

    def step(self, closure=None):

        loss = None
        if closure is not None:
            loss = closure()

        for group in self.param_groups:

            for p in group['params']:
                if p.grad is None:
                    continue
                d_p = p.grad.data
                p.data.add_(-group['lr'], d_p)
        return loss

TODO

Instantiate, gradcheck and train the model

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gradients-0.0.2.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

gradients-0.0.2-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file gradients-0.0.2.tar.gz.

File metadata

  • Download URL: gradients-0.0.2.tar.gz
  • Upload date:
  • Size: 4.9 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

Hashes for gradients-0.0.2.tar.gz
Algorithm Hash digest
SHA256 446fff205a2c7cfe94f92a6efaee02ff3a64a2860638138cb9af5bbcc9bfc337
MD5 555b6872f0ee12bfa74ebb8151065efe
BLAKE2b-256 b34facb862c4cfa3cb4bc0040772554f613252fddd6b7782c3984c9a8760e845

See more details on using hashes here.

File details

Details for the file gradients-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: gradients-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.9 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

Hashes for gradients-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 51efe0e7004e551644dd92306c03a9ebe126e77156145a52b11aaf247b12ae72
MD5 5af4c6524d042b827572f09db54f1d38
BLAKE2b-256 cb2ee3d61cc729e8b6e53a82946ac4ca43cea68ce25b24cc808de8cf4490d0e2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page