What is the issue with my last dense keras layer?

Issue

I am working on a small NN in keras for multi-class classification problem. I have 9 different labels and my features are also 9.

My train/test shapes are the following:

Sets shape:
x_train shape: (7079, 9)
y_train shape: (7079,)
x_test shape: (7079, 9)
y_test shape: (7079,)

But when I try to make them categorical:

y_train = tf.keras.utils.to_categorical(y_train, num_classes=9)
y_test = tf.keras.utils.to_categorical(y_test, num_classes=9)

I get the following error:

IndexError: index 9 is out of bounds for axis 1 with size 9

Here is more info about the y_train

print(np.unique(y_train)) # [1. 2. 3. 4. 5. 6. 7. 8. 9.]
print(len(np.unique(y_train))) # 9

Anyone would know what the problem is?

Solution

The shape of the y_train is 1D. You have to make it one-hot encoded. Something like

y_train = tf.keras.utils.to_categorical(y_train , num_classes=9)

And same goes for y_test too.

Update

According to the doc,

tf.keras.utils.to_categorical(y, num_classes=None, dtype="float32")

Here, y: class vector to be converted into a matrix (integers from 0 to num_classes). As in your case, y_train is something like [1,2,..]. You need to do as follows:

y_train = tf.keras.utils.to_categorical(y_train - 1, num_classes=9)

Here is an example for reference. If we do

class_vector = np.array([1, 1, 2, 3, 5, 1, 4, 2])
print(class_vector)

output_matrix = tf.keras.utils.to_categorical(class_vector, 
                                      num_classes = 5, dtype ="float32")
print(output_matrix)
[1 1 2 3 5 1 4 2]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-15-69c8be7a0f1a> in <module>()
      6 print(class_vector)
      7 
----> 8 output_matrix = tf.keras.utils.to_categorical(class_vector, num_classes = 5, dtype ="float32")
      9 print(output_matrix)

/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/utils/np_utils.py in to_categorical(y, num_classes, dtype)
     76   n = y.shape[0]
     77   categorical = np.zeros((n, num_classes), dtype=dtype)
---> 78   categorical[np.arange(n), y] = 1
     79   output_shape = input_shape + (num_classes,)
     80   categorical = np.reshape(categorical, output_shape)

IndexError: index 5 is out of bounds for axis 1 with size 5

To solve this, we convert the data to a zero-based format.

output_matrix = tf.keras.utils.to_categorical(class_vector - 1, 
                                     num_classes = 5, dtype ="float32")
print(output_matrix)

[[1. 0. 0. 0. 0.]
 [1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 0. 1.]
 [1. 0. 0. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 1. 0. 0. 0.]]

Answered By – M.Innat

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published