关于TensorFlow的tensorflow.org教程展示了使用tf.GradientTape的方法:
x = tf.convert_to_tensor([1,2,3]);
with tf.GradientTape() as t:
t.watch(x); 我想知道为什么我不能像这样将t.watch(x)移动到with块之外:
x = tf.convert_to_tensor([1,2,3]);
t = tf.GradientTape();
t.watch(x); #ERROR错误是:
tape.py (59):
pywrap_tensorflow.TFE_Py_TapeWatch(tape._tape, tensor)
AttributeError: 'NoneType' object has no attribute '_tape'发布于 2019-09-26 06:25:01
我知道怎么做了。tf.GradientTape类设计为在with块内工作,即。它有enter和exit方法。
因此,为了让它在“with”块之外工作,必须显式地调用方法__enter__,但是,必须避免直接调用__enter__:
x = tf.convert_to_tensor([1,2,3]);
t = tf.GradientTape();
t.__enter__();
t.watch(x); https://stackoverflow.com/questions/58110537
复制相似问题