Tensorflow ValueError: Shapes (64, 1) and (1, 1) are incompatible

Tensorflow ValueError: Shapes (64, 1) and (1, 1) are incompatible

我正在尝试构建一个连体神经网络来分析 MNIST 数据集,但是在尝试将模型拟合到数据集时,我遇到了这个问题,据此我的训练数据和标签形状不匹配。我尝试更改损失函数并尝试压缩标签数组,但“解决方案”均无效。

这是训练和标签数组的形状:

pairTrain shape: (120000, 2, 28, 28, 1)
labelTrain shape: (120000, 1)

这是我的模型:

def build_model(input_shape, embedDim=48):
    inputs = Input(input_shape)

    x = Conv2D(64, (2, 2), padding="same", activation="relu", input_shape=input_shape)(inputs)
    x = MaxPooling2D()(x)
    x = Dropout(0.3)(x)

    x = Conv2D(32, (2, 2), padding="same", activation="relu")(x)
    x = MaxPooling2D()(x)
    x = Dropout(0.3)(x)

    x = Conv2D(16, (2, 2), padding="same", activation="relu")(x)
    x = MaxPooling2D()(x)
    x = Dropout(0.3)(x)

    outputs = Flatten()(x)
    outputs = Dense(embedDim)(outputs)

    model = Model(inputs, outputs)

    return model

最后是生成错误的代码:


imgA = Input(shape=(28, 28, 1))
imgB = Input(shape=(28, 28, 1))

featA = build_model((28, 28, 1))(imgA)
featB = build_model((28, 28, 1))(imgB)

distance = Lambda(euclidean_distance)([featA, featB])
output = Dense(1, activation="sigmoid")(distance)

model = Model([imgA, imgB], output)

model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
history = model.fit(
    [pairTrain[:, 0], pairTrain[:, 1]], labelTrain,
    validation_data=[[pairTest[:, 0], pairTest[:, 1]], labelTest],
    batch_size=64,
    epochs=10
)

model.save("output/siamese_model")

请帮我解决问题。

我无法使用以下代码重现错误。我怀疑您的标签形状与您报告的不同,或者它不包含严格的二进制数据(0 和 1)。

此外,您应该使用 tf.keras.losses.BinaryCrossentropy 而不是 tf.keras.losses.CategoricalCrossentropy,因为您的标签应该是二进制的,并在最后一层激活 sigmoid

from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Input, Flatten, Dense, Lambda
from tensorflow.keras.models import Model
import tensorflow as tf

def build_model(input_shape, embedDim=48):
    inputs = Input(input_shape)

    x = Conv2D(64, (2, 2), padding="same", activation="relu", input_shape=input_shape)(inputs)
    x = MaxPooling2D()(x)
    x = Dropout(0.3)(x)

    x = Conv2D(32, (2, 2), padding="same", activation="relu")(x)
    x = MaxPooling2D()(x)
    x = Dropout(0.3)(x)

    x = Conv2D(16, (2, 2), padding="same", activation="relu")(x)
    x = MaxPooling2D()(x)
    x = Dropout(0.3)(x)

    outputs = Flatten()(x)
    outputs = Dense(embedDim)(outputs)

    model = Model(inputs, outputs)

    return model

imgA = Input(shape=(28, 28, 1))
imgB = Input(shape=(28, 28, 1))

featA = build_model((28, 28, 1))(imgA)
featB = build_model((28, 28, 1))(imgB)

distance = Lambda(lambda x: x[0]-x[1])([featA, featB])
output = Dense(1, activation="sigmoid")(distance)

model = Model([imgA, imgB], output)

pairTrain =  tf.random.uniform((10, 2, 28, 28, 1))
labelTrain = tf.random.uniform(shape=(10, 1), minval=0, maxval=2, dtype=tf.int32)

pairTest =  tf.random.uniform((10, 2, 28, 28, 1))
labelTest = tf.random.uniform(shape=(10, 1), minval=0, maxval=2, dtype=tf.int32)

model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
history = model.fit(
    [pairTrain[:, 0], pairTrain[:, 1]], labelTrain,
    validation_data=[[pairTest[:, 0], pairTest[:, 1]], labelTest],
    batch_size=64,
    epochs=10
)

model.save("output/siamese_model")
Epoch 1/10
1/1 [==============================] - 2s 2s/step - loss: 0.7061 - accuracy: 0.5000 - val_loss: 0.6862 - val_accuracy: 0.7000
Epoch 2/10
1/1 [==============================] - 0s 80ms/step - loss: 0.7882 - accuracy: 0.4000 - val_loss: 0.6751 - val_accuracy: 0.6000
Epoch 3/10
1/1 [==============================] - 0s 81ms/step - loss: 0.6358 - accuracy: 0.5000 - val_loss: 0.6755 - val_accuracy: 0.6000
Epoch 4/10
1/1 [==============================] - 0s 79ms/step - loss: 0.7027 - accuracy: 0.5000 - val_loss: 0.6759 - val_accuracy: 0.6000
Epoch 5/10
1/1 [==============================] - 0s 82ms/step - loss: 0.6970 - accuracy: 0.4000 - val_loss: 0.6752 - val_accuracy: 0.6000
Epoch 6/10
1/1 [==============================] - 0s 83ms/step - loss: 0.7564 - accuracy: 0.4000 - val_loss: 0.6779 - val_accuracy: 0.6000
Epoch 7/10
1/1 [==============================] - 0s 73ms/step - loss: 0.7123 - accuracy: 0.6000 - val_loss: 0.6818 - val_accuracy: 0.6000