簡體   English   中英

如何通過重塑對 MNIST 數據集進行預測?

[英]How do I make predictions on the MNIST dataset with reshaping?

I'm working on a simple example of a ANN using the MNIST dataset. I think I understand the basic breakdown of the model, including reshaping the data, but I'm having trouble with the prediction aspect. 

model = keras.Sequential([
    layers.Dense(512, activation='relu'),
    layers.Dense(100, activation='relu'),
    layers.Dense(10, activation='softmax')
])

model.compile(optimizer='rmsprop',
             loss='sparse_categorical_crossentropy',
             metrics=["accuracy"])

train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype("float32") / 255
test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype("float32") / 255

model.fit(train_images, train_labels, epochs=5, batch_size=128)
model.predict(test_images)

img = test_images[4].reshape(28, 28)
plt.imshow(img)

model.predict(test_images[4])

當我預測“img”時,我收到以下錯誤:“ValueError:層順序的輸入 0 與層不兼容:輸入形狀的預期軸 -1 具有值 784,但接收到的輸入形狀為(無,1)。 " 但是,我重新塑造了“IMG”,所以我不確定如何修復錯誤來測試模型的預測。 我也試過 model.predict(img)。 請指教。

在下面更改您的代碼:

train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype("float32") / 255
test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype("float32") / 255

使用此代碼:您的圖層沒有得到正確的輸入形狀

X_train, X_test = X_train / 255, X_test /255
X_train = np.expand_dims(X_train, -1)
X_test = np.expand_dims(X_test, -1)

X_train.shape    // Shape should be (60000,28,28,1)

//Before using labels do this
y_train = to_categorical(y_train, K)
y_test = to_categorical(y_test, K)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM