Search code examples
pythontensorflowmachine-learningdeep-learningneural-network

Tensorflow Neural Network with more than 2 categories


So i watched a tensorflow tutorial on udemy and decided to try for myself, he said if you want more than 2 categories change the activation to "softmax" and units to 4 since i have 4 different categories it could be in (changed from 0:1, to 1:4), everything works if there is only 2 different values in "y" but as soon as i change it to 4 units and 4 categories i get error:

ValueError: Error when checking target: expected dense_3 to have shape (4,) but got array with shape (1,)

even changing it back to shape "1" just results in true or false category

my dataset in y:

enter image description here

import numpy as np
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense

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
    
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)

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

Problem here

# 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
cm = confusion_matrix(y_test, y_pred)

Solution

  • With multiclass task, your y_train and y_test have to be one-hot-encoded. That means they must have dimension (number_of_samples, 4), where 4 denotes the number of classes.

    You need to apply tensorflow.keras.utils.to_categorical to them before train-test splitting.

    y = to_categorical(y, 4)
    

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