Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
662f20e
60 min blitz What is PyTorch
CamiWilliams Mar 16, 2020
667dce6
Addressing comments
CamiWilliams Mar 17, 2020
e9c637f
Create recipes
jspisak Mar 19, 2020
1b42103
Delete recipes
jspisak Mar 19, 2020
3dd2843
Merge pull request #889 from CamiWilliams/60minblitz-cw-update
Mar 19, 2020
e13f437
Merge pull request #898 from pytorch/master
brianjo Mar 19, 2020
2e21606
Add recipes directory
Mar 19, 2020
96bea9f
Add example recipe. Remove built out recipe folder.
Mar 20, 2020
fead120
Add files via upload
jspisak Mar 20, 2020
e9b56c6
Add files via upload
jspisak Mar 20, 2020
9e0ef06
60 min blitz autograd
CamiWilliams Mar 25, 2020
2e58196
Delete custom_dataset.ipynb
jspisak Mar 26, 2020
732401b
Add files via upload
jspisak Mar 26, 2020
6474227
Add files via upload
jspisak Mar 26, 2020
4a6f291
Update recipes_index with the Tutorials (Old) converted to recipes
jlin27 Mar 30, 2020
95015f0
Correct links in some of the cards
jlin27 Mar 31, 2020
b4d3dee
Update the READM.md with all recipes and add in tutorials(old) that a…
jlin27 Mar 31, 2020
677e93b
Correct information for Writing Custom Data Transformations recipe
Mar 31, 2020
e5bd72d
grad_fn elaboration
CamiWilliams Mar 31, 2020
126fca1
remove extra line
CamiWilliams Mar 31, 2020
0796258
Add files via upload
jspisak Mar 31, 2020
155c2c4
Add "Extending TorchScript with Custom C++ Classes" tutorial
jlin27 Mar 31, 2020
5108c9b
Add "Extending TorchScript with Custom C++ Classs" tutorial
Mar 31, 2020
e0f14da
Delete custom_dataset_transforms_loader.ipynb
jspisak Mar 31, 2020
d1e0ad1
Delete custom_dataset.ipynb
jspisak Mar 31, 2020
9723447
Delete custom_transforms.ipynb
jspisak Mar 31, 2020
b303d65
Add files via upload
jspisak Mar 31, 2020
5be1c73
Add in "Extending TorchScript with Custom C++ Classes" tutorial
Mar 31, 2020
0810689
Correct indentations on customcarditem
Mar 31, 2020
a516697
Add "Extending TorchScript with Custom C++ Classes" tutorial
jlin27 Mar 31, 2020
8f0bb27
1.5 Updates to TorchScript tutorial
Mar 31, 2020
774ebdf
Add "Getting Started with Distributed RPC Framework" tutorial
Mar 31, 2020
72dc1f5
Add "Getting Started with Distributed RPC Framework" tutorial
jlin27 Mar 31, 2020
0c0d183
Add recipes, clean up ordering, correct naming
Mar 31, 2020
1ba3cad
Merge pull request #908 from CamiWilliams/60minblitz-cw-update
Apr 1, 2020
c1563de
Add files via upload
jspisak Apr 1, 2020
232ebe1
Remove extra done from Step 2
Apr 6, 2020
3c68894
Defining a neural network recipe
CamiWilliams Apr 8, 2020
c3e28f2
Zeroing out gradients recipe
CamiWilliams Apr 8, 2020
ee060a3
what is state dict recipe
CamiWilliams Apr 8, 2020
d4acd99
Merge branch 'master' into tutorials_refresh
Apr 8, 2020
ef25345
Saving and loading models for inference
CamiWilliams Apr 8, 2020
4031e3c
Saving and loading a general checkpoint
CamiWilliams Apr 8, 2020
8b8849c
Saving multiple models in pytorch
CamiWilliams Apr 9, 2020
161878f
Warmstarting a model using parameters from a different model recipe
CamiWilliams Apr 9, 2020
c6a6eb2
TorchScript Inference recipe initial commit
fbbradheintz Apr 9, 2020
e77e726
updated according to script
CamiWilliams Apr 13, 2020
b3cac80
updated according to script
CamiWilliams Apr 13, 2020
9128f54
updated according to script
CamiWilliams Apr 13, 2020
f188fd7
updated according to script
CamiWilliams Apr 13, 2020
d90015f
updated according to script
CamiWilliams Apr 13, 2020
95f9b58
updated according to script
CamiWilliams Apr 13, 2020
488973a
updated according to script
CamiWilliams Apr 13, 2020
e32812c
Initial commit of py generated from ipynb through script
CamiWilliams Apr 13, 2020
77ed222
Some minor syntax adjustments
jlin27 Apr 17, 2020
33bd051
Update with syntax changes
jlin27 Apr 17, 2020
b6a14af
draft for flask deployment tutorial
fbbradheintz Apr 20, 2020
b1cce2e
Merge pull request #933 from CamiWilliams/basics-recipe-definenetwork
Apr 20, 2020
d33d0e1
Merge pull request #935 from CamiWilliams/basics-recipe-statedict
Apr 20, 2020
0e77e73
Merge pull request #944 from fbbradheintz/tutorials_refresh
Apr 20, 2020
a69eb7e
Edit some code and heading syntax
jlin27 Apr 20, 2020
564b5c5
Merge pull request #936 from CamiWilliams/basics-recipe-saveloadinfer…
Apr 20, 2020
594f581
Some syntax changes for grammar and heading consistency
jlin27 Apr 20, 2020
de0b952
Merge pull request #938 from CamiWilliams/basics-recipe-saveloadcheck…
Apr 20, 2020
767b9a8
Some syntax edits for build and headers
jlin27 Apr 20, 2020
b4b21ef
Merge pull request #939 from CamiWilliams/basics-recipe-multiplesaveload
Apr 20, 2020
c793ed6
Some syntax edits for build and headings
Apr 20, 2020
0e020a0
Merge pull request #943 from CamiWilliams/basics-recipe-warmstart
Apr 20, 2020
961624d
Some syntax edits for build and headers
Apr 20, 2020
c2d76d2
Merge pull request #949 from CamiWilliams/basics-recipe-acrossdevices
Apr 20, 2020
7b3d5c0
Syntax edits for build and header
Apr 20, 2020
e9e5776
Merge pull request #934 from CamiWilliams/basics-recipe-zerogradients
Apr 20, 2020
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
10 changes: 10 additions & 0 deletions .jenkins/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ if [[ "${JOB_BASE_NAME}" == *worker_* ]]; then
FILES_TO_RUN+=($(basename $filename .py))
fi
count=$((count+1))
done
for filename in $(find recipes_source/ -name '*.py' -not -path '*/data/*'); do
if [ $(($count % $NUM_WORKERS)) != $WORKER_ID ]; then
echo "Removing runnable code from "$filename
python $DIR/remove_runnable_code.py $filename $filename
else
echo "Keeping "$filename
FILES_TO_RUN+=($(basename $filename .py))
fi
count=$((count+1))
done
echo "FILES_TO_RUN: " ${FILES_TO_RUN[@]}

Expand Down
158 changes: 105 additions & 53 deletions beginner_source/blitz/autograd_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,78 +3,114 @@
Autograd: Automatic Differentiation
===================================

Central to all neural networks in PyTorch is the ``autograd`` package.
Let’s first briefly visit this, and we will then go to training our
first neural network.
Deep learning uses artificial neural networks, which are computing systems
composed of many layers of interconnected units. By passing data through these
interconnected units, a neural network, or model, is able to learn how to
approximate the computations required to transform that data input into some
output. We will learn how to fully construct neural networks in the next section
of this tutorial.

Before we do so, it is important that we introduce some concepts.

The ``autograd`` package provides automatic differentiation for all operations
on Tensors. It is a define-by-run framework, which means that your backprop is
defined by how your code is run, and that every single iteration can be
different.
In the training phase, models are able to increase their accuracy through gradient decent.
In short, gradient descent is the process of minimizing our loss (or error) by tweaking the
weights and biases in our model.

Let us see this in more simple terms with some examples.
This process introduces the concept "automatic differentiation". Automatic differentiation
helps us calculate the derivatives of our loss function to know how much we should
adjust those weights and biases to decrease loss. There are various methods on how to
perform automatic differentiation, one of the most popular being backpropagation (finding the
loss based on the previous EPOCH, or iteration).

In the ``autograd`` package in PyTorch, we have access to automatic differentiation for
all operations on tensors. It is a define-by-run framework, which means, for example,
that your backpropagation is defined by how your code is run and that every iteration can be different.


Getting Started
---------------

This entire process is simplified using PyTorch. In this section of the tutorial, we will
see examples of gradient descent, automatic differentiation, and backpropagation in PyTorch.
This will which will introduce to training our first neural network in the following section.

Tensor
--------
^^^^^^^

``torch.Tensor`` is the central class of the package. If you set its attribute
``.requires_grad`` as ``True``, it starts to track all operations on it. When
you finish your computation you can call ``.backward()`` and have all the
gradients computed automatically. The gradient for this tensor will be
accumulated into ``.grad`` attribute.
``torch.Tensor`` is the central class of PyTorch. When you create a tensor,
if you set its attribute ``.requires_grad`` as ``True``, the package tracks
all operations on it. When your computation is finished, you can then call
``.backward()`` on the tensor, and have all the gradients computed automatically.
The gradient for this tensor will be accumulated into ``.grad`` attribute.

To stop a tensor from tracking history, you can call ``.detach()`` to detach
it from the computation history, and to prevent future computation from being
tracked.

To prevent tracking history (and using memory), you can also wrap the code block
in ``with torch.no_grad():``. This can be particularly helpful when evaluating a
model because the model may have trainable parameters with
``requires_grad=True``, but for which we don't need the gradients.

There’s one more class which is very important for autograd
implementation - a ``Function``.

``Tensor`` and ``Function`` are interconnected and build up an acyclic
graph, that encodes a complete history of computation. Each tensor has
a ``.grad_fn`` attribute that references a ``Function`` that has created
the ``Tensor`` (except for Tensors created by the user - their
``grad_fn is None``).

If you want to compute the derivatives, you can call ``.backward()`` on
a ``Tensor``. If ``Tensor`` is a scalar (i.e. it holds a one element
data), you don’t need to specify any arguments to ``backward()``,
however if it has more elements, you need to specify a ``gradient``
argument that is a tensor of matching shape.
model, where a it may have trainable parameters with
``requires_grad=True``, but whose gradients are not necessary.

Before we dive into code, there’s one more very important class when implementing
``autograd`` - a ``Function``.

``Tensor`` and ``Function`` are interconnected. Together, they build an acyclic
graph that encodes a complete history of computation. Programmatically, this means
that each tensor has a ``.grad_fn`` attribute, referencing a ``Function`` that created
the tensor (except for when tensors are explicitly created by the developer - their
``grad_fn`` is ``None``).

Let's see this in action. As always, we will first need to import PyTorch into our program.

"""

import torch

###############################################################
# Create a tensor and set ``requires_grad=True`` to track computation with it
# Create a tensor and set ``requires_grad=True``. This will track all operations on the tensor.

x = torch.ones(2, 2, requires_grad=True)
print(x)

###############################################################
# Do a tensor operation:
# Now, perform a simple tensor operation:

y = x + 2
print(y)

###############################################################
# ``y`` was created as a result of an operation, so it has a ``grad_fn``.
# ``grad_fn`` points to the last operation performed on the tensor.
# In this case it is an addition function, so its value is ``AddBackward0``.

print(y.grad_fn)

###############################################################
# Do more operations on ``y``
# Perform more operations on ``y``:

z = y * y * 3
out = z.mean()

print(z, out)

################################################################
# ``.requires_grad_( ... )`` changes an existing Tensor's ``requires_grad``
# flag in-place. The input flag defaults to ``False`` if not given.
# Notice how the output of ``z`` has a gradient function of ``MulBackward0`` and
# the output of ``out`` has ``MeanBackward0``. These outputs give us insight to
# the history of the tensor, where ``MulBackward`` indicates that a multiplication
# operation was performed, and ``MeanBackward`` indicates the mean was calculated
# previously on this tensor.
#
# Now if we perform a multiplication operation on ``z`` again:

print (z.median())

################################################################
# You can see that the ``grad_fn`` was updated to ``MedianBackward``.
#
# You can change the ``requires_grad`` flag in place using the
# ``.requires_grad_( ... )`` function.
# The input flag defaults to ``False`` if not manually specified.

a = torch.randn(2, 2)
a = ((a * 3) / (a - 1))
print(a.requires_grad)
Expand All @@ -86,20 +122,31 @@
###############################################################
# Gradients
# ---------
# Let's backprop now.
# Because ``out`` contains a single scalar, ``out.backward()`` is
# equivalent to ``out.backward(torch.tensor(1.))``.
# Now that we understand how these operations work with tensors, let's practice with
# backpropagation in PyTorch, using ``.backward()``.
#
# To compute the derivative, you can call ``.backward()`` on
# a ``tensor``. If ``tensor`` is a scalar (i.e. it holds a one element
# data), you don’t need to specify any arguments to ``backward()``. If it has more elements,
# you need to specify a ``gradient`` argument, which will be a tensor of matching shape.
#
# ``loss.backward()`` computes the derivative of the loss (``dloss/dx``) for every
# parameter ``x`` where ``requires_grad = True``. These are then accumulated into ``x.grad``.
#
# In our examples, ``out`` is the loss for ``x``. Because ``out`` contains a single scalar,
# ``out.backward()`` is equivalent to ``out.backward(torch.tensor(1.))``.

print (x)
print (out)
out.backward()

###############################################################
# Print gradients d(out)/dx
#

# Print the gradients d(out)/dx
print(x.grad)

###############################################################
# You should have got a matrix of ``4.5``. Let’s call the ``out``
# Let's break this down mathematically for further understanding.
#
# You should have got a matrix of ``4.5``. Let’s call ``out``
# *Tensor* “:math:`o`”.
# We have that :math:`o = \frac{1}{4}\sum_i z_i`,
# :math:`z_i = 3(x_i+2)^2` and :math:`z_i\bigr\rvert_{x_i=1} = 27`.
Expand All @@ -108,7 +155,7 @@
# :math:`\frac{\partial o}{\partial x_i}\bigr\rvert_{x_i=1} = \frac{9}{2} = 4.5`.

###############################################################
# Mathematically, if you have a vector valued function :math:`\vec{y}=f(\vec{x})`,
# If you have a vector valued function :math:`\vec{y}=f(\vec{x})`,
# then the gradient of :math:`\vec{y}` with respect to :math:`\vec{x}`
# is a Jacobian matrix:
#
Expand Down Expand Up @@ -152,7 +199,7 @@
# non-scalar output.

###############################################################
# Now let's take a look at an example of vector-Jacobian product:
# Check out this example to see vector-Jacobian product in practice:

x = torch.randn(3, requires_grad=True)

Expand All @@ -167,32 +214,37 @@
# could not compute the full Jacobian directly, but if we just
# want the vector-Jacobian product, simply pass the vector to
# ``backward`` as argument:

v = torch.tensor([0.1, 1.0, 0.0001], dtype=torch.float)
y.backward(v)

print(x.grad)

###############################################################
# You can also stop autograd from tracking history on Tensors
# with ``.requires_grad=True`` either by wrapping the code block in
# As mentioned previously, you can stop ``autograd`` from tracking history on tensors
# (via ``.requires_grad=True``) either by wrapping the code block in
# ``with torch.no_grad():``

print(x.requires_grad)
print((x ** 2).requires_grad)

with torch.no_grad():
print((x ** 2).requires_grad)

###############################################################
# Or by using ``.detach()`` to get a new Tensor with the same
# content but that does not require gradients:
# Or by using ``.detach()``, which yields a new tensor with the same
# content that does not require gradients:

print(x.requires_grad)
y = x.detach()
print(y.requires_grad)
print(x.eq(y).all())


###############################################################
# With this understanding of how ``autograd`` works in PyTorch, let's move on to
# the next section to construct our neural networks.
#
# **Read Later:**
#
# Document about ``autograd.Function`` is at
# For more information about ``autograd.Function``, check out our documentation:
# https://pytorch.org/docs/stable/autograd.html#function
Loading