使用 TensorFlow 的多类分类标签错误

Multiclass classification label error using TensoFlow

我正在使用 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

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

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

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

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

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

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

将损失函数从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]。对此的解决方案是分类函数