首页 > 解决方案 > AttributeError:模块 'keras.datasets.fashion_mnist' 没有属性 'shape'

问题描述

我一直在尝试使用 google colab 在时尚 mnist 数据集上实现 SVM,并不断收到上述错误。这里可能是什么问题?

import keras
from keras.datasets import fashion_mnist

data = keras.datasets.fashion_mnist
nsamples, nx, ny = data.shape

标签: pythonkeras

解决方案


要重塑您的数据,您应该替换这部分代码:

data = keras.datasets.fashion_mnist
nsamples, nx, ny = data.shape
data = data.reshape(nsamples,nx*ny)
(xtrain, ytrain), (xtest, ytest) = data.load_data()
convert(xtrain, ytrain)
convert(xtest, ytest)

有了这个:

(xtrain, ytrain), (xtest, ytest) = keras.datasets.fashion_mnist.load_data()

# xtrain.shape = (60000,28,28) before reshaping
xtrain = xtrain.reshape(xtrain.shape[0],(xtrain.shape[1] * xtrain.shape[2]))
# xtrain.shape = (60000,784) after reshaping

# xtest.shape = (10000,28,28) before reshaping
xtest = xtest.reshape(xtest.shape[0],(xtest.shape[1] * xtest.shape[2]))
# xtest.shape = (10000,784) after reshaping

convert(xtrain, ytrain)
convert(xtest, ytest)

推荐阅读