keras Python?形状(无,1)和(无,25)与CNN模型不兼容

webghufk  于 2022-11-13  发布在  Python
关注(0)|答案(1)|浏览(165)

我尝试训练模型,但遇到此错误 *ValueError:形状(无,1)和(无,24)不兼容 *

我的密码

model = models.Sequential()
model.add(layers.Conv2D(32, kernel_size=(3, 3), activation='relu',input_shape=(28, 28, 1)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D(pool_size=(2, 2)))
model.add(layers.Dropout(0.25))
model.add(layers.Flatten())
model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(25, activation=tf.nn.softmax))
model.summary()

模型摘要

Model: "sequential_6"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv2d_18 (Conv2D)          (None, 26, 26, 32)        320       
                                                                 
 conv2d_19 (Conv2D)          (None, 24, 24, 64)        18496     
                                                                 
 conv2d_20 (Conv2D)          (None, 22, 22, 128)       73856     
                                                                 
 max_pooling2d_8 (MaxPooling  (None, 11, 11, 128)      0         
 2D)                                                             
                                                                 
 dropout_8 (Dropout)         (None, 11, 11, 128)       0         
                                                                 
 flatten_5 (Flatten)         (None, 15488)             0         
                                                                 
 dense_10 (Dense)            (None, 128)               1982592   
                                                                 
 dropout_9 (Dropout)         (None, 128)               0         
                                                                 
 dense_11 (Dense)            (None, 25)                3225      
                                                                 
=================================================================
Total params: 2,078,489
Trainable params: 2,078,489
Non-trainable params: 0

编译器

model.compile(loss=tf.keras.losses.categorical_crossentropy,
        optimizer=tf.keras.optimizers.Adadelta(), metrics=['accuracy'])

符合模型

model.fit(train_img, train_y_values, batch_size=64, epochs=5, 
        verbose=1, validation_data=(test_img, test_y_values))

错误

Epoch 1/5
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-61-1ace0d4f685d> in <module>
      1 model.fit(train_img, train_y_values, batch_size=64, epochs=5, 
----> 2         verbose=1, validation_data=(test_img, test_y_values))

1 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py in tf__train_function(iterator)
     13                 try:
     14                     do_return = True
---> 15                     retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
     16                 except:
     17                     do_return = False

ValueError: in user code:

    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1051, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1040, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1030, in run_step  **
        outputs = model.train_step(data)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 890, in train_step
        loss = self.compute_loss(x, y, y_pred, sample_weight)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 949, in compute_loss
        y, y_pred, sample_weight, regularization_losses=self.losses)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 139, in __call__
        losses = call_fn(y_true, y_pred)
    File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 243, in call  **
        return ag_fn(y_true, y_pred, **self._fn_kwargs)
    File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1788, in categorical_crossentropy
        y_true, y_pred, from_logits=from_logits, axis=axis)
    File "/usr/local/lib/python3.7/dist-packages/keras/backend.py", line 5119, in categorical_crossentropy
        target.shape.assert_is_compatible_with(output.shape)

    ValueError: Shapes (None, 1) and (None, 24) are incompatible

列车_img形状=(27455,28,28)
训练y值形状=(27455,)
测试图像形状=(7172,28,28)
测试y值形状=(7172,)
"谁来救救我"

fkaflof6

fkaflof61#

该问题是由于模型的最后一个图层(无,25)与标签(无,1)不匹配造成的。
这可以通过将损失函数更改为“sparse_categorical_crossentropy”来解决
有关详细信息,请参阅What is the difference between sparse_categorical_crossentropy and categorical_crossentropy?

相关问题