【发布时间】: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