'How the TensorFlow Output Dense layers nodes mapping to labels?
I'm starting to study the tensorflow with the image classification sample which is the first sample on the tensorflow official document. It creates the Keras Sequential model as below:
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10)
])
The input data is keras.datasets.fashion_mnist containning 28*28 images. and the image label is in [0,1,2...9].
According to the explaination , I suppose the keras.layers.Dense(10) here is for the 10 labes and each neuron(node) will hold a logit for one label and the output shape of the layer is (batchsize, 10).
My question is:
1.Can we make the ouput dense layer node number greater than 10, like 11, then the output shape is (batchsize, 11), what does this mean and what will the output look like?
2.How the 10 node (from node[0] to node[9]) in the output layer mapping to the label 0 - 9, how do we know the output order of the layer from node[0] to node[9] is mapping to label[0] to label[9]. per my understanding ,if weight of each node is randomly initialized, the order should also be random?
Thanks very much. Harrison
Solution 1:[1]
I believe I got the answer.
The reason is the Loss function I was using.
I used tf.keras.losses.SparseCategoricalCrossentropy as loss function and this function requires the output layer node is exactly equal to the number of categories.(0,1,2,..9) here
since output shape will be (batchsize, nodecount) and SparseCategoricalCrossentropy will map the first node to 0 and second node to 1 and so on.
Thank everyone for viewing this question. Hope this answer helps to you.
Harrison.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Harrison Wang |