Skip to content Skip to sidebar Skip to footer

How To Make An Output Layer Of Size 5 When My Output Labels Are Even Numbers In Tensorflow?

I have labels 0,2,4,6,8 in my training data. So, technically, I have 5 classes but when I write the following code, model = keras.Sequential([ layers.Dense(units=512, activatio

Solution 1:

As the error you received, your integer labels should more like 0, 1, 2, 3, 4 instead of 0,2,4,6,8. You can convert your labels to solve the issue or you can transform your labels to an on-hot encoded vector as follows.

import numpy as np 
import pandas as pd 

x = np.random.randint(0, 256, size=(5, 784)).astype("float32")
y = pd.get_dummies(np.array([0, 2, 4, 6, 8])).values
x.shape, y.shape
((5, 784), (5, 5))

y
array([[1, 0, 0, 0, 0],
       [0, 1, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 0, 1, 0],
       [0, 0, 0, 0, 1]], dtype=uint8)

And additionally, you also need to use the categorical_crossentropy loss function instead of sparse_categorical_crossentropy. Full working code:

model = keras.Sequential([
    layers.Dense(units=512, activation="relu",input_shape=[784]),
    layers.Dense(units=512, activation="relu"),
    layers.Dense(units=5,activation="softmax")
])
model.compile(
    optimizer='adam',
    loss='categorical_crossentropy',
    metrics=['accuracy'],
)

history = model.fit(
    x, y,
    epochs=3, verbose=2
)

Epoch 1/3
380ms/step - loss: 153.8635 - accuracy: 0.2000
Epoch 2/3
16ms/step - loss: 194.0642 - accuracy: 0.6000
Epoch 3/3
16ms/step - loss: 259.9468 - accuracy: 0.6000

Post a Comment for "How To Make An Output Layer Of Size 5 When My Output Labels Are Even Numbers In Tensorflow?"