【问题标题】:ValueError: `logits` and `labels` must have the same shape, received ((None, 10) vs (None, 11, 2, 2))ValueError: `logits` 和 `labels` 必须具有相同的形状,收到 ((None, 10) vs (None, 11, 2, 2))
【发布时间】:2022-01-11 12:08:32
【问题描述】:

输入:

x_train shape: (6000, 16, 16, 1)
x_test shape: (5000, 16, 16, 1)
y_test shape: (5000, 11, 2)
y_test shape: (5000, 16, 16, 1)
6000 train samples
5000 test samples

神经网络:-

batch_size = 128
num_classes = 10
epochs = 10
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer=tf.keras.optimizers.Adadelta(),metrics=['accuracy'])

错误:-

Epoch 1/10
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-47-628ddb8c3023> in <module>()
 ----> 1 hist = model.fit(x_train, 
 y_train,batch_size=batch_size,epochs=epochs,verbose=1,validation_data=(x_test, 
  y_test))
  2 print("The model has successfully trained")
  3 model.save('mnist.h5')
  4 print("Saving the model as mnist.h5")

   1 frames
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py 
    in autograph_handler(*args, **kwargs)
    1127           except Exception as e:  # pylint:disable=broad-except
    1128             if hasattr(e, "ag_error_metadata"):

-> 1129 引发 e.ag_error_metadata.to_exception(e) 1130 其他: 第1131章

    ValueError: in user code:

如果有任何混淆,我会尽力解释我的问题,然后我的 抱歉,让我知道,我会添加它。

【问题讨论】:

    标签: pandas machine-learning deep-learning neural-network conv-neural-network


    【解决方案1】:

    正如错误所述,虽然您的模型输出每个样本超过 10 个类别的预测,但您以某种不兼容的方式传递了“真实标签”,为什么您的标签是三维的?每个样本有一个 11x2x2 的张量,而不是一个单热编码的类号。

    【讨论】:

    • 你能告诉我我的重塑正确吗? x_train = x_train.reshape(x_train.shape[0], 16, 16, 1) x_test = x_test.reshape(x_test.shape[0], 16, 16, 1) input_shape = (16, 16, 1) # convert class vectors to binary class matrices y_train = keras.utils.np_utils.to_categorical(y_train) y_test = keras.utils.np_utils.to_categorical(y_test)
    • 我不知道你的数据是什么,但假设你的数据是灰度 16x16 图像,它看起来还可以
    猜你喜欢
    • 2021-10-18
    • 2020-12-06
    • 2021-07-27
    • 2021-10-28
    • 2021-07-28
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多