|
265 | 265 | # |
266 | 266 | # Computational graph after backward pass |
267 | 267 | # |
268 | | -# If you call ``retain_grad()`` on a non-leaf node, it results in a no-op. |
269 | | -# If we call ``retain_grad()`` on a node that has ``requires_grad=False``, |
| 268 | +# If you call ``retain_grad()`` on a leaf tensor, it results in a no-op |
| 269 | +# since leaf tensors already retain their gradients by default (when |
| 270 | +# ``requires_grad=True``). |
| 271 | +# If we call ``retain_grad()`` on a tensor that has ``requires_grad=False``, |
270 | 272 | # PyTorch actually throws an error, since it can’t store the gradient if |
271 | 273 | # it is never calculated. |
272 | 274 | # |
|
298 | 300 | # +----------------+------------------------+------------------------+---------------------------------------------------+-------------------------------------+ |
299 | 301 | # | ``is_leaf`` | ``requires_grad`` | ``retains_grad`` | ``require_grad()`` | ``retain_grad()`` | |
300 | 302 | # +================+========================+========================+===================================================+=====================================+ |
301 | | -# | ``True`` | ``False`` | ``False`` | sets ``requires_grad`` to ``True`` or ``False`` | no-op | |
| 303 | +# | ``True`` | ``False`` | ``False`` | sets ``requires_grad`` to ``True`` or ``False`` | throws error | |
302 | 304 | # +----------------+------------------------+------------------------+---------------------------------------------------+-------------------------------------+ |
303 | | -# | ``True`` | ``True`` | ``False`` | sets ``requires_grad`` to ``True`` or ``False`` | no-op | |
| 305 | +# | ``True`` | ``True`` | ``False`` | sets ``requires_grad`` to ``True`` or ``False`` | no-op (already retains) | |
304 | 306 | # +----------------+------------------------+------------------------+---------------------------------------------------+-------------------------------------+ |
305 | 307 | # | ``False`` | ``True`` | ``False`` | no-op | sets ``retains_grad`` to ``True`` | |
306 | 308 | # +----------------+------------------------+------------------------+---------------------------------------------------+-------------------------------------+ |
307 | | -# | ``False`` | ``True`` | ``True`` | no-op | no-op | |
| 309 | +# | ``False`` | ``True`` | ``True`` | no-op | no-op (already retains) | |
308 | 310 | # +----------------+------------------------+------------------------+---------------------------------------------------+-------------------------------------+ |
309 | 311 | # |
310 | 312 |
|
|
0 commit comments