上下文管理器可以将两个两个相关的操作更改为一个。例如:
with open('some_file', 'w') as opened_file:
opened_file.write('Hola!')
上面的代码等效于:
file = open('some_file', 'w')
try:
file.write('Hola!')
finally:
file.close()
def grad(model, inputs, targets):
with tf.GradientTape() as tape:
loss_value = loss(model, inputs, targets)
return loss_value, tape.gradient(loss_value, model.trainable_variables)
它等同于什么?
答案 0 :(得分:1)
我不是python专家,但是我认为with是由__enter__
方法和__exit__
方法(https://book.pythontips.com/en/latest/context_managers.html)定义的。
对于tf.GradientTape方法,__enter__
是:
def __enter__(self):
"""Enters a context inside which operations are recorded on this tape."""
self._push_tape()
return self
https://github.com/tensorflow/tensorflow/blob/r2.0/tensorflow/python/eager/backprop.py#L801-L804
以及__exit__
方法
def __exit__(self, typ, value, traceback):
"""Exits the recording context, no further operations are traced."""
if self._recording:
self._pop_tape()
https://github.com/tensorflow/tensorflow/blob/r2.0/tensorflow/python/eager/backprop.py#L806-L809
然后
with tf.GradientTape() as tape:
loss_value = loss(model, inputs, targets)
是
tape = tf.GradientTape()
tape.push_tape()
loss_value = loss(model, inputs, targets)
self._pop_tape()