[Python] Using a symbolic `tf.Tensor` as a Python `bool` is not allowed

Discussão em 'Python' iniciado por Stack, Fevereiro 5, 2025.

  1. Stack

    Stack Membro Participativo

    when I add a masking layer in my model, it gives this error. works fine without the masking tho. If anyone can solve this, id be grateful ! been scratching my head for 3 days now. TensorFlow 2.17

    def build_lstm(examples):
    print("Building Model")
    X_train = examples[:, :-1, :]
    y_train = examples[:, 1:, :]
    model = Sequential()
    model.add(Masking(mask_value=0.0, input_shape=(X_train.shape[1], X_train.shape[2])))
    model.add(LSTM(128, return_sequences=True, kernel_regularizer=l2(0.001), kernel_initializer=GlorotUniform()))
    model.add(LayerNormalization())
    model.add(Dropout(0.1))
    model.add(LSTM(256, return_sequences=True, kernel_regularizer=l2(0.001)))
    model.add(Dropout(0.1))
    #model.add(Lambda(lambda x: x, mask=None))
    model.add(TimeDistributed(Dense(y_train.shape[2], kernel_regularizer=l2(0.001))))
    #adadelta
    scheduler = ReduceLROnPlateau(monitor='loss', factor=0.5, patience=5, verbose=1)
    early_stopping = EarlyStopping(monitor='loss', patience=5, restore_best_weights=True)
    model.compile(loss=MeanSquaredError(), optimizer=Adam(learning_rate=0.00001, clipvalue=1.0),metrics=[MeanSquaredError(), MeanAbsoluteError(),r2_metric,'RootMeanSquaredError'])
    print("Training Model")
    history = model.fit(X_train, y_train, batch_size=64, epochs=30, callbacks=[scheduler, early_stopping])
    print("Training complete. Returning Model")
    return model, history


    Here is the complete error:

    OperatorNotAllowedInGraphError: Exception encountered when calling TimeDistributed.call().

    Using a symbolic `tf.Tensor` as a Python `bool` is not allowed. You can attempt the following resolutions to the problem: If you are running in Graph mode, use Eager execution mode or decorate this function with @tf.function. If you are using AutoGraph, you can try decorating this function with @tf.function. If that does not work, then you may be using an unsupported feature or your source code may not be visible to AutoGraph. See https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/autograph/g3doc/reference/limitations.md#access-to-source-code for more information.

    Arguments received by TimeDistributed.call():
    • inputs=tf.Tensor(shape=(None, 13, 256), dtype=float32)
    • training=True
    • mask=tf.Tensor(shape=(None, 13), dtype=bool)

    Continue reading...

Compartilhe esta Página