首页 文章

分类交叉熵和标签编码

提问于
浏览
-4

我正在尝试编写多类输出,类是['A','B','C','D','E','F','G'] .

有人可以详细说明下一条错误消息:

“ValueError:你正在传递一个形状(79,1)的目标数组,同时使用了亏损 categorical_crossentropy . categorical_crossentropy 期望目标是形状的二进制矩阵(1和0)(样本,类) . 如果你的目标是整数类,你可以通过以下方式将它们转换为预期的格式: from keras.utils.np_utils import to_categorical y_binary = to_categorical(y_int)

或者,您可以使用损失函数 sparse_categorical_crossentropy ,它确实需要整数目标 . “

我的代码:

# Part 1 - Data Preprocessing

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd 

# Importing the dataset
dataa = pd.read_csv('test_out.csv')

XX = dataa.iloc[:, 0:4].values
yy = dataa.iloc[:, 4].values

# Encoding categorical data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_Y_1 = LabelEncoder()
yy = labelencoder_Y_1.fit_transform(yy)

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(XX, yy, test_size = 0.2, 
random_state = 0)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# Part 2 - Now let's make the ANN!

# Importing the Keras libraries and packages
import keras
from keras.models import Sequential
from keras.layers import Dense


# Initialising the ANN
classifier = Sequential()

# Adding the input layer and the first hidden layer
classifier.add(Dense(output_dim = 6, init = 'uniform', activation = 'relu', 
input_dim = 4))

# Adding the second hidden layer
classifier.add(Dense(output_dim = 6, init = 'uniform', activation = 'relu'))

# Adding the output layer
classifier.add(Dense(output_dim = 1, init = 'uniform', activation = 
'softmax'))

# Compiling the ANN
classifier.compile(optimizer = 'adam', loss = 'categorical_crossentropy', 
metrics = ['accuracy'])

# Fitting the ANN to the Training set
classifier.fit(X_train, y_train, batch_size = 10, nb_epoch = 50)

# Part 3 - Making the predictions and evaluating the model

# Predicting the Test set results
y_pred = classifier.predict(X_test)
y_pred = (y_pred > 0.5)

# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)

2 回答

  • 3

    问题出在你的代码的这一部分,

    # Encoding categorical data
    from sklearn.preprocessing import LabelEncoder, OneHotEncoder
    labelencoder_Y_1 = LabelEncoder()
    yy = labelencoder_Y_1.fit_transform(yy)
    

    您忘了对 yy 进行单热编码,请注意 LabelEncoder 仅将您的分类数据转换为数字1,即 [A, B, C, D, E, F, G][1, 2, 3, 4, 5, 6, 7] . 你必须对它进行一次热编码,因为你想使用 softmax 激活,而 categorical_crossentropy (我是要点) .

    所以,应该是这样的,

    # Encoding categorical data
    from keras.utils import to_categorical
    from sklearn.preprocessing import LabelEncoder
    labelencoder_Y_1 = LabelEncoder()
    yy = labelencoder_Y_1.fit_transform(yy)
    yy = to_categorical(yy)
    
  • 0

    我假设您要预测的目标类是二进制,即只有2个可能的值可能发生

    如果您的目标是二进制,则应使用 sigmoid 激活函数激活模型的最后一层 . 此外,应使用 binary_crossentropysparse_categorical_crossentropy 编译模型 .

    如果目标是多类的,即多于2个可能的值,则必须在keras的 to_categorical 帮助下将目标转换为分类 . 然后你应该使用 categorical_crossentropy 编译模型,并且应该使用 softmax 激活函数激活模型中的最后一层 .

相关问题