Skip to main content

calculate example-wise gradient

Project description

this repository is still under construction (2021/07/21)

ExGrads

This repository provides a hook script: calculating Example-wise Gradients efficiently.

Note

This script use the work as an important reference.
I think it is the great first step to handle per-example gradients efficiently.
I'd like to express my respect for the step.

Features of This Script

How to Use

import torch
import exgrads as ExGrads

batch,dim,label = 5,3,2
x = torch.randn(batch,dim)                                  #: inputs
y = torch.randint(low=0,high=label-1,size=(batch,))         #: outputs
model   = torch.nn.Sequential(torch.nn.Linear(dim, label))  #: PyTorch model
loss_fn = torch.nn.functional.cross_entropy                 #: loss function

ExGrads.add_hooks(model)
model.zero_grad()
loss_fn(model(x), y).backward()
ExGrads.compute_grad1(model)

# param.grad:     gradient averaged over the batch
# param.grad1[i]: gradient of i-th example
for param in model.parameters():
	assert(torch.allclose(param.grad1.sum(dim=0), param.grad))

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

ExGrads-0.1.2.tar.gz (4.0 kB view details)

Uploaded Source

Built Distributions

ExGrads-0.1.2-py3.8.egg (6.6 kB view details)

Uploaded Source

ExGrads-0.1.2-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file ExGrads-0.1.2.tar.gz.

File metadata

  • Download URL: ExGrads-0.1.2.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.10

File hashes

Hashes for ExGrads-0.1.2.tar.gz
Algorithm Hash digest
SHA256 a71ec62c246c8883dbe207768d6dba1f6263d7f8395d29e68074a0875c31d65f
MD5 d5892dd94460d681e04478038c26acc7
BLAKE2b-256 8d564846565ea86dc932c477fda4d1a399677f1bd58581b705f8ad00c9b338ce

See more details on using hashes here.

File details

Details for the file ExGrads-0.1.2-py3.8.egg.

File metadata

  • Download URL: ExGrads-0.1.2-py3.8.egg
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.10

File hashes

Hashes for ExGrads-0.1.2-py3.8.egg
Algorithm Hash digest
SHA256 9e473298cce9793e5a078dacc27cd286d635679273c76b410da0a04cc6b63d47
MD5 2a9df7aa8a9d0636903c95d90e7f9d5e
BLAKE2b-256 99cf16965201b3264ea3119d35c5dbfb92aab89be516832bf914fb7a41b86aa6

See more details on using hashes here.

File details

Details for the file ExGrads-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: ExGrads-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.10

File hashes

Hashes for ExGrads-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ac917ae4b850325b32739739c355b18ccb5d00c9bea7eb54de6062ba238eac8b
MD5 91cbbdffcc393ee0520f0e8fd3e41dec
BLAKE2b-256 7a2cee4f913b756472be16360fe342a8ebea68f33c1252a15d492a003d6abd35

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