我使用 CNN 模型创建了 CIFAR10 数据集学习模型。为啥会出现错误?

Posted

技术标签:

【中文标题】我使用 CNN 模型创建了 CIFAR10 数据集学习模型。为啥会出现错误?【英文标题】:I created a CIFAR10 dataset learning model using a CNN model. Why is there an error?我使用 CNN 模型创建了 CIFAR10 数据集学习模型。为什么会出现错误? 【发布时间】:2020-12-20 03:05:23 【问题描述】:

我使用 CNN 模型创建了 CIFAR10 数据集学习模型。 为什么会出现错误?我应该如何解决它? 我是在 Google colab 环境中完成的。

import tensorflow as tf
import keras
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense
from keras.datasets import cifar10

LOSS = 'categorical_crossentropy'
OPTIMIZER = 'adam'

def model_build():
  model = Sequential()

  # 1
  model.add(Conv2D(
    filters=32,
    kernel_size=(5,5),
    padding='same',
    activation='relu',
    input_shape=(32,32,3),
    kernel_regularizer='l2',
  ))
  model.add(MaxPooling2D(
    pool_size=(2,2),
    padding='same'
  ))

  # 2
  model.add(Conv2D(
    filters=64,
    kernel_size=(5,5),
    padding='same',
    activation='relu',
    kernel_regularizer='l2',
  ))
  model.add(MaxPooling2D(
    pool_size=(2,2),
    padding='same'
  ))

  # 3
  model.add(Flatten())
  model.add(Dense(
    units=512,
    activation='relu',
    kernel_regularizer='l2',
  ))

  # 4
  model.add(Dense(
  units=10,
  activation='softmax'
  ))

  model.compile(
  loss=LOSS,
  optimizer=OPTIMIZER,
  metrics=['accuracy']
  )

  return model

def load_dataset():
  (X_train, Y_train), (X_test, Y_test) = cifar10.load_data()

  X_train = X_train.astype('float32')
  X_test = X_test.astype('float32')
  X_train = X_train / 255.0
  X_test = X_test / 255.0

  return (X_train, Y_train), (X_test, Y_test)

model = model_build()
(X_train, Y_train), (X_test, Y_test) = load_dataset()

model.fit(
  x=X_train, y=Y_train,
  epochs=10,
  batch_size=32,
  verbose=1,
)

model.evaluate(
  x=X_test, y=Y_test,
  verbose=1,
)

这个错误发生在我身上

ValueError Traceback(最近调用 最后)在()

 77   epochs=10,
 78   batch_size=32,
 79   verbose=1,       <------Error
 80 )
 81

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)

975           except Exception as e:  # pylint:disable=broad-except
976             if hasattr(e, "ag_error_metadata"):
977               raise e.ag_error_metadata.to_exception(e)       <---Error
978             else:
979               raise 

ValueError:在用户代码中:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:805 train_function  * return step_function(self, iterator)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:795 step_function  ** outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:1259 run          return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2730 call_for_each_replica         return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:3417 _call_for_each_replica          return fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:788  run_step  **         outputs = model.train_step(data)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:756 train_step          y, y_pred, sample_weight, regularization_losses=self.losses)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:203 __call__         loss_value = loss_obj(y_t, y_p, sample_weight=sw)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/losses.py:152  __call__         losses = call_fn(y_true, y_pred)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/losses.py:256 call  **         return ag_fn(y_true, y_pred, **self._fn_kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper         return target(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/losses.py:1537 categorical_crossentropy         return K.categorical_crossentropy(y_true, y_pred, from_logits=from_logits)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper         return target(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/backend.py:4833 categorical_crossentropy         target.shape.assert_is_compatible_with(output.shape)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py:1134 assert_is_compatible_with         raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (None, 1) and (None, 10) are incompatible 

感谢您的回答。

【问题讨论】:

【参考方案1】:

我认为您的标签是整数而不是单热向量,它的形状是 (None, 1)。

试试:

LOSS = 'sparse_categorical_crossentropy'

【讨论】:

非常感谢。但是使用 'sparse_categorical_crossentropy' 的效果是什么?这对我来说太难理解了.. @kkkjkkkh 例如如果你的标签像 [1, 0, 0], [0, 1, 0], [0, 0, 1] - 你应该使用'categorical_crossentropy'。如果相同的标签显示为 [0, 1, 2] - 您应该使用 'sparse_categorical_entropy'

以上是关于我使用 CNN 模型创建了 CIFAR10 数据集学习模型。为啥会出现错误?的主要内容,如果未能解决你的问题,请参考以下文章

小白学习keras教程六基于CIFAR-10数据集训练CNN-RNN神经网络模型

基于卷积神经网络的CIFAR10图像分类

第七节:CNN练习1使用四种网络结构(VGGResNetMobileNetInceptionNet)进行cifar10训练

Keras CIFAR-10分类 自定义simple CNN篇

如何使用Tensorflow数据集进行CNN模型训练

深度学习基于tensorflow的小型物体识别训练(数据集:CIFAR-10)