簡體   English   中英

Tensorflow 在預測時給出錯誤:輸入形狀的預期軸 -1 的值為 784,但收到的輸入形狀為 [None, 28]

[英]Tensorflow gives error when predicting: expected axis -1 of input shape to have value 784 but received input with shape [None, 28]

當我使用 Python 在 TensorFlow 中使用神經網絡進行預測時,出現以下錯誤: ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 784 but received input with shape [None, 28]

我正在嘗試按照 Tensorflow 網站上的教程來訓練神經網絡來對服裝進行分類。 我編寫了以下代碼:

import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
import numpy as np
from skimage import color, io

print(tf.__version__)

data = keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = data.load_data()

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
train_images = train_images / 255
test_images = test_images / 255


model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation="relu"),
    keras.layers.Dense(10, activation="softmax")
])

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

model.fit(train_images, train_labels, epochs=20)

print(type(test_images))
images = [test_images[0]]

predictions = model.predict(images)

print(class_names[np.argmax(predictions[0])])

非常感謝任何幫助,TIA。

為了社區的利益,來自評論部分。

通過將model.predict(images)更改為以下model.predict(images)行已經解決了這個問題。

model.predict(np.expand_dims(test_images[0],0))

暫無
暫無

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

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