# Tensorflow - calling a model inside a GradientTape scope VS calling it inside a loss function

Is there a difference in the gradient computation between the two code snippets ...

Code 1:

with tf.GradientTape() as tape:

output_A = model_A(input)
output_B = model_B(input)

loss = loss_fn(output_A, output_B, true_output_A, true_output_B)

# ------------------------------------------------------------------------
MSE = tf.keras.losses.MeanSquaredError()

def loss_fn(output_A, output_B, true_output_A, true_output_B)

loss = MSE(output_A, true_output_A) + MSE(output_B, true_output_B)

return loss



Code 2:

with tf.GradientTape() as tape:

output_A = model_A(input)

loss = loss_fn(output_A, model_B, input, true_output_A, true_output_B)

# ------------------------------------------------------------------------
MSE = tf.keras.losses.MeanSquaredError()

def loss_fn(output_A, model_B, input, true_output_A, true_output_B)

output_B = model_B(input)

loss = MSE(output_A, true_output_A) + MSE(output_B, true_output_B)

return loss



output_A and output_B are related using a mathematical equation .. I would like model_A to learn how to generate output_A using the way model_B generates output_B ..

I hope that makes sense ...

• This is the only official Tensorflow tutorial where they call the Model inside the Loss function .. but this doesn't seem to work in my case tensorflow.org/tutorials/customization/… Commented May 2, 2022 at 3:05