Reputation: 51
I created a CNN model using the cifar100 dataset from Keras. When adding the top_k_categorical_accuracy metric, I should be seeing the accuracy of when one of the top 5 predicted classes is the correct class. However, when training, top_k_categorical_accuracy stays very small, around 4-5%, as accuracy and validation accuracy increase all the way to 40-50%. Top 5 accuracy should be much higher than normal accuracy, instead its giving very odd results. I wrote my own metric using different k values but still the same issue. Even when I use k=1, which should then give the same value of accuracy, the same issue occurs.
Model code:
cnn = Sequential()
cnn.add(Conv2D(filters=200, kernel_size=2, padding='same', activation='relu', input_shape=(train_images.shape[1:])))
cnn.add(Conv2D(filters=200, kernel_size=2, padding='same', activation='relu'))
cnn.add(Conv2D(filters=200, kernel_size=2, padding='same', activation='relu'))
cnn.add(MaxPooling2D(pool_size=2, padding='same'))
cnn.add(Dropout(0.4))
cnn.add(Conv2D(filters=200, kernel_size=2, padding='same', activation='relu'))
cnn.add(Conv2D(filters=200, kernel_size=2, padding='same', activation='relu'))
cnn.add(Conv2D(filters=200, kernel_size=2, padding='same', activation='relu'))
cnn.add(Conv2D(filters=200, kernel_size=2, padding='same', activation='relu'))
cnn.add(Dropout(0.4))
cnn.add(MaxPooling2D(pool_size=2, padding='same'))
cnn.add(Dropout(0.5))
cnn.add(Flatten())
cnn.add(Dense(550, activation='relu'))
cnn.add(Dropout(0.4))
cnn.add(Dense(100, activation='softmax'))
Compile code:
cnn.compile(loss='sparse_categorical_crossentropy', optimizer=opt.Adam(lr=learn_rate), metrics=['accuracy', 'top_k_categorical_accuracy'])
Upvotes: 1
Views: 2921
Reputation: 51
Turns out, since I am using the sparse_categorical_crossentropy loss function I need to use the sparse_top_k_categorical_accuracy function. This metric also requires your labels be flattened to one dimension. After that, metric is correct and model is training!
Upvotes: 4