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
- Fast and Exact calculating $
\text{tr}[\bold{H}]
$ - other usages (comming soon in a month, I hope)
- Fast and Exact calculating $
- Less memory mode (WIP)
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
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.0.tar.gz
(3.9 kB
view details)
Built Distributions
ExGrads-0.1.0-py3.8.egg
(5.0 kB
view details)
File details
Details for the file ExGrads-0.1.0.tar.gz
.
File metadata
- Download URL: ExGrads-0.1.0.tar.gz
- Upload date:
- Size: 3.9 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 | 94e70267f08bb1a69efe851bb1c18b28451882cef3e5fc3b86db15fbf4e2de3a |
|
MD5 | 46614a2bbc67f3d390e7ef2f13fb72d2 |
|
BLAKE2b-256 | 51c3c0fcea9e065a5d616b6a2ed25a63b81bd3ebbb8cbe4884918a1d2251876e |
File details
Details for the file ExGrads-0.1.0-py3.8.egg
.
File metadata
- Download URL: ExGrads-0.1.0-py3.8.egg
- Upload date:
- Size: 5.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 49ebad906d745b7f6c2af15974395185ac5e1f8014330b28e3f3f533275a4500 |
|
MD5 | ad760759f4e19e8e29de16bbd12e2eef |
|
BLAKE2b-256 | 16f4cdc5494910d55c253c1065d1f5326717d95a9db2e23ce07452f4d0a411bc |
File details
Details for the file ExGrads-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: ExGrads-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9111eed1f1b085aa9b1778a55342b27fe2a18d1eb3b4424f17477b573fad8e4 |
|
MD5 | 1116c4b6a0470707b8cfe3457a6caaca |
|
BLAKE2b-256 | 28a49e26af2c127ed98a765ce8e3630d5cacaa253ad5b68aa97876b524041269 |