I have included the relevant portion of my code below for reference:
@tf.function
@tf.autograph.experimental.do_not_convert
def model_train(features, labels):
# Define the GradientTape context
with tf.GradientTape() as tape:
# Get the probabilities
predictions = mlp_cnn(features)
# Calculate the loss
loss = loss_func(labels, predictions)
# Get the gradients
trainable_vars = mlp_cnn.trainable_variables
gradients = tape.gradient(loss, trainable_vars)
# Update the weights
optimizer.apply_gradients(zip(gradients, trainable_vars))
train_loss(loss)
train_acc(labels, predictions)
the error message is:
Traceback (most recent call last):
File "G:\My Drive\ym\about_lab(20230213)\sarcopenia_ML\handover\Wan\code\DLR\dlrProject\test01.py", line 161, in <module>
model_train(features, labels)
File "C:\Users\SallyLin\Documents\PyPrograms\venv\odProject\lib\site-packages\tensorflow\python\util\traceback_utils.py", line 153, in error_handler
raise e.with_traceback(filtered_tb) from None
File "G:\My Drive\ym\about_lab(20230213)\sarcopenia_ML\handover\Wan\code\DLR\dlrProject\test01.py", line 138, in model_train
gradients = tape.gradient(loss, trainable_vars)
AttributeError: 'KerasTensor' object has no attribute '_id'
Best regards,
sally