简体   繁体   English

如何处理 tf.keras 中输出标签的“无效参数错误”

[英]How to handle the 'Invalid Argument Error' for output labels in tf.keras

I am using a data set with classification labels 1 to 8. While using keras sequential model, when I create an output layer with 8 neurons, it gives an Invalid argument error.我正在使用分类标签为 1 到 8 的数据集。在使用 keras 顺序模型时,当我创建一个具有 8 个神经元的输出层时,它给出了无效参数错误。 My understanding is that it recognizes labels as 0 to 7 and does not include 8. Thus, when I create an output layer with 9 neurons, it seems to work.我的理解是它将标签识别为 0 到 7 并且不包括 8。因此,当我创建一个具有 9 个神经元的输出层时,它似乎可以工作。 My query is, is it okay to use 9 neurons instead of 8?我的问题是,可以使用 9 个神经元而不是 8 个神经元吗?

Code is as follows:代码如下:

model = keras.models.Sequential()
model.add(keras.layers.InputLayer(input_shape=X.shape[1:] )) 
model.add(keras.layers.Dense(70, activation="selu",kernel_initializer="lecun_normal"))
model.add(keras.layers.Dense(70, activation="selu",kernel_initializer="lecun_normal"))
model.add(keras.layers.Dense(8, activation="softmax"))

model.compile(loss='sparse_categorical_crossentropy',
              optimizer='sgd',
              metrics=['accuracy'])
history = model.fit(X, y, epochs=100,
                    validation_data=(X_test, y_test))

error message: InvalidArgumentError: Received a label value of 8 which is outside the valid range of [0, 8).错误消息:InvalidArgumentError:接收到超出有效范围 [0, 8) 的 8 标签值。 Label values: 2 7 7 6 8 1 2 8 6 3 6 8 2 6 1 2 5 8 8 8 1 1 7 8 2 8 6 8 7 5 8 6 [[node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits (defined at :6) ]] [Op:__inference_train_function_823581]标签值:2 7 7 6 8 1 2 8 6 3 6 8 2 6 1 2 5 8 8 8 1 1 7 8 2 8 6 8 7 5 8 6 [[node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropySoftmaxLogsWith/SparseLogos6LogsWith/ ] [操作:__inference_train_function_823581]

Using 9 neurons wouldn't be a right choice to go with.使用 9 个神经元并不是一个正确的选择。 Since, you have a total of 8 class labels in your dataset, your logit vector should be 8-dimensional which would then be converted to a categorical distribution by applying the softmax function and thus computing the Cross-entropy loss.由于您的数据集中共有 8 个类标签,因此您的 logit 向量应该是8-dimensional ,然后通过应用softmax函数将其转换为分类分布,从而计算交叉熵损失。 The computation of the softmax categorical distribution includes a normalization factor which is the sum of the exponential of the logit vector components. softmax 分类分布的计算包括一个归一化因子,它是 logit 向量分量的指数之和。 Thus, adding 9 neurons would simply make this computation flawed since you would be adding an extra random value every time.因此,添加 9 个神经元只会使此计算出现缺陷,因为您每次都会添加一个额外的随机值。

In this case, the best solution would be to adjust your labels in the pre-processing stage.在这种情况下,最好的解决方案是在预处理阶段调整标签。 It is a pretty simple job to do and doesn't require much coding either.这是一项非常简单的工作,也不需要太多编码。 Let's say your labels are stored in a numpy array, you could do something like this:假设您的标签存储在一个 numpy 数组中,您可以执行以下操作:

labels = np.array([1, 2, 3, 4, 1, 5, 8]

map_dict = {1:0, 2:1, 3:2, 4:3, 5:4, 6:5, 7:6, 8:7}

for k, v in map_dict.items():
    labels[labels == k] = v

When you have 8 layers at the top of the model and you are using sparse categorial cross entropy loss it expects the label values to be 0 up to 7. When it gets a label of 8 it creates an error.当模型顶部有 8 个层并且您使用稀疏分类交叉熵损失时,它预计标签值为 0 到 7。当它获得 8 的标签时,它会产生错误。 When you change from 8 neurons to 9 it expects to see labels in the range of 0 to 8 so it will not generate an error.当您从 8 个神经元更改为 9 个神经元时,它预计会看到 0 到 8 个范围内的标签,因此不会产生错误。 Since none of the samples have a label of 0 your network will classify properly.由于没有一个样本的标签为 0,因此您的网络将正确分类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM