Clears all information stored in this tape. Equivalent to exiting and reentering the tape context manager with a new tape. For example, the two following code blocks are equivalent: ``` with tf.GradientTape() as t: loss = loss_fn() with tf.GradientTape() as t: loss
(self)
| 893 | self._push_tape() |
| 894 | |
| 895 | def reset(self): |
| 896 | """Clears all information stored in this tape. |
| 897 | |
| 898 | Equivalent to exiting and reentering the tape context manager with a new |
| 899 | tape. For example, the two following code blocks are equivalent: |
| 900 | |
| 901 | ``` |
| 902 | with tf.GradientTape() as t: |
| 903 | loss = loss_fn() |
| 904 | with tf.GradientTape() as t: |
| 905 | loss += other_loss_fn() |
| 906 | t.gradient(loss, ...) # Only differentiates other_loss_fn, not loss_fn |
| 907 | |
| 908 | |
| 909 | # The following is equivalent to the above |
| 910 | with tf.GradientTape() as t: |
| 911 | loss = loss_fn() |
| 912 | t.reset() |
| 913 | loss += other_loss_fn() |
| 914 | t.gradient(loss, ...) # Only differentiates other_loss_fn, not loss_fn |
| 915 | ``` |
| 916 | |
| 917 | This is useful if you don't want to exit the context manager for the tape, |
| 918 | or can't because the desired reset point is inside a control flow construct: |
| 919 | |
| 920 | ``` |
| 921 | with tf.GradientTape() as t: |
| 922 | loss = ... |
| 923 | if loss > k: |
| 924 | t.reset() |
| 925 | ``` |
| 926 | """ |
| 927 | self._pop_tape() |
| 928 | self._tape = None |
| 929 | self._push_tape() |
| 930 | |
| 931 | def watched_variables(self): |
| 932 | """Returns variables watched by this tape in order of construction.""" |