Gradient array contains NAN values.
When I run my code with single GPU, the gradient array does not contain any nan values. However, if I use more than one GPU, some gradient arrays contain nan values. Is this normal? If not, how can I fix this problem?
there are many situations about appearing NAN. You can follow these steps to check your program:
- check whether there are something wrong about input data
- setup smaller learn rate
I've examined both, my findings are:
- The input data has no nan values.
- Learning rate is 0.01. I think it is small enough. Isn't it?
On the other hand, the issues above should not be the reason of single and multi GPU cases since everything is the same for both single and multi GPU cases. Same batch, examples in the same order, same parameters etc. but different results for single and multi GPU. Only randomness may be occurred from Dropout layer. I'll try cancelling that layer again but I don't think it's the reason of nan values.
I've removed Dropout layer and nan values are disappeared. I think one of cases mentioned in http://stackoverflow.com/questions/25506281/what-are-all-the-possible-calculations-that-could-cause-a-nan-in-python is performed but I couldn't find where the issue is raised. Any help from senior contributors will be appreciated.
@mcxkarakoc Could you provide a reproducible example?
@sxjscience Here is a simplified example. Please note that it does not generate nan values in every run. You may have to run many times. In fact, that's the important part of the problem. As a note, to my observation, as the shape of the batch gets bigger, the chance of occurring nan values increases.
import mxnet as mx
import logging
import sys
def mean_abs(x):
return mx.ndarray.divide(mx.ndarray.sum(mx.ndarray.abs(x)), reduce(lambda x, y: x * y, x.shape));
logger = logging.getLogger();
logger.setLevel(logging.DEBUG);
formatter = logging.Formatter('%(message)s');
h2 = logging.StreamHandler(sys.stdout)
h2.setFormatter(formatter);
logger.addHandler(h2)
mx.random.seed(0)
data = mx.nd.normal(loc=74.0131, scale=61.9225, shape=(8192, 12288));
data = data - 74.0131
dataIter = mx.io.NDArrayIter(data=data, label=data, batch_size=4096, last_batch_handle='roll_over');
x = mx.symbol.Variable('data')
x = mx.symbol.Dropout(data=x, p=0.99)
x = mx.symbol.FullyConnected(name='encoder_0', data=x, num_hidden=16384)
x = mx.symbol.Activation(data=x, act_type='sigmoid')
x = mx.symbol.FullyConnected(name='decoder_0', data=x, num_hidden=12288)
x = mx.symbol.LinearRegressionOutput(data=x, name='softmax')
mon = mx.mon.Monitor(1, stat_func=mean_abs, pattern='.*', sort=True);
modSolver = mx.mod.Module(x, context=[mx.gpu(0), mx.gpu(1)]);
modSolver.fit(dataIter,
kvstore=mx.kvstore.create('local'),
optimizer='sgd',
eval_metric=mx.metric.create('rmse'),
monitor=mon,
initializer=mx.initializer.Uniform(0.07),
num_epoch=1);
Here is the results of one of the runs.
Batch: 1 activation0_backward_data 2.58393e-06
Batch: 1 activation0_backward_data 2.58335e-06
Batch: 1 activation0_output 0.500049
Batch: 1 activation0_output 0.499977
Batch: 1 data 49.4144
Batch: 1 data 49.3992
Batch: 1 decoder_0_backward_bias 0.391884
Batch: 1 decoder_0_backward_bias 0.392082
Batch: 1 decoder_0_backward_data 0.0180703
Batch: 1 decoder_0_backward_data 0.0180631
Batch: 1 decoder_0_backward_weight 0.215413
Batch: 1 decoder_0_backward_weight 0.215398
Batch: 1 decoder_0_bias 1.80592e-06
Batch: 1 decoder_0_bias 1.80592e-06
Batch: 1 decoder_0_output 2.93011
Batch: 1 decoder_0_output 2.92981
Batch: 1 decoder_0_weight 0.0348374
Batch: 1 decoder_0_weight 0.0348374
Batch: 1 dropout0_mask 1.00231
Batch: 1 dropout0_mask 1.00166
Batch: 1 dropout0_output 49.441
Batch: 1 dropout0_output 49.5205
Batch: 1 encoder_0_backward_bias 0.00342043
Batch: 1 encoder_0_backward_bias 0.00342288
Batch: 1 encoder_0_backward_data 0.00402836
Batch: 1 encoder_0_backward_data 0.00402722
Batch: 1 encoder_0_backward_weight 0.257857
Batch: 1 encoder_0_backward_weight 0.258375
Batch: 1 encoder_0_bias 1.27707e-08
Batch: 1 encoder_0_bias 1.27707e-08
Batch: 1 encoder_0_output 2208.79
Batch: 1 encoder_0_output 2210.08
Batch: 1 encoder_0_weight 0.0348379
Batch: 1 encoder_0_weight 0.0348379
Batch: 1 softmax_backward_data 0.00402836
Batch: 1 softmax_backward_data 0.00402722
Batch: 1 softmax_backward_label nan ********************** PROBLEM ********************
Batch: 1 softmax_backward_label 35.9656
Batch: 1 softmax_label 49.4144
Batch: 1 softmax_label 49.3992
Batch: 1 softmax_output 2.93011
Batch: 1 softmax_output 2.92981
Batch: 2 activation0_backward_data 2.60461e-06
Batch: 2 activation0_backward_data 2.6355e-06
Batch: 2 activation0_output 0.499976
Batch: 2 activation0_output 0.500013
Batch: 2 data 49.4038
Batch: 2 data 49.3949
Batch: 2 decoder_0_backward_bias 0.39078
Batch: 2 decoder_0_backward_bias 0.388034
Batch: 2 decoder_0_backward_data 0.0180632
Batch: 2 decoder_0_backward_data 0.0180607
Batch: 2 decoder_0_backward_weight 0.215219
Batch: 2 decoder_0_backward_weight 0.213855
Batch: 2 decoder_0_bias 3.49115e-06
Batch: 2 decoder_0_bias 3.49115e-06
Batch: 2 decoder_0_output 2.92459
Batch: 2 decoder_0_output 2.92481
Batch: 2 decoder_0_weight 0.0348374
Batch: 2 decoder_0_weight 0.0348374
Batch: 2 dropout0_mask 1.00176
Batch: 2 dropout0_mask 0.999085
Batch: 2 dropout0_output 49.5606
Batch: 2 dropout0_output 49.3116
Batch: 2 encoder_0_backward_bias 0.00343645
Batch: 2 encoder_0_backward_bias 0.00344834
Batch: 2 encoder_0_backward_data 0.00402759
Batch: 2 encoder_0_backward_data 0.00402679
Batch: 2 encoder_0_backward_weight 0.260728
Batch: 2 encoder_0_backward_weight 0.262505
Batch: 2 encoder_0_bias 1.89561e-08
Batch: 2 encoder_0_bias 1.89561e-08
Batch: 2 encoder_0_output 2212.39
Batch: 2 encoder_0_output 2206.8
Batch: 2 encoder_0_weight 0.0348379
Batch: 2 encoder_0_weight 0.0348379
Batch: 2 softmax_backward_data 0.00402759
Batch: 2 softmax_backward_data 0.00402679
Batch: 2 softmax_backward_label 2.5945e-06
Batch: 2 softmax_backward_label 2.58193e-06
Batch: 2 softmax_label 49.4038
Batch: 2 softmax_label 49.3949
Batch: 2 softmax_output 2.92459
Batch: 2 softmax_output 2.92481
Epoch[0] Train-rmse=62.028608
Epoch[0] Time cost=18.860
In my real code, encoder_0_backward_data sometimes becomes nan value in some or all GPUs. I couldn't write my real code here since my data is complex and private. However, the logic is the same.
@sxjscience @piiswrong I think the problem is related with the issues (numerical computation issue) in these links: https://github.com/fchollet/keras/issues/511 and https://github.com/fchollet/keras/issues/1244. They couldn't reach a stable solution, too. Theano has nanguardmode solution which I don't know its effectiveness. An efficient solution for the numerical computation stability would be good. For now, I'll apply the suggestions mentioned in http://deeplearning.net/software/theano/tutorial/nan_tutorial.html. Just FYI.
@mcxkarakoc For this example, I feel that the input here is one issue for the unstable behavior.
data = mx.nd.normal(loc=74.0131, scale=61.9225, shape=(8192, 12288));
data = data - 74.0131
This means that data will be non-centered and the sigmoids are at the saturate region.
Also, the hidden nodes of FC layer is quite large and may cause some computational problems.
@piiswrong Regarding the issue #782 , are they the same problem for softmax_backward_label? However, in my experiments (as you may see above), softmax_backward_label is not consistent. It takes different values including nan. According to your saying "we don't backprop to label" , softmax_backward_label should be constant, shouldn't it?