You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 94de252
Browse filesBrowse the repository at this point in the historyBrowse files
"\nAutograd: automatic differentiation\n===================================\n\nCentral to all neural networks in PyTorch is the ``autograd`` package.\nLet\u2019s first briefly visit this, and we will then go to training our\nfirst neural network.\n\n\nThe ``autograd`` package provides automatic differentiation for all operations\non Tensors. It is a define-by-run framework, which means that your backprop is\ndefined by how your code is run, and that every single iteration can be\ndifferent.\n\nLet us see this in more simple terms with some examples.\n\nVariable\n--------\n\n``autograd.Variable`` is the central class of the package. It wraps a\nTensor, and supports nearly all of operations defined on it. Once you\nfinish your computation you can call ``.backward()`` and have all the\ngradients computed automatically.\n\nYou can access the raw tensor through the ``.data`` attribute, while the\ngradient w.r.t. this variable is accumulated into ``.grad``.\n\n.. figure:: /_static/img/Variable.png\n :alt: Variable\n\n Variable\n\nThere\u2019s one more class which is very important for autograd\nimplementation - a ``Function``.\n\n``Variable`` and ``Function`` are interconnected and build up an acyclic\ngraph, that encodes a complete history of computation. Each variable has\na ``.grad_fn`` attribute that references a ``Function`` that has created\nthe ``Variable`` (except for Variables created by the user - their\n``grad_fn is None``).\n\nIf you want to compute the derivatives, you can call ``.backward()`` on\na ``Variable``. If ``Variable`` is a scalar (i.e. it holds a one element\ndata), you don\u2019t need to specify any arguments to ``backward()``,\nhowever if it has more elements, you need to specify a ``grad_output``\nargument that is a tensor of matching shape.\n\n"
"``y`` was created as a result of an operation, so it has a ``grad_fn``.\n\n"
73
-
]
92
+
],
93
+
"metadata": {},
94
+
"cell_type": "markdown"
74
95
},
75
96
{
76
-
"cell_type": "code",
77
97
"execution_count": null,
98
+
"source": [
99
+
"print(y.grad_fn)"
100
+
],
101
+
"outputs": [],
78
102
"metadata": {
79
103
"collapsed": false
80
104
},
81
-
"outputs": [],
82
-
"source": [
83
-
"print(y.grad_fn)"
84
-
]
105
+
"cell_type": "code"
85
106
},
86
107
{
87
-
"cell_type": "markdown",
88
-
"metadata": {},
89
108
"source": [
90
109
"Do more operations on y\n\n"
91
-
]
110
+
],
111
+
"metadata": {},
112
+
"cell_type": "markdown"
92
113
},
93
114
{
94
-
"cell_type": "code",
95
115
"execution_count": null,
116
+
"source": [
117
+
"z = y * y * 3\nout = z.mean()\n\nprint(z, out)"
118
+
],
119
+
"outputs": [],
96
120
"metadata": {
97
121
"collapsed": false
98
122
},
99
-
"outputs": [],
100
-
"source": [
101
-
"z = y * y * 3\nout = z.mean()\n\nprint(z, out)"
102
-
]
123
+
"cell_type": "code"
103
124
},
104
125
{
105
-
"cell_type": "markdown",
106
-
"metadata": {},
107
126
"source": [
108
127
"Gradients\n---------\nlet's backprop now\n``out.backward()`` is equivalent to doing ``out.backward(torch.Tensor([1.0]))``\n\n"
109
-
]
128
+
],
129
+
"metadata": {},
130
+
"cell_type": "markdown"
110
131
},
111
132
{
112
-
"cell_type": "code",
113
133
"execution_count": null,
134
+
"source": [
135
+
"out.backward()"
136
+
],
137
+
"outputs": [],
114
138
"metadata": {
115
139
"collapsed": false
116
140
},
117
-
"outputs": [],
118
-
"source": [
119
-
"out.backward()"
120
-
]
141
+
"cell_type": "code"
121
142
},
122
143
{
123
-
"cell_type": "markdown",
124
-
"metadata": {},
125
144
"source": [
126
145
"print gradients d(out)/dx\n\n\n"
127
-
]
146
+
],
147
+
"metadata": {},
148
+
"cell_type": "markdown"
128
149
},
129
150
{
130
-
"cell_type": "code",
131
151
"execution_count": null,
152
+
"source": [
153
+
"print(x.grad)"
154
+
],
155
+
"outputs": [],
132
156
"metadata": {
133
157
"collapsed": false
134
158
},
135
-
"outputs": [],
136
-
"source": [
137
-
"print(x.grad)"
138
-
]
159
+
"cell_type": "code"
139
160
},
140
161
{
141
-
"cell_type": "markdown",
142
-
"metadata": {},
143
162
"source": [
144
163
"You should have got a matrix of ``4.5``. Let\u2019s call the ``out``\n*Variable* \u201c$o$\u201d.\nWe have that $o = \\frac{1}{4}\\sum_i z_i$,\n$z_i = 3(x_i+2)^2$ and $z_i\\bigr\\rvert_{x_i=1} = 27$.\nTherefore,\n$\\frac{\\partial o}{\\partial x_i} = \\frac{3}{2}(x_i+2)$, hence\n$\\frac{\\partial o}{\\partial x_i}\\bigr\\rvert_{x_i=1} = \\frac{9}{2} = 4.5$.\n\n"
145
-
]
164
+
],
165
+
"metadata": {},
166
+
"cell_type": "markdown"
146
167
},
147
168
{
148
-
"cell_type": "markdown",
149
-
"metadata": {},
150
169
"source": [
151
170
"You can do many crazy things with autograd!\n\n"
152
-
]
171
+
],
172
+
"metadata": {},
173
+
"cell_type": "markdown"
153
174
},
154
175
{
155
-
"cell_type": "code",
156
176
"execution_count": null,
177
+
"source": [
178
+
"x = torch.randn(3)\nx = Variable(x, requires_grad=True)\n\ny = x * 2\nwhile y.data.norm() < 1000:\n y = y * 2\n\nprint(y)"
179
+
],
180
+
"outputs": [],
157
181
"metadata": {
158
182
"collapsed": false
159
183
},
160
-
"outputs": [],
161
-
"source": [
162
-
"x = torch.randn(3)\nx = Variable(x, requires_grad=True)\n\ny = x * 2\nwhile y.data.norm() < 1000:\n y = y * 2\n\nprint(y)"
0 commit comments