1

TensorFlow tutorials on tensorflow.org show the way to use tf.GradientTape as:

x = tf.convert_to_tensor([1,2,3]);
with tf.GradientTape() as t:
  t.watch(x); 

I wonder why I can't move the t.watch(x) outside of with block like this:

x = tf.convert_to_tensor([1,2,3]);
t = tf.GradientTape();
t.watch(x); #ERROR

The error is:

tape.py (59):
pywrap_tensorflow.TFE_Py_TapeWatch(tape._tape, tensor)

AttributeError: 'NoneType' object has no attribute '_tape'
Dee
  • 7,455
  • 6
  • 36
  • 70

1 Answers1

1

I found out how. The tf.GradientTape class is designed to work within with block, ie. it has enter and exit methods.

So to have it working outside 'with' block, method __enter__ must be explicitly called, however, avoid calling __enter__ directly:

x = tf.convert_to_tensor([1,2,3]);
t = tf.GradientTape();
t.__enter__();
t.watch(x); 

Reference: Explaining Python's '__enter__' and '__exit__'

Dee
  • 7,455
  • 6
  • 36
  • 70