Ermolai
Ermolai

Reputation: 323

Train a NN for a subclass of cifar in Tensorflow : map labels

I chose a subclass of cifar100,containing only 20 out of the 100 classes.When i try to build a NN with 20 outputs i get this error: enter image description here

My 20-labels have values in rage [0-100] but i only need 20 neurons in the last layer.When i make the last layer 100 neurons i can train my model,but the accuracy is low.

I want to map the numbers in range(100) to make them range(20) so that the NN will accept them. Thank you in advance.

Upvotes: 0

Views: 64

Answers (1)

akensert
akensert

Reputation: 304

I think you might want to do something like this (which will be done as a preprocessing step before you feed your data/labels to the network):

# dummy array/labels
arr = [1, 3, 6, 8, 10, 4, 7, 15, 25, 19]
print('original array =', arr)

# create mapping (range(20) in your case)
mapping = dict(zip(set(arr), range(10)))
print('mapping =', mapping)

# apply the mapping
new_arr = list(map(lambda x: mapping[x], arr))
print('new array =', new_arr)

# >> output:
# original array = [1, 3, 6, 8, 10, 4, 7, 15, 25, 19]
# mapping = {1: 0, 3: 1, 4: 2, 6: 3, 7: 4, 8: 5, 10: 6, 15: 7, 19: 8, 25: 9}
# new array = [0, 1, 3, 5, 6, 2, 4, 7, 9, 8]

So basically the original labels of yours (which is len(set(labels)) = 20 but with values > 20, if I understand it correctly) is mapped to the smallest possible values so that it can work with your loss function. It might be wise to keep the mapping for later, if you need to map the labels back to the original values.

Upvotes: 1

Related Questions