Tensorflow: Incompatible shapes: [1,896] vs. [1,32] [[{{node LogicalAnd_3}}]] [Op:__inference_one_step_on_iterator_6201]

36 Views Asked by At

I'm trying to build a model for classification with keras, and i'm have this error:

Incompatible shapes: [1,896] vs. [1,32]
     [[{{node LogicalAnd_3}}]] [Op:__inference_one_step_on_iterator_6201]

I tried this:

batch_size = 32
num_class=56
model = tf.keras.Sequential([
    tf.keras.layers.Dense(384, activation='relu'),
    tf.keras.layers.Dropout(.3),
    tf.keras.layers.Dense(192, activation='relu'),
    tf.keras.layers.Dropout(.3),
    tf.keras.layers.Dense(48, activation='relu'),
    tf.keras.layers.Dropout(.15),
    tf.keras.layers.Dense(num_class, activation='softmax')
])


model.compile(optimizer=Adam(learning_rate=0.001),
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy', tf.keras.metrics.Precision(), tf.keras.metrics.Recall(), tf.keras.metrics.F1Score(average="macro")])


class F1ScoreCallback(tf.keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs=None):
        print(logs.get('f1_score'))
        macro_f1 = logs.get('f1_score')
        if macro_f1 > 0.8:
            print(f"Macro F1-score atteint : {macro_f1:.4f}. Arrêt de l'entraînement.")
            self.model.stop_training = True
model.summary()

history = model.fit(X_train, Y_train, epochs=50,
                    batch_size=batch_size,
                    validation_split=0.2, callbacks=[F1ScoreCallback()])

0

There are 0 best solutions below