具有 2 个以上类别的 Tensorflow 神经网络

Tensorflow Neural Network with more than 2 categories

所以我看了一个关于 udemy 的 tensorflow 教程并决定自己尝试,他说如果你想要超过 2 个类别将激活更改为 "softmax" 并将单位更改为 4 因为我有 4 个不同的类别它可以在(从 0:1 更改为 1:4),如果 "y" 中只有 2 个不同的值,则一切正常,但一旦我将其更改为 4 个单位和 4 个类别,我就会出错:

ValueError:检查目标时出错:预期 dense_3 具有形状 (4,) 但得到的数组具有形状 (1,)

即使将其改回形状“1”也只会导致正确或错误的类别

我在 y 中的数据集:

import numpy as np

dataset = np.load('/Users/alex/desktop/ANN_dataset_for_brightness.npy')
X = dataset[:, 0:17]
y = dataset[:, 17:19]

for i in range (27):
    if y[i] == 400:
        y[i] = 4
    elif y[i] == 300:
        y[i] = 3
    elif y[i] == 200:
        y[i] = 2
    elif y[i] == 100:
        y[i] = 1




from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)


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

# Initialising the ANN
classifier = Sequential()

# Adding the input layer and the first hidden layer // input dim for input layer
classifier.add(Dense(activation="relu", input_dim=17, units=6, kernel_initializer="uniform"))

# Adding the second hidden layer
classifier.add(Dense(activation="relu", units=6, kernel_initializer="uniform"))


这里有问题

# units = categories, softmax = more than 2
classifier.add(Dense(activation="softmax", units=4, kernel_initializer="uniform"))







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

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

# 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)

对于多类任务,您的 y_train 和 y_test 必须进行单热编码。这意味着它们必须具有维度 (number_of_samples, 4),其中 4 表示 类.

的数量

您需要在训练-测试拆分之前对它们应用 tensorflow.keras.utils.to_categorical。

y = to_categorical(y, 4)

参考:https://www.tensorflow.org/api_docs/python/tf/keras/utils/to_categorical