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.hooks.register(model)
model.zero_grad()
loss_fn(model(x), y).backward()
ExGrads.hooks.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))

ExGrads.hooks.deregister(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

ExGrads-0.1.12.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

ExGrads-0.1.12-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ExGrads-0.1.12.tar.gz
  • Upload date:
  • Size: 3.7 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.12.tar.gz
Algorithm Hash digest
SHA256 efb130ed8992f5d222917ec04edad3deed77b8186bddbd7634a5a9f9b931581b
MD5 7beb00f605d9cc88b589a4b5d39e82d7
BLAKE2b-256 2a5db24adf883c18240708b1af78ece70516b29c8a6378acfad9ae711b652e63

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ExGrads-0.1.12-py3-none-any.whl
  • Upload date:
  • Size: 5.1 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.12-py3-none-any.whl
Algorithm Hash digest
SHA256 c302415159c697d4e69f10a3f586e06eff810f98edc104703ffa303ea1085f47
MD5 719cf549486ea45670e9d83052526b70
BLAKE2b-256 5ec5e613d39517cdc93b69a83e3fb5104be28e64b5e64d981ce782f4469aa3a3

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