Keras 层不兼容问题

我是机器学习的新手,正在尝试一些示例来学习它。我正在使用 fashion_mnist 数据库,并编写了下面的代码。我在这行代码上遇到了错误:

history = model.fit(train_X, train_y, epochs = 10, validation_data = (valid_X, valid_y))The error is the following: 

ValueError: Input 0 of layer sequential_5 is incompatible with thelayer: : expected min_ndim=4, found ndim=3. Full shape received:[None, 28, 28]

我哪里做错了,如何修复它?
提前感谢您。

以下是完整的源代码:

import matplotlib.pyplot as pltimport numpy as npimport pandas as pdimport tensorflow as tf#from tensorflow import keras#import kerasfrom keras.layers import Densefashion_mnist = tf.keras.datasets.fashion_mnist# Contrary to Scikit_Learn, Keras images are 28x28 array rather than a 1D array of size 784# Pixels intensities are integers (0 to 255) rather than floats (0.0 to 255.0)fashion_mnist.load_data()  # Dataset already split in Training and Testing# Dataset already split in Training and Testing(train_X, train_y), (test_X, test_y) = fashion_mnist.load_data()  print(train_X.shape)print(train_y.shape)print(test_X.shape)print(test_y.shape)# Adding 'valid' sample and Scaling the data (intensity of pixels from 0 to 1)valid_X, train_X = train_X[:5000], train_X[5000:]valid_y, train_y = train_y[:5000], train_y[5000:]print(train_X.shape)print(train_y.shape)print(test_X.shape)print(test_y.shape)print(valid_X.shape)print(valid_y.shape)# Dataset already split in Training and Testingtrain_X[0]# Labeling the 'y' data for the 1st imagey_modalities = ['T-shirt', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']print(train_y[0])print(y_modalities[train_y[0]])image_1 = train_X[0]plt.imshow(image_1, cmap = 'binary')print('Class number:', train_y[0])print('Label is:', y_modalities[train_y[0]])image_2 = train_X[1]plt.imshow(image_2, cmap = 'binary')print('Class number:', train_y[1])print('Label is:', y_modalities[train_y[1]])image_3 = train_X[2]plt.imshow(image_3, cmap = 'binary')print('Class number:', train_y[2])print('Label is:', y_modalities[train_y[2]])plt.figure(figsize = (10, 10))for i in range(25):    plt.subplot(5, 5, i+1)    plt.subplot(5, 5, i+1)    plt.xticks([])    plt.yticks([])    plt.grid(False)    plt.imshow(train_X[i], cmap = plt.cm.binary)    plt.xlabel(y_modalities[train_y[i]])plt.show()# Adding 'valid' sample and Scaling the data (intensity of pixels from 0 to 1)train_X , valid_X, test_X = train_X /255.0, valid_X/255.0, test_X/255.0print("after validation")print(train_X.shape)print(valid_X.shape)print(test_X.shape)# Scaled 1 image arraytrain_X[0].round(2)train_ymodel = tf.keras.models.Sequential([    tf.keras.layers.Conv2D(64, kernel_size = 3,  activation = 'relu', padding = 'same', input_shape = [28, 28, 1]),    tf.keras.layers.MaxPooling2D(2, 2),    tf.keras.layers.Conv2D(128, kernel_size = 3, activation = 'relu', padding = 'same'),    tf.keras.layers.Conv2D(128, kernel_size = 3, activation = 'relu', padding = 'same'),    tf.keras.layers.MaxPooling2D(2),    tf.keras.layers.Conv2D(256, kernel_size = 3, activation = 'relu', padding = 'same'),    tf.keras.layers.Conv2D(256, kernel_size = 3, activation = 'relu', padding = 'same'),    tf.keras.layers.MaxPooling2D(2,2),    tf.keras.layers.Flatten(),    tf.keras.layers.Dense(128, activation = 'relu'),    tf.keras.layers.Dropout(0.5),    tf.keras.layers.Dense(64, activation = 'relu'),    tf.keras.layers.Dropout(0.5),    tf.keras.layers.Dense(100, activation = 'relu'),    tf.keras.layers.Dense(10, activation = 'softmax')])# Built-in loss functions can be passed via their string identifier for a CLASSIFIER modelmodel.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])model.summary()history = model.fit(train_X, train_y, epochs = 10, validation_data = (valid_X, valid_y))pd.DataFrame(history.history).plot(figsize = (8, 5))plt.grid(True)plt.gca().set_ylim(0,1)plt.showmodel.evaluate(test_X, test_y)new_X = test_X[:9]prob_y = model.predict(new_X)prob_yy_pred = model.predict_classes(new_X)y_prednp.array(y_modalities)[y_pred]for i in range(9):    plt.subplot(330 + 1 + i)    x_i = test_X[i].reshape(28, 28)      # Replacing train_X with test_X    plt.imshow(x_i, cmap = 'binary')

回答:

这意味着您的图像形状与所需输入不一致,因此您需要像这样重塑您的输入,其余代码保持不变。

(train_X, train_y), (test_X, test_y) = fashion_mnist.load_data()train_X = train_X.reshape(-1, 28, 28, 1)test_X = test_X.reshape(-1, 28, 28, 1)

另外,您需要将损失函数更改为 ‘SparseCategoricalCrossentropy’

model.compile(loss = tf.keras.losses.SparseCategoricalCrossentropy(), optimizer = 'adam', metrics = ['accuracy'])

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中创建了一个多类分类项目。该项目可以对…

发表回复

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