Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何解决此错误:预期展平_输入为3维,但得到了形状为(1,28,28,3)的数组?_Python_Tensorflow_Machine Learning_Keras - Fatal编程技术网

Python 如何解决此错误:预期展平_输入为3维,但得到了形状为(1,28,28,3)的数组?

Python 如何解决此错误:预期展平_输入为3维,但得到了形状为(1,28,28,3)的数组?,python,tensorflow,machine-learning,keras,Python,Tensorflow,Machine Learning,Keras,我正在尝试使用tensorflow让我的代码识别我学校项目中的数字。但我一直在犯这个错误。有人能帮我吗?非常感谢你 尝试过变平、改变尺寸等,但没有效果 这是我的代码: import tensorflow as tf mnist = tf.keras.datasets.mnist (x_train, y_train),(x_test, y_test) = mnist.load_data() x_train, x_test = x_train / 255.0, x_test / 255.0 mod

我正在尝试使用tensorflow让我的代码识别我学校项目中的数字。但我一直在犯这个错误。有人能帮我吗?非常感谢你

尝试过变平、改变尺寸等,但没有效果

这是我的代码:

import tensorflow as tf
mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([

tf.keras.layers.Flatten(input_shape=(28,28)),
tf.keras.layers.Dense(512, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])

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

model.fit(x_train, y_train, epochs=1)
model.evaluate(x_test, y_test)
# Part 3 - Making new predictions
import numpy as np
from keras.preprocessing import image
import keras
test_image = image.load_img('Number 8.jpg', target_size=(28, 28))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)
result = model.predict(test_image)
print(np.argmax(result[0]))

预计是3个数组,我想它是从这一行来的

tf.keras.layers.Flatten(输入形状=(28,28)),

你可以用

tf.keras.layers.flant()

即使您的图像是(28,28),培训时也会有一个批次维度
[batch\u size,28,28]
。由于未在
model.fit中传递批处理大小,因此使用默认值