Skip to content

Commit 7a57b5c

Browse files
sidhantnagpalsoumith
authored andcommitted
Corrects the Jacobian matrix in Autograd tutorial (#426)
Fixes pytorch/pytorch#16352
1 parent 5fff874 commit 7a57b5c

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

beginner_source/blitz/autograd_tutorial.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,23 +114,23 @@
114114
#
115115
# .. math::
116116
# J=\left(\begin{array}{ccc}
117-
# \frac{\partial y_{1}}{\partial x_{1}} & \cdots & \frac{\partial y_{m}}{\partial x_{1}}\\
117+
# \frac{\partial y_{1}}{\partial x_{1}} & \cdots & \frac{\partial y_{1}}{\partial x_{n}}\\
118118
# \vdots & \ddots & \vdots\\
119-
# \frac{\partial y_{1}}{\partial x_{n}} & \cdots & \frac{\partial y_{m}}{\partial x_{n}}
119+
# \frac{\partial y_{m}}{\partial x_{1}} & \cdots & \frac{\partial y_{m}}{\partial x_{n}}
120120
# \end{array}\right)
121121
#
122122
# Generally speaking, ``torch.autograd`` is an engine for computing
123-
# Jacobian-vector product. That is, given any vector
123+
# vector-Jacobian product. That is, given any vector
124124
# :math:`v=\left(\begin{array}{cccc} v_{1} & v_{2} & \cdots & v_{m}\end{array}\right)^{T}`,
125-
# compute the product :math:`J\cdot v`. If :math:`v` happens to be
125+
# compute the product :math:`v^{T}\cdot J`. If :math:`v` happens to be
126126
# the gradient of a scalar function :math:`l=g\left(\vec{y}\right)`,
127127
# that is,
128128
# :math:`v=\left(\begin{array}{ccc}\frac{\partial l}{\partial y_{1}} & \cdots & \frac{\partial l}{\partial y_{m}}\end{array}\right)^{T}`,
129-
# then by the chain rule, the Jacobian-vector product would be the
129+
# then by the chain rule, the vector-Jacobian product would be the
130130
# gradient of :math:`l` with respect to :math:`\vec{x}`:
131131
#
132132
# .. math::
133-
# J\cdot v=\left(\begin{array}{ccc}
133+
# J^{T}\cdot v=\left(\begin{array}{ccc}
134134
# \frac{\partial y_{1}}{\partial x_{1}} & \cdots & \frac{\partial y_{m}}{\partial x_{1}}\\
135135
# \vdots & \ddots & \vdots\\
136136
# \frac{\partial y_{1}}{\partial x_{n}} & \cdots & \frac{\partial y_{m}}{\partial x_{n}}
@@ -144,12 +144,15 @@
144144
# \frac{\partial l}{\partial x_{n}}
145145
# \end{array}\right)
146146
#
147-
# This characteristic of Jacobian-vector product makes it very
147+
# (Note that :math:`v^{T}\cdot J` gives a row vector which can be
148+
# treated as a column vector by taking :math:`J^{T}\cdot v`.)
149+
#
150+
# This characteristic of vector-Jacobian product makes it very
148151
# convenient to feed external gradients into a model that has
149152
# non-scalar output.
150153

151154
###############################################################
152-
# Now let's take a look at an example of Jacobian-vector product:
155+
# Now let's take a look at an example of vector-Jacobian product:
153156

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

@@ -162,7 +165,7 @@
162165
###############################################################
163166
# Now in this case ``y`` is no longer a scalar. ``torch.autograd``
164167
# could not compute the full Jacobian directly, but if we just
165-
# want the Jacobian-vector product, simply pass the vector to
168+
# want the vector-Jacobian product, simply pass the vector to
166169
# ``backward`` as argument:
167170
v = torch.tensor([0.1, 1.0, 0.0001], dtype=torch.float)
168171
y.backward(v)

0 commit comments

Comments
 (0)