diff --git a/beginner_source/understanding_leaf_vs_nonleaf_tutorial.py b/beginner_source/understanding_leaf_vs_nonleaf_tutorial.py index 740c4d4bd7..6c8fa91a01 100644 --- a/beginner_source/understanding_leaf_vs_nonleaf_tutorial.py +++ b/beginner_source/understanding_leaf_vs_nonleaf_tutorial.py @@ -265,8 +265,10 @@ # # Computational graph after backward pass # -# If you call ``retain_grad()`` on a non-leaf node, it results in a no-op. -# If we call ``retain_grad()`` on a node that has ``requires_grad=False``, +# If you call ``retain_grad()`` on a leaf tensor, it results in a no-op +# since leaf tensors already retain their gradients by default (when +# ``requires_grad=True``). +# If we call ``retain_grad()`` on a tensor that has ``requires_grad=False``, # PyTorch actually throws an error, since it can’t store the gradient if # it is never calculated. # @@ -298,13 +300,13 @@ # +----------------+------------------------+------------------------+---------------------------------------------------+-------------------------------------+ # | ``is_leaf`` | ``requires_grad`` | ``retains_grad`` | ``require_grad()`` | ``retain_grad()`` | # +================+========================+========================+===================================================+=====================================+ -# | ``True`` | ``False`` | ``False`` | sets ``requires_grad`` to ``True`` or ``False`` | no-op | +# | ``True`` | ``False`` | ``False`` | sets ``requires_grad`` to ``True`` or ``False`` | throws error | # +----------------+------------------------+------------------------+---------------------------------------------------+-------------------------------------+ -# | ``True`` | ``True`` | ``False`` | sets ``requires_grad`` to ``True`` or ``False`` | no-op | +# | ``True`` | ``True`` | ``False`` | sets ``requires_grad`` to ``True`` or ``False`` | no-op (already retains) | # +----------------+------------------------+------------------------+---------------------------------------------------+-------------------------------------+ # | ``False`` | ``True`` | ``False`` | no-op | sets ``retains_grad`` to ``True`` | # +----------------+------------------------+------------------------+---------------------------------------------------+-------------------------------------+ -# | ``False`` | ``True`` | ``True`` | no-op | no-op | +# | ``False`` | ``True`` | ``True`` | no-op | no-op (already retains) | # +----------------+------------------------+------------------------+---------------------------------------------------+-------------------------------------+ #