Skip to content

Commit c2afd4a

Browse files
authored
Merge branch 'main' into fix-neural_style_tutorial_weight_init
2 parents af3ef43 + f05f050 commit c2afd4a

14 files changed

+1397
-18
lines changed

.pyspelling.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ matrix:
1919
- open: '\.\.\s+(figure|literalinclude|math|image|grid)::'
2020
close: '\n'
2121
# Exclude roles:
22-
- open: ':(?:(class|py:mod|mod|func)):`'
22+
- open: ':(?:(class|py:mod|mod|func|meth|obj)):`'
2323
content: '[^`]*'
2424
close: '`'
2525
# Exclude reStructuredText hyperlinks
@@ -70,7 +70,7 @@ matrix:
7070
- open: ':figure:.*'
7171
close: '\n'
7272
# Ignore reStructuredText roles
73-
- open: ':(?:(class|file|func|math|ref|octicon)):`'
73+
- open: ':(?:(class|file|func|math|ref|octicon|meth|obj)):`'
7474
content: '[^`]*'
7575
close: '`'
7676
- open: ':width:'

_static/img/pendulum.gif

122 KB
Loading

_static/img/rollout_recurrent.png

338 KB
Loading

advanced_source/pendulum.py

Lines changed: 912 additions & 0 deletions
Large diffs are not rendered by default.

advanced_source/static_quantization_tutorial.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,15 @@ Note: this code is taken from
206206
207207
# Fuse Conv+BN and Conv+BN+Relu modules prior to quantization
208208
# This operation does not change the numerics
209-
def fuse_model(self):
209+
def fuse_model(self, is_qat=False):
210+
fuse_modules = torch.ao.quantization.fuse_modules_qat if is_qat else torch.ao.quantization.fuse_modules
210211
for m in self.modules():
211212
if type(m) == ConvBNReLU:
212-
torch.ao.quantization.fuse_modules(m, ['0', '1', '2'], inplace=True)
213+
fuse_modules(m, ['0', '1', '2'], inplace=True)
213214
if type(m) == InvertedResidual:
214215
for idx in range(len(m.conv)):
215216
if type(m.conv[idx]) == nn.Conv2d:
216-
torch.ao.quantization.fuse_modules(m.conv, [str(idx), str(idx + 1)], inplace=True)
217+
fuse_modules(m.conv, [str(idx), str(idx + 1)], inplace=True)
217218
218219
2. Helper functions
219220
-------------------
@@ -533,7 +534,7 @@ We fuse modules as before
533534
.. code:: python
534535
535536
qat_model = load_model(saved_model_dir + float_model_file)
536-
qat_model.fuse_model()
537+
qat_model.fuse_model(is_qat=True)
537538
538539
optimizer = torch.optim.SGD(qat_model.parameters(), lr = 0.0001)
539540
# The old 'fbgemm' is still available but 'x86' is the recommended default.

beginner_source/introyt/tensorboardyt_tutorial.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,14 @@ def forward(self, x):
214214
# Check against the validation set
215215
running_vloss = 0.0
216216

217-
net.train(False) # Don't need to track gradents for validation
217+
# In evaluation mode some model specific operations can be omitted eg. dropout layer
218+
net.train(False) # Switching to evaluation mode, eg. turning off regularisation
218219
for j, vdata in enumerate(validation_loader, 0):
219220
vinputs, vlabels = vdata
220221
voutputs = net(vinputs)
221222
vloss = criterion(voutputs, vlabels)
222223
running_vloss += vloss.item()
223-
net.train(True) # Turn gradients back on for training
224+
net.train(True) # Switching back to training mode, eg. turning on regularisation
224225

225226
avg_loss = running_loss / 1000
226227
avg_vloss = running_vloss / len(validation_loader)

beginner_source/transformer_tutorial.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
######################################################################
3131
# In this tutorial, we train a ``nn.TransformerEncoder`` model on a
32-
# language modeling task. Please note that this tutorial does not cover
32+
# causal language modeling task. Please note that this tutorial does not cover
3333
# the training of `nn.TransformerDecoder <https://pytorch.org/docs/stable/generated/torch.nn.TransformerDecoder.html#torch.nn.TransformerDecoder>`__, as depicted in
3434
# the right half of the diagram above. The language modeling task is to assign a
3535
# probability for the likelihood of a given word (or a sequence of words)
@@ -41,8 +41,10 @@
4141
# Along with the input sequence, a square attention mask is required because the
4242
# self-attention layers in ``nn.TransformerDecoder`` are only allowed to attend
4343
# the earlier positions in the sequence. For the language modeling task, any
44-
# tokens on the future positions should be masked. To produce a probability
45-
# distribution over output words, the output of the ``nn.TransformerEncoder``
44+
# tokens on the future positions should be masked. This masking, combined with fact that
45+
# the output embeddings are offset with later positions ensures that the
46+
# predictions for position i can depend only on the known outputs at positions less than i.
47+
# To produce a probability distribution over output words, the output of the ``nn.TransformerEncoder``
4648
# model is passed through a linear layer to output unnormalized logits.
4749
# The log-softmax function isn't applied here due to the later use of
4850
# `CrossEntropyLoss <https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html>`__,
@@ -91,6 +93,11 @@ def forward(self, src: Tensor, src_mask: Tensor = None) -> Tensor:
9193
"""
9294
src = self.embedding(src) * math.sqrt(self.d_model)
9395
src = self.pos_encoder(src)
96+
if src_mask is None:
97+
"""Generate a square causal mask for the sequence. The masked positions are filled with float('-inf').
98+
Unmasked positions are filled with float(0.0).
99+
"""
100+
src_mask = nn.Transformer.generate_square_subsequent_mask(len(src)).to(device)
94101
output = self.transformer_encoder(src, src_mask)
95102
output = self.linear(output)
96103
return output

en-wordlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Colab
6262
Conv
6363
ConvNet
6464
ConvNets
65+
customizable
6566
DCGAN
6667
DCGANs
6768
DDP

index.rst

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,26 @@ What's new in PyTorch tutorials?
312312
:link: intermediate/mario_rl_tutorial.html
313313
:tags: Reinforcement-Learning
314314

315+
.. customcarditem::
316+
:header: Recurrent DQN
317+
:card_description: Use TorchRL to train recurrent policies
318+
:image: _static/img/rollout_recurrent.png
319+
:link: intermediate/dqn_with_rnn_tutorial.html
320+
:tags: Reinforcement-Learning
321+
315322
.. customcarditem::
316323
:header: Code a DDPG Loss
317324
:card_description: Use TorchRL to code a DDPG Loss
318325
:image: _static/img/half_cheetah.gif
319326
:link: advanced/coding_ddpg.html
320327
:tags: Reinforcement-Learning
321328

322-
329+
.. customcarditem::
330+
:header: Writing your environment and transforms
331+
:card_description: Use TorchRL to code a Pendulum
332+
:image: _static/img/pendulum.gif
333+
:link: advanced/pendulum.html
334+
:tags: Reinforcement-Learning
323335

324336
.. Deploying PyTorch Models in Production
325337
@@ -951,6 +963,7 @@ Additional Resources
951963
intermediate/reinforcement_q_learning
952964
intermediate/reinforcement_ppo
953965
intermediate/mario_rl_tutorial
966+
advanced/pendulum
954967

955968
.. toctree::
956969
:maxdepth: 2

0 commit comments

Comments
 (0)