mnist CNN ValueError 预期的最小维度为4,但发现维度为3。接收到的完整形状为:[32, 28, 28] [重复]

我定义了模型如下。

tf.keras.datasets.mnist model = keras.models.Sequential([      tf.keras.layers.Conv2D(28, (3,3), activation='relu', input_shape=(28, 28, 1)),      tf.keras.layers.MaxPooling2D((2, 2)),      tf.keras.layers.Conv2D(56, (3,3), activation='relu'),      tf.keras.layers.Flatten(),      tf.keras.layers.Dense(64, activation='relu'),      tf.keras.layers.Dense(10, activation='softmax'),  ])  model.fit(x_train, y_train, epochs=3)  <----错误

当我尝试使用我的数据集运行时,出现了以下错误。

 ValueError: 层 sequential_3 的输入 0 与层不兼容:预期的最小维度为4,但发现维度为3。接收到的完整形状为:[32, 28, 28]

回答:

从您的错误来看,我认为您可能没有在训练集中添加通道轴,即 [batch, w, h, channel]。这是工作代码

数据集

import tensorflow as tf import numpy as np from sklearn.model_selection import train_test_split(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()x_train = np.expand_dims(x_train, axis=-1) # <--- 添加通道轴x_train = x_train.astype('float32') / 255y_train = tf.keras.utils.to_categorical(y_train, num_classes=10)print(x_train.shape, y_train.shape)# (60000, 28, 28, 1) (60000, 10)

训练

model = tf.keras.models.Sequential([      tf.keras.layers.Conv2D(28, (3,3), activation='relu', input_shape=(28, 28, 1)),      tf.keras.layers.MaxPooling2D((2, 2)),      tf.keras.layers.Conv2D(56, (3,3), activation='relu'),      tf.keras.layers.Flatten(),      tf.keras.layers.Dense(64, activation='relu'),      tf.keras.layers.Dense(10, activation='softmax'),  ])  model.compile(loss='categorical_crossentropy',optimizer='Adam',metrics=['accuracy'])model.fit(x_train, y_train, epochs=3)
第1轮/共3轮1875/1875 [==============================] - 11s 2ms/step - 损失: 0.2803 - 准确率: 0.9160第2轮/共3轮1875/1875 [==============================] - 4s 2ms/step - 损失: 0.0434 - 准确率: 0.9869第3轮/共3轮1875/1875 [==============================] - 4s 2ms/step - 损失: 0.0273 - 准确率: 0.9917

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注