简体   繁体   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]

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.

相关问题 输入形状的预期轴 -1 的值为 28,但收到的输入形状为 (None, 28, 28, 5) - expected axis -1 of input shape to have value 28, but received input with shape (None, 28, 28, 5) 层顺序的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 8,但收到的输入具有形状(无,71) - Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 8 but received input with shape (None, 71) 层密集的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 3,但收到的输入形状为 (None, 1) - Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape (None, 1) “ValueError:……与图层不兼容:输入形状的预期轴 -1 的值为 8,但接收到的输入形状为(无、7、169)” - “ValueError: …incompatible with the layer: expected axis -1 of input shape to have value 8 but received input with shape (None, 7, 169)” ValueError: ...与图层不兼容:输入形状的预期轴 -1 的值为 20,但接收到的输入形状为 (None, 20, 637) - ValueError: ...incompatible with the layer: expected axis -1 of input shape to have value 20 but received input with shape (None, 20, 637) ValueError:输入形状的预期轴 -1 的值为 51948,但接收到的输入形状为(无,52) - ValueError: expected axis -1 of input shape to have value 51948 but received input with shape (None, 52) 如何解决“输入形状的预期轴 -1 的值为 1,但收到的输入形状为 [None, 256, 256, 3]'”错误? - How can I solve "expected axis -1 of input shape to have value 1 but received input with shape [None, 256, 256, 3]'" error? 错误:检查模型输入时出错:期望dense_input_6具有形状(无,784)但是具有形状的数组(784L,1L) - Error: Error when checking model input: expected dense_input_6 to have shape (None, 784) but got array with shape (784L, 1L) ValueError:检查输入时出错:期望dense_18_input具有形状(784,)但是有形状的数组(1,) - ValueError: Error when checking input: expected dense_18_input to have shape (784,) but got array with shape (1,) 检查输入时出错:预期dense_1_input 具有形状(784,) 但得到形状为(10,) 的数组 - Error when checking input: expected dense_1_input to have shape (784,) but got array with shape (10,)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM