这是我在TensorFlow网站上的教程中获取的代码。在进行到一半时,我遇到了这个错误。我成功地训练了模型,但当测试图像被传入时,我得到了错误。
from tensorflow import kerasimport numpy as npimport pandas as pdimport matplotlib.pyplot as pltfashion_mnist= keras.datasets.fashion_mnist(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']train_images=train_images/255.0test_images=train_images/255.0model=keras.Sequential([ keras.layers.Flatten(input_shape=(28,28)), keras.layers.Dense(128,activation='relu'), keras.layers.Dense(10)])model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])model.fit(train_images,train_labels,epochs=5)test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)print('\nTest accuracy:', test_acc)```this code gives the following error:ValueError: Input arrays should have the same number of samples as target arrays. Found 60000 input samples and 10000 target samples.
回答:
test_images=train_images/255.0
应该改为:
test_images=test_images/255.0
否则你是在将train_images除以255,然后再将结果除以255。