Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
1fc2cfa
Add doc for TBPTT
Nov 15, 2024
efae604
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 15, 2024
24bf319
Merge branch 'master' into chualan/add-example-for-TBPTT
chualanagit Nov 15, 2024
b296ec0
remove url to prevent linting error
Nov 16, 2024
7f9673d
attempt to fix linter
Nov 16, 2024
aca0046
Merge branch 'master' into chualan/add-example-for-TBPTT
chualanagit Nov 18, 2024
4e37ab9
Merge branch 'master' into chualan/add-example-for-TBPTT
lantiga Nov 19, 2024
dce88a6
Merge branch 'master' into chualan/add-example-for-TBPTT
chualanagit Nov 25, 2024
54c89a8
add tbptt.rst file
Nov 25, 2024
5c367c5
adjust doc:
Nov 25, 2024
610809c
nit
Nov 25, 2024
48dbdd2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 25, 2024
6f7f206
Merge branch 'master' into chualan/add-example-for-TBPTT
chualanagit Nov 26, 2024
5262534
make example easily copy and runnable
Nov 26, 2024
ce9790e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 26, 2024
bbf7c12
Merge branch 'master' into chualan/add-example-for-TBPTT
chualanagit Nov 26, 2024
ef2a826
address comments
Nov 26, 2024
5709003
Merge branch 'master' into chualan/add-example-for-TBPTT
chualanagit Nov 26, 2024
3feec73
Merge branch 'master' into chualan/add-example-for-TBPTT
chualanagit Dec 9, 2024
8aee16a
fix doc test warning
Dec 9, 2024
0d2a384
Update docs/source-pytorch/common/tbptt.rst
lantiga Dec 10, 2024
bbf2a3f
Merge branch 'master' into chualan/add-example-for-TBPTT
lantiga Dec 10, 2024
66072cc
Merge branch 'master' into chualan/add-example-for-TBPTT
lantiga Dec 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/source-pytorch/common/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,13 @@ How-to Guides
:col_css: col-md-4
:height: 180

.. displayitem::
:header: Truncated Back-Propagation Through Time
:description: Efficiently step through time when training recurrent models
:button_link: ../common/tbtt.html
:col_css: col-md-4
:height: 180

.. raw:: html

</div>
Expand Down
59 changes: 59 additions & 0 deletions docs/source-pytorch/common/tbptt.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
##############################################
Truncated Backpropagation Through Time (TBPTT)
##############################################

Truncated Backpropagation Through Time (TBPTT) performs backpropogation every k steps of
a much longer sequence. This is made possible by passing training batches
split along the time-dimensions into splits of size k to the
``training_step``. In order to keep the same forward propagation behavior, all
hidden states should be kept in-between each time-dimension split.


.. code-block:: python

import torch
import torch.optim as optim
import pytorch_lightning as pl
from pytorch_lightning import LightningModule

class LitModel(LightningModule):

def __init__(self):
super().__init__()

# 1. Switch to manual optimization
self.automatic_optimization = False

self.truncated_bptt_steps = 10
self.my_rnn = ParityModuleRNN() # Define RNN model using ParityModuleRNN

# 2. Remove the `hiddens` argument
def training_step(self, batch, batch_idx):

# 3. Split the batch in chunks along the time dimension
split_batches = split_batch(batch, self.truncated_bptt_steps)

batch_size = 10
hidden_dim = 20
hiddens = torch.zeros(1, batch_size, hidden_dim, device=self.device)
for split_batch in range(split_batches):
# 4. Perform the optimization in a loop
loss, hiddens = self.my_rnn(split_batch, hiddens)
self.backward(loss)
self.optimizer.step()
self.optimizer.zero_grad()

# 5. "Truncate"
hiddens = hiddens.detach()

# 6. Remove the return of `hiddens`
# Returning loss in manual optimization is not needed
return None

def configure_optimizers(self):
return optim.Adam(self.my_rnn.parameters(), lr=0.001)

if __name__ == "__main__":
model = LitModel()
trainer = pl.Trainer(max_epochs=5)
trainer.fit(model, train_dataloader) # Define your own dataloader
Loading