本文介绍了当我的输出标签在 Tensorflow 中是偶数时,如何制作大小为 5 的输出层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的训练数据中有标签 0、2、4、6、8.所以,从技术上讲,我有 5 个类,但是当我编写以下代码时,

I have labels 0,2,4,6,8 in my training data. So, technically, I have 5 classes but when I write the following code,

model = keras.Sequential([
    layers.Dense(units=512, activation="relu",input_shape=[784]),
    layers.Dense(units=512, activation="relu"),
    layers.Dense(units=5,activation="softmax")
])
model.compile(
    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy'],
)

history = model.fit(
    training_X, training_Y.reshape(2465,1),
    validation_data=(val_X, val_Y.reshape(986,1)),
    epochs=50,
    verbose = 1
)

我收到此错误,

InvalidArgumentError:  Received a label value of 8 which is outside the valid range of [0, 5).  Label values: 6 8 4 6 2 4 8 0 2 2 4 6 0 2 6 4 4 2 2 8 0 0 6 0 2 8 0 2 2 6 4 4

那么,我如何仅使用 5 个输出单元并针对这些标签对其进行训练?

So, how do I use only 5 output units and train it for these labels?

推荐答案

作为你收到的错误,你的整数标签应该更像 0, 1, 2, 3, 4 而不是 0,2,4,6,8.您可以转换标签来解决问题,也可以将标签转换为 on-hot 编码向量,如下所示.

As the error you received, your integer labels should more like 0, 1, 2, 3, 4 instead of 0,2,4,6,8. You can convert your labels to solve the issue or you can transform your labels to an on-hot encoded vector as follows.

import numpy as np 
import pandas as pd 

x = np.random.randint(0, 256, size=(5, 784)).astype("float32")
y = pd.get_dummies(np.array([0, 2, 4, 6, 8])).values
x.shape, y.shape
((5, 784), (5, 5))

y
array([[1, 0, 0, 0, 0],
       [0, 1, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 0, 1, 0],
       [0, 0, 0, 0, 1]], dtype=uint8)

此外,您还需要使用 categorical_crossentropy 损失函数而不是 sparse_categorical_crossentropy.完整的工作代码:

And additionally, you also need to use the categorical_crossentropy loss function instead of sparse_categorical_crossentropy. Full working code:

model = keras.Sequential([
    layers.Dense(units=512, activation="relu",input_shape=[784]),
    layers.Dense(units=512, activation="relu"),
    layers.Dense(units=5,activation="softmax")
])
model.compile(
    optimizer='adam',
    loss='categorical_crossentropy',
    metrics=['accuracy'],
)

history = model.fit(
    x, y,
    epochs=3, verbose=2
)

Epoch 1/3
380ms/step - loss: 153.8635 - accuracy: 0.2000
Epoch 2/3
16ms/step - loss: 194.0642 - accuracy: 0.6000
Epoch 3/3
16ms/step - loss: 259.9468 - accuracy: 0.6000

这篇关于当我的输出标签在 Tensorflow 中是偶数时,如何制作大小为 5 的输出层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 10:31