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
- Calculate example-wise gradient efficiently
There is no method calculating Hessian in contrast to the referenced work. - Handle general modules
Including Linear, Conv2d, BatchNorm2d, and BatchNorm1d. More modules will be added soon. - How to use this script in practice
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.register(model)
model.zero_grad()
loss_fn(model(x), y).backward()
# 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.deregister(model)
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
ExGrads-0.1.8.tar.gz
(4.4 kB
view details)
Built Distribution
File details
Details for the file ExGrads-0.1.8.tar.gz
.
File metadata
- Download URL: ExGrads-0.1.8.tar.gz
- Upload date:
- Size: 4.4 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | bda68bbb77629876c238545ff1dc821e26235bfe173865b33a842aa74ca17ea1 |
|
MD5 | 00ea6678b2717ee2783848fc01794610 |
|
BLAKE2b-256 | 04c00bfe4e6df48de8fa26374542a73884f3378d1fc1c317114c4bfd094e807a |
File details
Details for the file ExGrads-0.1.8-py3-none-any.whl
.
File metadata
- Download URL: ExGrads-0.1.8-py3-none-any.whl
- Upload date:
- Size: 5.8 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3ee649c69332a954cf6fcd71265d131045cd130ded2c6846c52a521bff53886 |
|
MD5 | badf19477489cc634086f5e3046f3a8d |
|
BLAKE2b-256 | 4a12171615d44e8966fdbfecdfcfe1fb78ce0d2576db8d706c41467f766ba925 |