【问题标题】:Multiclass classification label error using TensoFlow使用 TensorFlow 的多类分类标签错误
【发布时间】:2021-10-04 18:25:38
【问题描述】:

我正在使用 TensorFlow 进行多类分类。目标有 4 个值 [0, 1, 13, 14]。这就是我在最后一个 Dense Layer 中取 4 的原因。我在损失函数中使用 sparse_categorical_crossentropy。我的代码如下:-

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
 
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Conv1D, MaxPool1D, Dropout, Conv2D
 
import matplotlib.pyplot as plt
 

x_train, x_test, y_train, y_test = train_test_split(X, data['seg_type'], test_size=0.33, random_state=1)
 
#The known number of output classes.
labels = [0, 1, 13, 14]
num_classes = 4
# #  label encoding
# encoder = LabelEncoder()
# y_train = encoder.fit_transform(y_train)
# y_test = encoder.fit_transform(y_test)
 
# # one hot encoding
# y_train = tensorflow.keras.utils.to_categorical(y_train, num_classes)
# y_test =  tensorflow.keras.utils.to_categorical(y_test, num_classes)

y_train = np.array(y_train)
y_test = np.array(y_test)

# build CNN model
model = Sequential()
model.add(Conv2D(32, (1, 1) , input_shape = (1, 3, 1), activation='relu'))  
model.add(Conv2D(64, (1, 1) , input_shape = (1, 3, 1), activation='relu'))
model.add(Conv2D(128, (1, 1) ,activation='relu'))  
model.add(Flatten())  # flatten
model.add(Dense(32, activation='relu'))  # fc
model.add(Dense(64, activation='relu'))  
model.add(Dense(128, activation='relu'))
model.add(Dense(4, activation='softmax'))

 
# model compile
model.compile(loss='sparse_categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
model.summary()
 
batch_size = 32
epochs = 20
history = model.fit(x_train, y_train,
                  batch_size=batch_size,
                  epochs=epochs,
                  verbose=2)

但是在拟合模型后它显示错误:-

InvalidArgumentError:  Received a label value of 14 which is outside the valid range of [0, 4).  Label values: 14 14 13 13 13 14 13 13 14 1 13 13 14 14 14 1 14 13 1 14 13 14 14 13 1 14 13 14 1 14 14 14
     [[node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits (defined at <ipython-input-81-33f0652484e5>:6) ]] [Op:__inference_train_function_574154]

Function call stack:
train_function

完整代码 - My notebook

数据 - Access the data here

提前谢谢!!!!!!!!!!

【问题讨论】:

    标签: tensorflow deep-learning tensorflow2.0 multiclass-classification


    【解决方案1】:

    由于您在此处的代码中使用了四个标签,

    #The known number of output classes.
    labels = [0, 1, 13, 14]
    num_classes = 4
    

    标签的有效范围是 0、1、2 和 3。因此,传递给预测的值应始终来自此范围本身,模型无法识别 13 和 14 值。

    您需要使用任何编码器将输入标签转换为有效范围,并在预测它们时进行反向操作。

    您可以将标签更改为 [0,3] 范围内的解决方法。

    【讨论】:

      【解决方案2】:

      将损失函数从sparse_categorical_crossentropy更改为分类交叉熵,并将数据转换为分类数据

              from keras.utils import to_categorical
      
              y_data = to_categorical(y_data, classes = 4)
      

      现在它可以工作了,目前的情况是类应该像 [0, 1, 2, 3] 但你正在使用 [0, 1, 13, 14]。解决这个问题的方法是分类函数

      【讨论】:

        猜你喜欢
        • 2017-06-03
        • 1970-01-01
        • 2016-05-25
        • 1970-01-01
        • 2017-01-08
        • 2018-06-11
        • 2017-12-30
        • 2020-05-01
        相关资源
        最近更新 更多