[英]Tensorflow gives error when predicting: expected axis -1 of input shape to have value 784 but received input with shape [None, 28]
When I make a prediction using a neural network in TensorFlow using Python, I get the following error: 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]
.当我使用 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]
。
I am trying to follow the tutorial on Tensorflow's site to train a neural network to classify clothing items.我正在尝试按照 Tensorflow 网站上的教程来训练神经网络来对服装进行分类。 I have written the following code:
我编写了以下代码:
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])])
Any help is much appreciated, TIA.非常感谢任何帮助,TIA。
From the comment section for the benefit of the community.为了社区的利益,来自评论部分。
By changing the model.predict(images)
to below lines has solved the issue.通过将
model.predict(images)
更改为以下model.predict(images)
行已经解决了这个问题。
model.predict(np.expand_dims(test_images[0],0))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.