mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

training-accuracy nan

Open ArsenLuca opened this issue 9 years ago • 9 comments

When I was training using my own dataset, the training accuracy was shown to be nan when in epoch 8N-1(e.g. epoch 7, 15,23,31....). This makes me feel puzzled....

Also, in the example https://github.com/dmlc/mxnet-notebooks/blob/master/python/how_to/finetune.ipynb, the training accuracy falls to be nan in epoch 6. I was wondering how did this happen?

ArsenLuca avatar Dec 16 '16 02:12 ArsenLuca

because when called get() for a metric, the count is 0, which is often due to reset() is called. it doesn't not affect model, but sometime annoying.

mli avatar Dec 16 '16 07:12 mli

@mli many thanks,it's really annoying...

ArsenLuca avatar Dec 16 '16 08:12 ArsenLuca

same problem. really annoying

kli-casia avatar Aug 28 '17 09:08 kli-casia

same problem. Does anyone know how to fix it?

mangye16 avatar Sep 26 '17 09:09 mangye16

#5528

maxenceliu avatar Oct 12 '17 08:10 maxenceliu

The "Epoch[x] Train-accuracy=xxx" line printed at the end of every epoch gives the impression that the accuracy metric is for the entire epoch. In reality, it is NOT.

For example if an epoch consists of 101 batches and we were printing metrics every 10 batch because callback was created that way, what will be printed as 'Train-accuracy=xxx' at the end of an epoch is actually just the accuracy from a single batch (101'st batch). Printing this as 'Epoch[x] Train-accuracy=xxx' is very misleading.

Ideally we should remove this misleading print statement. But then, I'm sure there is a lot of existing scripts out there that look for this statement. We can't remove this without breaking those scripts. Since this will be a breaking change, let's do it in a major version change.

For now, to avoid the nan error, we can avoid resetting the metrics when processing callback for the last batch. It will be reset at the beginning of the next epoch anyway.

indhub avatar Apr 06 '18 08:04 indhub

This should fix it:

diff --git a/python/mxnet/module/base_module.py b/python/mxnet/module/base_module.py
index c03f8e7..10b3e75 100644
--- a/python/mxnet/module/base_module.py
+++ b/python/mxnet/module/base_module.py
@@ -22,6 +22,7 @@
 import time
 import logging
 import warnings
+from copy import deepcopy
 
 from .. import metric
 from .. import ndarray
@@ -523,8 +524,9 @@ class BaseModule(object):
                     monitor.toc_print()
 
                 if batch_end_callback is not None:
+                    arg_eval_metric = eval_metric if not end_of_batch else deepcopy(eval_metric)
                     batch_end_params = BatchEndParam(epoch=epoch, nbatch=nbatch,
-                                                     eval_metric=eval_metric,
+                                                     eval_metric=arg_eval_metric,
                                                      locals=locals())
                     for callback in _as_list(batch_end_callback):
                         callback(batch_end_params)

indhub avatar Apr 06 '18 08:04 indhub

Indu - why is this issue still open if PR 10437 fixed the issue?

srochel avatar Apr 14 '18 06:04 srochel

Fixed by https://github.com/apache/incubator-mxnet/pull/10437

indhub avatar Apr 25 '18 15:04 indhub