I'm getting output from CNTK's trainer/progress writer telling me my accuracy is > 99% when in fact it is around 0.5%. According to this metric does mean loss, but it wouldn't surprise me to learn I'm somehow using CNTK's trainer/loss function incorrectly.
Here's sample output from the example below (different than my model but produces a similar effect):
-------------------------------------------------------------------
Finished Epoch[1 of 20]: [Training] loss = 2.302585 * 100, metric = 48.10% * 100 0.802s (124.7 samples/s);
Accuracy % 11.0
Finished Epoch[2 of 20]: [Training] loss = 2.302514 * 100, metric = 49.82% * 100 0.043s (2325.6 samples/s);
Accuracy % 15.0
Here's a minimum working example that demonstrates the difference between the real accuracy and that reported by metric. I wrote a small accuracy function to test it, which I'm pretty sure is correctly implemented.
import cntk as C
import numpy as np
from cntk.ops import relu
from cntk.layers import Dense, Convolution2D
minibatchSize = 100
def printAccuracy(net, X, Y):
outs = net(X)
pred = np.argmax(Y, 1)
indx = np.argmax(outs, 1)
same = pred == indx
print("Accuracy %", np.sum(same)/minibatchSize*100)
outputs = 10
input_var = C.input_variable((7, 19, 19), name='features')
label_var = C.input_variable((outputs))
epochs = 20
cc = C.layers.Convolution2D((3,3), 64, activation=relu)(input_var)
net = C.layers.Dense(outputs)(cc)
loss = C.cross_entropy_with_softmax(net, label_var)
pe = C.classification_error(net, label_var)
learner = C.adam(net.parameters, 0.0018, 0.9, minibatch_size=minibatchSize)
progressPrinter = C.logging.ProgressPrinter(tag='Training', num_epochs=epochs)
trainer = C.Trainer(net, (loss, pe), learner, progressPrinter)
for i in range(epochs):
X = np.zeros((minibatchSize, 7, 19, 19), dtype=np.float32)
Y = np.random.rand(minibatchSize, outputs)
trainer.train_minibatch({input_var : X, label_var : Y})
trainer.summarize_training_progress()
printAccuracy(net, X, Y)
The problem is that the label var data doesn't have the expected properties.
For cross_entropy_with_softmax
it must represent a probability distribution, usually one-hot encoding.
For classification_error
it must be one-hot encoding.
So if you change your Y
data so it has exactly one 1 in each row, you will get accuracy = 100% - metric.