分类的交叉熵和标签编码

时间:2018-03-24 18:36:42

标签: tensorflow scikit-learn keras

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

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

“ValueError:你正在传递一个形状(79,1)的目标数组,同时使用为categorical_crossentropycategorical_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 个答案:

答案 0 :(得分:3)

问题在于代码的这一部分,

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

您忘记对yy进行单热编码,请注意LabelEncoder仅将您的分类数据转换为数字数据,即[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)

答案 1 :(得分:0)

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

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

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