Tensorflow:不兼容的形状:[1,896] 与 [1,32] [[{{node LogicalAnd_3}}]] [Op:__inference_one_step_on_iterator_6201]

问题描述 投票:0回答:1

我正在尝试使用 keras 构建分类模型,但出现此错误:

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

我试过这个:

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()])

tensorflow keras deep-learning classification cross-entropy
1个回答
0
投票

请提供有关输入数据 X_Train 和 Y_Train 及其形状的更多详细信息

对于像这样的简单模型,请避免使用多个 dropout 层。它可能会因为丢弃太多值而影响模型的性能。

© www.soinside.com 2019 - 2024. All rights reserved.