mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

A bug in BatchNorm Op ?

Open jimxinbo opened this issue 10 years ago • 6 comments

Hi there, great work coding up the batchnorm layer. I can see it follows mostly to the BN paper except for using moving-mean, var for efficience, that is OK!

However, I think there a bug in the backward code.

In line 187-191 of batch_norm-inl.h, after computing gmean correctly, the code screwed it up by gmean += tmp. Not sure why to compute the tmp at all, becasue the following assignment to grad_in is correct without this extra tmp.

Seems the effect of this extra tmp will not be huge because it was devided by m 2 time, but mathematically it is wrong according to both my math and the BN paper.

I maybe wrong and I hope I was, let me know.

jimxinbo avatar Dec 24 '15 07:12 jimxinbo

I didn't write the code but you can verify by either 1) check the gradient with gradient checker in test/python/unittests/check_utils.py or 2) compare it with CuDNNBatchNorm @antinucleon

piiswrong avatar Dec 24 '15 08:12 piiswrong

Thanks piiswrong, always in time. Sorry I could not test it mysefl now. But honestly, I think it is pretty obvious, just ask related people to check about it. Anyway, It's clearly more of carelessness than a math mistake.

Note that, in the Inception-BN example, the corresponding m in BN is quit large and the difference can be rather minor as to affect the results. So maybe no one noticed it.

jimxinbo avatar Dec 24 '15 08:12 jimxinbo

Looking into it.

winstywang avatar Dec 24 '15 11:12 winstywang

@jimxinbo Check P4 of http://arxiv.org/pdf/1502.03167.pdf. The gradient w.r.t. \mu has two terms, the tmp corresponds to the second term.

winstywang avatar Dec 24 '15 11:12 winstywang

@winstywang thx Naiyan, interesting.

Check out this icml paper http://jmlr.csail.mit.edu/proceedings/papers/v37/ioffe15.pdf

I will look into it later but you can also check out why the difference.

jimxinbo avatar Dec 24 '15 11:12 jimxinbo

Hmm at a first glance though, seems the arxiv version took into account mu_b in sigma_b. Maybe the arxiv one is correct.

jimxinbo avatar Dec 24 '15 11:12 jimxinbo