{"id":25178773,"url":"https://github.com/saran-nns/gradients","last_synced_at":"2026-02-19T12:31:15.805Z","repository":{"id":57435705,"uuid":"386222818","full_name":"Saran-nns/gradients","owner":"Saran-nns","description":"Build your deep learning algorithms with confidence","archived":false,"fork":false,"pushed_at":"2024-09-03T11:42:37.000Z","size":164,"stargazers_count":0,"open_issues_count":4,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-21T03:42:50.401Z","etag":null,"topics":["finite-difference-method","gradient-check","jax","pytorch","tensorflow2"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Saran-nns.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":"CITATION.cff","codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2021-07-15T08:44:00.000Z","updated_at":"2024-09-03T11:41:09.000Z","dependencies_parsed_at":"2025-04-13T16:15:29.379Z","dependency_job_id":null,"html_url":"https://github.com/Saran-nns/gradients","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/Saran-nns/gradients","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Saran-nns%2Fgradients","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Saran-nns%2Fgradients/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Saran-nns%2Fgradients/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Saran-nns%2Fgradients/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Saran-nns","download_url":"https://codeload.github.com/Saran-nns/gradients/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Saran-nns%2Fgradients/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268690217,"owners_count":24291080,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-04T02:00:09.867Z","response_time":79,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["finite-difference-method","gradient-check","jax","pytorch","tensorflow2"],"created_at":"2025-02-09T15:16:50.148Z","updated_at":"2025-10-17T23:31:41.349Z","avatar_url":"https://github.com/Saran-nns.png","language":"Python","readme":"\u003ca href=\"url\"\u003e\u003cimg src=\"https://raw.githubusercontent.com/Saran-nns/gradients/master/imgs/LOGO.jpg\"\u003e\u003c/a\u003e\n## Build your deep learning models with confidence\n\n\n[![Build Status](https://github.com/Saran-nns/gradients/actions/workflows/build.yml/badge.svg)](https://github.com/Saran-nns/gradients/actions/workflows/build.yml)\n[![codecov](https://codecov.io/gh/Saran-nns/gradients/branch/main/graph/badge.svg)](https://codecov.io/gh/Saran-nns/gradients)\n[![PyPI version](https://badge.fury.io/py/gradients.svg)](https://badge.fury.io/py/gradients)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n[![Downloads](https://pepy.tech/badge/gradients)](https://pepy.tech/project/gradients)\n[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)\n[![DOI](https://zenodo.org/badge/386222818.svg)](https://zenodo.org/badge/latestdoi/386222818)\n\nGradients 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`.\n\n### Installation\n\n``` pip install gradients```\n\n### Package Overview\nOptimizing deep learning models is a two step process:\n\n1. Compute gradients with respect to parameters\n\n2. Update the parameters given the gradients\n\nIn 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;\n\n### Activation function with backward\n\n```python\nclass MySigmoid(torch.autograd.Function):\n\n    @staticmethod\n    def forward(ctx, input):\n        output = 1/(1+torch.exp(-input))\n        ctx.save_for_backward(output)\n        return output\n\n    @staticmethod\n    def backward(ctx, grad_output):\n        input, = ctx.saved_tensors\n        return grad_output*input*(1-input)\n```\n\n### Loss function with backward\n\n```python\nclass MSELoss(torch.autograd.Function):\n\n    @staticmethod\n    def forward(ctx, y_pred, y):\n        ctx.save_for_backward(y_pred, y)\n        return ((y_pred-y)**2).sum()/y_pred.shape[0]\n\n    @staticmethod\n    def backward(ctx, grad_output):\n        y_pred, y = ctx.saved_tensors\n        grad_input = 2 * (y_pred-y)/y_pred.shape[0]\n        return grad_input, None\n```\n### Pytorch Model\n\n```python\nclass MyModel(torch.nn.Module):\n    def __init__(self,D_in, D_out):\n        super(MyModel,self).__init__()\n        self.w1 = torch.nn.Parameter(torch.randn(D_in, D_out), requires_grad=True)\n        self.sigmoid = MySigmoid.apply\n    def forward(self,x):\n        y_pred = self.sigmoid(x.mm(self.w1))\n        return y_pred\n```\n### Check your implementation using Gradient\n\n```python\nimport torch\nfrom gradients import Gradient\n\nN, D_in, D_out = 10, 4, 3\n\n# Create random Tensors to hold inputs and outputs\nx = torch.randn(N, D_in)\ny = torch.randn(N, D_out)\n\n# Construct model by instantiating the class defined above\nmymodel = MyModel(D_in, D_out)\ncriterion = MSELoss.apply\n\n# Test custom build model\nGradient(mymodel,x,y,criterion,eps=1e-8)\n\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsaran-nns%2Fgradients","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsaran-nns%2Fgradients","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsaran-nns%2Fgradients/lists"}