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

36 views
Skip to first unread message

林菀純

unread,
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:

@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

Reply all
Reply to author
Forward
0 new messages