Cifar-100数据集错误-收到的标签值97超出有效范围[0,10)

时间:2019-10-25 04:59:19

标签: python tensorflow scikit-learn

使用Cifar-100数据集时得到此错误。我很确定我输入的形状正确,并且输出层为97,这是数据集中所有可能的输出。我可能做错了什么?注意:我是ML和Tensorflow的新手,请教我一些知识。 :)

tensorflow.python.framework.errors_impl.InvalidArgumentError:  Received a label value of 97 which is outside the valid range of [0, 10).  Label values: 7 97 79 82 97 33 19 73 28 93 32 6 51 68 67 38 55 1 56 60 97 27 79 36 87 34 20 22 7 42 34 62

谢谢。这是下面的代码。 Tensorflow的新手,我不知道我在做什么错。

import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

#print(train_images[0])
#print("Network Accuracy: " + str(test_acc))
# plt.imshow(train_images[0], cmap=plt.cm.binary) #greyscale
# plt.imshow(train_images[0]) #neon
# plt.show()

cifar100_mnist = keras.datasets.cifar100

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

train_images = train_images/255
test_images = test_images/255
classes = [
    'apple', 'aquarium_fish', 'baby', 'bear', 'beaver', 'bed', 'bee', 'beetle',
    'bicycle', 'bottle', 'bowl', 'boy', 'bridge', 'bus', 'butterfly', 'camel',
    'can', 'castle', 'caterpillar', 'cattle', 'chair', 'chimpanzee', 'clock',
    'cloud', 'cockroach', 'couch', 'crab', 'crocodile', 'cup', 'dinosaur',
    'dolphin', 'elephant', 'flatfish', 'forest', 'fox', 'girl', 'hamster',
    'house', 'kangaroo', 'keyboard', 'lamp', 'lawn_mower', 'leopard', 'lion',
    'lizard', 'lobster', 'man', 'maple_tree', 'motorcycle', 'mountain', 'mouse',
    'mushroom', 'oak_tree', 'orange', 'orchid', 'otter', 'palm_tree', 'pear',
    'pickup_truck', 'pine_tree', 'plain', 'plate', 'poppy', 'porcupine',
    'possum', 'rabbit', 'raccoon', 'ray', 'road', 'rocket', 'rose',
    'sea', 'seal', 'shark', 'shrew', 'skunk', 'skyscraper', 'snail', 'snake',
    'spider', 'squirrel', 'streetcar', 'sunflower', 'sweet_pepper', 'table',
    'tank', 'telephone', 'television', 'tiger', 'tractor', 'train', 'trout',
    'tulip', 'turtle', 'wardrobe', 'whale', 'willow_tree', 'wolf', 'woman',
    'worm'
]
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(32, 32, 3)),
    keras.layers.Dense(150, activation="relu"),
    keras.layers.Dense(97, activation="softmax")
])

model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
model.fit(train_images, train_labels, epochs=5)

test_loss, test_acc = model.evaluate(test_images, test_labels)
print(test_acc)
# print(test_images)
prediction = model.predict(test_images)
answer = np.argmax(prediction[1])
print(classes[answer])
# print(train_images[0])
plt.imshow(train_images[1])
plt.show()

1 个答案:

答案 0 :(得分:0)

cifar100数据集的最后一个softmax具有100个类。因此,模型中的最后一个密集层必须具有100作为单位值。

您可以在更新的代码下方找到

import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

#print(train_images[0])
#print("Network Accuracy: " + str(test_acc))
# plt.imshow(train_images[0], cmap=plt.cm.binary) #greyscale
# plt.imshow(train_images[0]) #neon
# plt.show()

cifar100_mnist = keras.datasets.cifar100

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

train_images = train_images/255
test_images = test_images/255
classes = [
    'apple', 'aquarium_fish', 'baby', 'bear', 'beaver', 'bed', 'bee', 'beetle',
    'bicycle', 'bottle', 'bowl', 'boy', 'bridge', 'bus', 'butterfly', 'camel',
    'can', 'castle', 'caterpillar', 'cattle', 'chair', 'chimpanzee', 'clock',
    'cloud', 'cockroach', 'couch', 'crab', 'crocodile', 'cup', 'dinosaur',
    'dolphin', 'elephant', 'flatfish', 'forest', 'fox', 'girl', 'hamster',
    'house', 'kangaroo', 'keyboard', 'lamp', 'lawn_mower', 'leopard', 'lion',
    'lizard', 'lobster', 'man', 'maple_tree', 'motorcycle', 'mountain', 'mouse',
    'mushroom', 'oak_tree', 'orange', 'orchid', 'otter', 'palm_tree', 'pear',
    'pickup_truck', 'pine_tree', 'plain', 'plate', 'poppy', 'porcupine',
    'possum', 'rabbit', 'raccoon', 'ray', 'road', 'rocket', 'rose',
    'sea', 'seal', 'shark', 'shrew', 'skunk', 'skyscraper', 'snail', 'snake',
    'spider', 'squirrel', 'streetcar', 'sunflower', 'sweet_pepper', 'table',
    'tank', 'telephone', 'television', 'tiger', 'tractor', 'train', 'trout',
    'tulip', 'turtle', 'wardrobe', 'whale', 'willow_tree', 'wolf', 'woman',
    'worm'
]
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(32, 32, 3)),
    keras.layers.Dense(150, activation="relu"),
    keras.layers.Dense(100, activation="softmax")
])

model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
model.fit(train_images, train_labels, epochs=5)

test_loss, test_acc = model.evaluate(test_images, test_labels)
print(test_acc)
# print(test_images)
prediction = model.predict(test_images)
answer = np.argmax(prediction[1])
print(classes[answer])

希望这会有所帮助