Менеджеры контекста могут изменить две две связанные операции в одну. Например:
with open('some_file', 'w') as opened_file:
opened_file.write('Hola!')
Вышеуказанный код эквивалентен:
file = open('some_file', 'w')
try:
file.write('Hola!')
finally:
file.close()
Но в https://www.tensorflow.org/tutorials/eager/custom_training_walkthrough#define_the_loss_and_gradient_function
Я нашел:
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)
что это эквивалентно?