Skip to content Skip to sidebar Skip to footer

Feeding Labels With One Hot Encoded Vectors In Neural Network

I'm trying to create a Categorical classification Neural Network(NN) I have been given dataset which has 169307 rows. My output labels are [0,1,2] I one hot encoded them but I'm no

Solution 1:

You actually didn't do the conversion. You've only created a 3x3 identity matrix one_hot_targets, but never used it. As a result, batch_y is an array of df["target"]:

target = df["target"]
l = target.values.tolist()
l = np.array(l)
...
batch_y = np.expand_dims(l, axis=0)  # Has shape `(1, 169307)`!

Your batch_x also doesn't seem correct, but the feature is not defined in the snippet, so I can't say what exactly that is.

[Update] How to do one-hot encoding:

from sklearn.preprocessing import OneHotEncoder

# Categorical target: 0, 1 or 2. The value is just an example
target = np.array([1, 2, 2, 1, 0, 2, 1, 1, 0, 2, 1])

target = target.reshape([-1, 1])      # add one extra dimension
encoder = OneHotEncoder(sparse=False)
encoder.fit(target)
encoded = encoder.transform(target)   # now it's one-hot: [N, 3]

Post a Comment for "Feeding Labels With One Hot Encoded Vectors In Neural Network"