AttributeError: 'KerasTensor' object has no attribute '_id'

Skip to first unread message


Jun 7, 2023, 9:06:06 AM6/7/23
to Keras-users
Hi there,
I hope this email finds you well. I am writing to seek your assistance regarding an error I encountered while using tf.keras to build a model.
During the model training process, I utilized the tf.GradientTape() function. However, I kept encountering the following error: "AttributeError: 'KerasTensor' object has no attribute '_id'."

I have included the relevant portion of my code below for reference:


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_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\", line 161, in <module>
    model_train(features, labels)

  File "C:\Users\SallyLin\Documents\PyPrograms\venv\odProject\lib\site-packages\tensorflow\python\util\", 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\", line 138, in model_train
    gradients = tape.gradient(loss, trainable_vars)

AttributeError: 'KerasTensor' object has no attribute '_id'

Best regards,


Reply all
Reply to author
0 new messages