Skip to content

Commit 7e74ec7

Browse files
committed
Update Documentation
1 parent 26f936c commit 7e74ec7

File tree

11 files changed

+594
-46
lines changed

11 files changed

+594
-46
lines changed

.github/workflows/sphinx-gh-pages.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
run: |
3636
python -m pip install --upgrade pip
3737
pip install torch --index-url https://download.pytorch.org/whl/cpu
38-
pip install sphinx sphinxcontrib-katex sphinx-rtd-theme
38+
pip install sphinx sphinxcontrib-katex sphinx-copybutton sphinx-rtd-theme
3939
- name: Sphinx Build
4040
run: |
4141
cd docs/

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 Takenori Yamamoto
3+
Copyright (c) 2019-2024 Takenori Yamamoto
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

MANIFEST.in

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
include test/__init__.py
2+
include examples/plots/README.md
23
include examples/plots/*.py
34
include examples/plots/figs/*.png
5+
include examples/emnist/README.md
46
include examples/emnist/*.py
57
include examples/emnist/figs/*.png

README.md

+28-13
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,36 @@ This library contains PyTorch implementations of the warmup schedules described
44

55
<p align="center"><img src="https://github.com/Tony-Y/pytorch_warmup/raw/master/examples/plots/figs/warmup_schedule.png" alt="Warmup schedule" width="400"/></p>
66

7-
![Python package](https://github.com/Tony-Y/pytorch_warmup/workflows/Python%20package/badge.svg)
7+
[![Python package](https://github.com/Tony-Y/pytorch_warmup/workflows/Python%20package/badge.svg)](https://github.com/Tony-Y/pytorch_warmup/)
88
[![PyPI version shields.io](https://img.shields.io/pypi/v/pytorch-warmup.svg)](https://pypi.python.org/pypi/pytorch-warmup/)
9-
[![PyPI license](https://img.shields.io/pypi/l/pytorch-warmup.svg)](https://pypi.python.org/pypi/pytorch-warmup/)
10-
[![PyPI pyversions](https://img.shields.io/pypi/pyversions/pytorch-warmup.svg)](https://pypi.python.org/pypi/pytorch-warmup/)
9+
[![PyPI license](https://img.shields.io/pypi/l/pytorch-warmup.svg)](https://github.com/Tony-Y/pytorch_warmup/blob/master/LICENSE)
10+
[![Python versions](https://img.shields.io/badge/python-3.7%20%7C%203.8%20%7C%203.9%20%7C%203.10%20%7C%203.11%20%7C%203.12-blue)](https://www.python.org)
1111

1212
## Installation
1313

14-
Make sure you have Python 3.7+ and PyTorch 1.1+. Then, run the following command in the project directory:
14+
Make sure you have Python 3.7+ and PyTorch 1.1+ or 2.x. Then, run the following command in the project directory:
1515

16-
```
16+
```shell
1717
python -m pip install .
1818
```
1919

2020
or install the latest version from the Python Package Index:
2121

22-
```
22+
```shell
2323
pip install -U pytorch_warmup
2424
```
2525

26+
## Examples
27+
28+
* [EMNIST](https://github.com/Tony-Y/pytorch_warmup/tree/master/examples/emnist) -
29+
A sample script to train a CNN model on the EMNIST dataset using the Adam algorithm with a warmup.
30+
* [Plots](https://github.com/Tony-Y/pytorch_warmup/tree/master/examples/plots) -
31+
A script to plot effective warmup periods as a function of 𝛽₂, and warmup schedules over time.
32+
2633
## Usage
2734

35+
The [Documentation](https://tony-y.github.io/pytorch_warmup/) provides more detailed information on this library, unseen below.
36+
2837
### Sample Codes
2938

3039
The scheduled learning rate is dampened by the multiplication of the warmup factor:
@@ -34,16 +43,20 @@ The scheduled learning rate is dampened by the multiplication of the warmup fact
3443
#### Approach 1
3544
[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Tony-Y/colab-notebooks/blob/master/PyTorch_Warmup_Approach1_chaining.ipynb)
3645

37-
When the learning rate schedule uses the global iteration number, the untuned linear warmup can be used as follows:
46+
When the learning rate schedule uses the global iteration number, the untuned linear warmup can be used
47+
together with `Adam` or its variant (`AdamW`, `NAdam`, etc.) as follows:
3848

3949
```python
4050
import torch
4151
import pytorch_warmup as warmup
4252

4353
optimizer = torch.optim.AdamW(params, lr=0.001, betas=(0.9, 0.999), weight_decay=0.01)
54+
# This sample code uses the AdamW optimizer.
4455
num_steps = len(dataloader) * num_epochs
4556
lr_scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=num_steps)
57+
# The LR schedule initialization resets the initial LR of the optimizer.
4658
warmup_scheduler = warmup.UntunedLinearWarmup(optimizer)
59+
# The warmup schedule initialization dampens the initial LR of the optimizer.
4760
for epoch in range(1,num_epochs+1):
4861
for batch in dataloader:
4962
optimizer.zero_grad()
@@ -53,9 +66,9 @@ for epoch in range(1,num_epochs+1):
5366
with warmup_scheduler.dampening():
5467
lr_scheduler.step()
5568
```
56-
Note that the warmup schedule must not be initialized before the learning rate schedule.
69+
Note that the warmup schedule must not be initialized before the initialization of the learning rate schedule.
5770

58-
If you want to use the learning rate schedule "chaining" which is supported for PyTorch 1.4.0 or above, you may simply give a code of learning rate schedulers as a suite of the `with` statement:
71+
If you want to use the learning rate schedule *chaining*, which is supported for PyTorch 1.4 or above, you may simply write a code of learning rate schedulers as a suite of the `with` statement:
5972
```python
6073
lr_scheduler1 = torch.optim.lr_scheduler.ExponentialLR(optimizer, gamma=0.9)
6174
lr_scheduler2 = torch.optim.lr_scheduler.StepLR(optimizer, step_size=3, gamma=0.1)
@@ -163,7 +176,7 @@ warmup_scheduler = warmup.ExponentialWarmup(optimizer, warmup_period=1000)
163176

164177
#### Untuned Warmup
165178

166-
The warmup period is given by a function of Adam's `beta2` parameter for `UntunedLinearWarmup` and `UntunedExponentialWarmup`.
179+
The warmup period is determined by a function of Adam's `beta2` parameter for `UntunedLinearWarmup` and `UntunedExponentialWarmup`.
167180

168181
##### Linear
169182

@@ -183,15 +196,17 @@ warmup_scheduler = warmup.UntunedExponentialWarmup(optimizer)
183196

184197
#### RAdam Warmup
185198

186-
The warmup factor depends on Adam's `beta2` parameter for `RAdamWarmup`. Please see the original paper for the details.
199+
The warmup factor depends on Adam's `beta2` parameter for `RAdamWarmup`. For details please refer to the
200+
[Documentation](https://tony-y.github.io/pytorch_warmup/radam_warmup.html) or
201+
"[On the Variance of the Adaptive Learning Rate and Beyond](https://arxiv.org/abs/1908.03265)."
187202

188203
```python
189204
warmup_scheduler = warmup.RAdamWarmup(optimizer)
190205
```
191206

192207
### Apex's Adam
193208

194-
The Apex library provides an Adam optimizer tuned for CUDA devices, [FusedAdam](https://nvidia.github.io/apex/optimizers.html#apex.optimizers.FusedAdam). The FusedAdam optimizer can be used with the warmup schedulers. For example:
209+
The Apex library provides an Adam optimizer tuned for CUDA devices, [FusedAdam](https://nvidia.github.io/apex/optimizers.html#apex.optimizers.FusedAdam). The FusedAdam optimizer can be used together with any one of the warmup schedules above. For example:
195210

196211
[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Tony-Y/colab-notebooks/blob/master/PyTorch_Warmup_FusedAdam.ipynb)
197212

@@ -206,4 +221,4 @@ warmup_scheduler = warmup.UntunedLinearWarmup(optimizer)
206221

207222
MIT License
208223

209-
Copyright (c) 2019 Takenori Yamamoto
224+
© 2019-2024 Takenori Yamamoto

docs/conf.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# -- Project information -----------------------------------------------------
2222

2323
project = 'PyTorch Warmup'
24-
copyright = '2019, Takenori Yamamoto'
24+
copyright = '2019-2024, Takenori Yamamoto'
2525
author = 'Takenori Yamamoto'
2626

2727

@@ -40,6 +40,7 @@
4040
'sphinx.ext.napoleon',
4141
'sphinx.ext.viewcode',
4242
'sphinxcontrib.katex',
43+
'sphinx_copybutton',
4344
]
4445

4546
# Add any paths that contain templates here, relative to this directory.
@@ -61,4 +62,8 @@
6162
# Add any paths that contain custom static files (such as style sheets) here,
6263
# relative to this directory. They are copied after the builtin static files,
6364
# so a file named "default.css" will overwrite the builtin "default.css".
64-
#html_static_path = ['_static']
65+
# html_static_path = ['_static']
66+
67+
# Copybutton settings
68+
copybutton_prompt_text = r">>> |\.\.\. |\$ "
69+
copybutton_prompt_is_regexp = True

docs/index.rst

+73
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,79 @@ This library contains PyTorch implementations of the warmup schedules described
1010
`On the adequacy of untuned warmup for adaptive optimization
1111
<https://arxiv.org/abs/1910.04209>`_.
1212

13+
.. image:: https://github.com/Tony-Y/pytorch_warmup/raw/master/examples/plots/figs/warmup_schedule.png
14+
:alt: Warmup schedule
15+
:width: 400
16+
:align: center
17+
18+
.. image:: https://github.com/Tony-Y/pytorch_warmup/workflows/Python%20package/badge.svg
19+
:alt: Python package
20+
:target: https://github.com/Tony-Y/pytorch_warmup/
21+
22+
.. image:: https://img.shields.io/pypi/v/pytorch-warmup.svg
23+
:alt: PyPI version shields.io
24+
:target: https://pypi.python.org/pypi/pytorch-warmup/
25+
26+
.. image:: https://img.shields.io/pypi/l/pytorch-warmup.svg
27+
:alt: PyPI license
28+
:target: https://github.com/Tony-Y/pytorch_warmup/blob/master/LICENSE
29+
30+
.. image:: https://img.shields.io/badge/python-3.7%20%7C%203.8%20%7C%203.9%20%7C%203.10%20%7C%203.11%20%7C%203.12-blue
31+
:alt: Python versions
32+
:target: https://www.python.org
33+
34+
Installation
35+
------------
36+
37+
Make sure you have Python 3.7+ and PyTorch 1.1+ or 2.x. Then, install the latest version from the Python Package Index:
38+
39+
.. code-block:: shell
40+
41+
pip install -U pytorch_warmup
42+
43+
Examples
44+
--------
45+
46+
.. image:: https://colab.research.google.com/assets/colab-badge.svg
47+
:alt: Open In Colab
48+
:target: https://colab.research.google.com/github/Tony-Y/colab-notebooks/blob/master/PyTorch_Warmup_Approach1_chaining.ipynb
49+
50+
* `EMNIST <https://github.com/Tony-Y/pytorch_warmup/tree/master/examples/emnist>`_ -
51+
A sample script to train a CNN model on the EMNIST dataset using the Adam algorithm with a warmup.
52+
53+
* `Plots <https://github.com/Tony-Y/pytorch_warmup/tree/master/examples/plots>`_ -
54+
A script to plot effective warmup periods as a function of :math:`\beta_{2}`, and warmup schedules over time.
55+
56+
Usage
57+
-----
58+
59+
When the learning rate schedule uses the global iteration number, the untuned linear warmup can be used
60+
together with :class:`Adam` or its variant (:class:`AdamW`, :class:`NAdam`, etc.) as follows:
61+
62+
.. code-block:: python
63+
64+
import torch
65+
import pytorch_warmup as warmup
66+
67+
optimizer = torch.optim.AdamW(params, lr=0.001, betas=(0.9, 0.999), weight_decay=0.01)
68+
# This sample code uses the AdamW optimizer.
69+
num_steps = len(dataloader) * num_epochs
70+
lr_scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=num_steps)
71+
# The LR schedule initialization resets the initial LR of the optimizer.
72+
warmup_scheduler = warmup.UntunedLinearWarmup(optimizer)
73+
# The warmup schedule initialization dampens the initial LR of the optimizer.
74+
for epoch in range(1,num_epochs+1):
75+
for batch in dataloader:
76+
optimizer.zero_grad()
77+
loss = ...
78+
loss.backward()
79+
optimizer.step()
80+
with warmup_scheduler.dampening():
81+
lr_scheduler.step()
82+
83+
Note that the warmup schedule must not be initialized before the initialization of the learning rate schedule.
84+
Other approaches can be found in `README <https://github.com/Tony-Y/pytorch_warmup?tab=readme-ov-file#usage>`_.
85+
1386
.. toctree::
1487
:maxdepth: 2
1588
:caption: Contents:

examples/emnist/README.md

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# EMNIST Example
2+
3+
Requirements: `pytorch_warmup` and `torchvision`.
4+
5+
<p align="center">
6+
<img src="https://github.com/Tony-Y/pytorch_warmup/raw/master/examples/emnist/figs/accuracy.png" alt="Accuracy" width="400"/></br>
7+
<i>Test accuracy over time for each warmup schedule.</i>
8+
</p>
9+
10+
<p align="center">
11+
<img src="https://github.com/Tony-Y/pytorch_warmup/raw/master/examples/emnist/figs/learning_rate.png" alt="Accuracy" width="400"/></br>
12+
<i>Learning rate over time for each warmup schedule.</i>
13+
</p>
14+
15+
## Download EMNIST Dataset
16+
17+
Run the Python script `download.py` to download the EMNIST dataset:
18+
19+
```shell
20+
python download.py
21+
```
22+
23+
This script shows download progress:
24+
25+
```
26+
Downloading zip archive
27+
Downloading https://biometrics.nist.gov/cs_links/EMNIST/gzip.zip to .data/EMNIST/raw/gzip.zip
28+
100.0%
29+
```
30+
31+
## Train A CNN Model
32+
33+
Run the Python script `main.py` to train a CNN model on the EMNIST dataset using the Adam algorithm.
34+
35+
### Untuned Linear Warmup
36+
37+
Train a CNN model with the *Untuned Linear Warmup* schedule:
38+
39+
```
40+
python main.py --warmup linear
41+
```
42+
43+
### Untuned Exponential Warmup
44+
45+
Train a CNN model with the *Untuned Exponential Warmup* schedule:
46+
47+
```
48+
python main.py --warmup exponential
49+
```
50+
51+
### RAdam Warmup
52+
53+
Train a CNN model with the *RAdam Warmup* schedule:
54+
55+
```
56+
python main.py --warmup radam
57+
```
58+
59+
### No Warmup
60+
61+
Train a CNN model without warmup:
62+
63+
```
64+
python main.py --warmup none
65+
```
66+
67+
### Usage
68+
69+
```
70+
usage: main.py [-h] [--batch-size N] [--test-batch-size N] [--epochs N] [--lr LR]
71+
[--lr-min LM] [--wd WD] [--beta2 B2] [--no-cuda] [--seed S]
72+
[--log-interval N] [--warmup {linear,exponential,radam,none}] [--save-model]
73+
74+
PyTorch EMNIST Example
75+
76+
options:
77+
-h, --help show this help message and exit
78+
--batch-size N input batch size for training (default: 64)
79+
--test-batch-size N input batch size for testing (default: 1000)
80+
--epochs N number of epochs to train (default: 10)
81+
--lr LR base learning rate (default: 0.01)
82+
--lr-min LM minimum learning rate (default: 1e-5)
83+
--wd WD weight decay (default: 0.01)
84+
--beta2 B2 Adam's beta2 parameter (default: 0.999)
85+
--no-cuda disables CUDA training
86+
--seed S random seed (default: 1)
87+
--log-interval N how many batches to wait before logging training status
88+
--warmup {linear,exponential,radam,none}
89+
warmup schedule
90+
--save-model For Saving the current Model
91+
```
92+
93+
© 2024 Takenori Yamamoto

examples/plots/README.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Plots
2+
3+
Requirements: `pytorch_warmup` and `matplotlib`.
4+
5+
## Effective Warmup Period
6+
7+
<p align="center">
8+
<img src="https://github.com/Tony-Y/pytorch_warmup/raw/master/examples/plots/figs/warmup_period.png" alt="Warmup period" width="400"/></br>
9+
<i>Effective warmup periods of RAdam and rule-of-thumb warmup schedules, as a function of 𝛽₂.</i>
10+
</p>
11+
12+
Run the Python script `effective_warmup_period.py` to show up the figure above:
13+
14+
```shell
15+
python effective_warmup_period.py
16+
```
17+
18+
### Usage
19+
20+
```
21+
usage: effective_warmup_period.py [-h] [--output {none,png,pdf}]
22+
23+
Effective warmup period
24+
25+
options:
26+
-h, --help show this help message and exit
27+
--output {none,png,pdf}
28+
Output file type (default: none)
29+
```
30+
31+
## Warmup Schedule
32+
33+
<p align="center">
34+
<img src="https://github.com/Tony-Y/pytorch_warmup/raw/master/examples/plots/figs/warmup_schedule.png" alt="Warmup schedule" width="400"/></br>
35+
<i>RAdam and rule-of-thumb warmup schedules over time for 𝛽₂ = 0.999.</i>
36+
</p>
37+
38+
Run the Python script `warmup_schedule.py` to show up the figure above:
39+
40+
```shell
41+
python warmup_schedule.py
42+
```
43+
44+
### Usage
45+
46+
```
47+
usage: warmup_schedule.py [-h] [--output {none,png,pdf}]
48+
49+
Warmup schedule
50+
51+
options:
52+
-h, --help show this help message and exit
53+
--output {none,png,pdf}
54+
Output file type (default: none)
55+
```
56+
57+
© 2024 Takenori Yamamoto

0 commit comments

Comments
 (0)