【问题标题】:Tensorflow - Received a label value of 99 which is outside the valid range of [0, 10)Tensorflow - 收到标签值 99,超出 [0, 10) 的有效范围
【发布时间】:2022-06-29 21:51:04
【问题描述】:

我正在尝试制作 cifar100 模型。当我开始训练模型时,我得到了这个错误

节点:'sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits' 收到的标签值 99 超出了 [0, 10) 的有效范围。标签值:1 47 23 85 26 78 60 78 26 85 11 13 24 60 1 65 97 7 14 59 20 35 94 65 79 43 24 78 47 41 0 91 56 2 63 78 32 96 87 162 62 71 16 37 82 92 28 55 7 71 14 14 85 69 12 48 3 26 18 26 96 69 10 34 28 96 88 13 99 17 69 65 12 92 46 89 41 93 23 13 2 93 47 87 83 72 82 37 79 22 22 [[{{node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]] [Op:__inference_train_function_657]

我的代码是

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

dataset = datasets.cifar100

(training_images, training_labels), (validation_images, validation_labels) = dataset.load_data()

training_images = training_images / 255.0
validation_images = validation_images / 255.0

model = tf.keras.Sequential([
                                    tf.keras.layers.Flatten(input_shape=(32,32,3)),
                                    tf.keras.layers.Dense(500, activation='relu'),
                                    tf.keras.layers.Dense(300, activation='relu'),
                                    tf.keras.layers.Dense(10, activation= 'softmax')
                                    ])

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(),
              metrics=['accuracy'])

history = model.fit(training_images,
                    training_labels,
                    batch_size=100,
                    epochs=10,
                    validation_data = (validation_images, validation_labels)
                    )

我在 Ubuntu 22.04

【问题讨论】:

    标签: python tensorflow machine-learning artificial-intelligence


    【解决方案1】:

    您的数据集由 100 个不同的类组成,而不是 10 个。所以只需在代码中更改这一行:

     tf.keras.layers.Dense(100, activation= 'softmax')
    

    它会起作用的。

    【讨论】:

    • 感谢您的快速响应!我现在就试试这个。
    猜你喜欢
    • 2020-04-03
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-18
    • 2019-11-22
    • 2021-04-28
    • 1970-01-01
    相关资源
    最近更新 更多