mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

Periodic loss value

Open MDR-EX1000 opened this issue 9 years ago • 3 comments

The cross-entropy loss for classification task is somehow periodic during iteration, and the period is exactly an epoch of iterations of data set. The loss is lowest at the end of every epoch and increases rapidly(relatively) at the beginning of the next epoch....
This phenomenon appears in different networks training on different data sets in my experiments , so I think there might be something wrong with my training procedure rather than the model or data sets. Some potential mistake I probably made: the use of metric the use of dataIter the use of optimizer

anyone have encountered similar problem?

here are my codes:

train_iter = mx.io.ImageRecordIter(
	path_imgrec=recpath,
	data_shape=datashape,
	batch_size=batchsize,
	rand_crop=True,
	rand_mirror=True)


opt = mx.optimizer.SGD(
	momentum=0.9,
	rescale_grad=1.0/batchsize,
	learning_rate=base_lr,
	wd=weight_decay
	)
...
metric = mx.metric.create('ce')

for epoch in range(loadEpoch + 1,maxEpoch):
	train_iter.reset()
	for i,databatch in enumerate(train_iter):
		step += 1
		mod.forward(databatch,is_train=True)
		mod.update_metric(metric,databatch.label)
		if(step % display == 0):
			t = '[' + time.asctime( time.localtime(time.time())) + ']'
			info = t + 'epoch['+str(epoch)+']' + ',iter['+str(step)+'] '+'loss: ' + str(metric.get()[1])
			print info
			log.write(info + '\n')
			log.flush()
			metric.reset()
		mod.backward()
		mod.update()
	print 'Saving checkpoint at epoch' + str(epoch)
	mod.save_checkpoint(prefix,epoch)

MDR-EX1000 avatar Jun 24 '17 09:06 MDR-EX1000

Does the model seem to converge? Periodically high loss may just be the manifestation of the same ordering of training samples. Turning on shuffle (shuffle=True in ImageRecordIter) will most likely make the periodic loss increase go away since the order of training samples will be randomized.

szha avatar Jun 26 '17 03:06 szha

@szha Actually the model can successfully converge... this phenomenon is obvious when the learning rate is small(0.001) ... Thank you for your advice. I'll try if it helps. To my understanding shuffling to distort the order of labels at the beginning is enough. Confusingly when using Caffe, I didn't have this problem...

MDR-EX1000 avatar Jun 26 '17 04:06 MDR-EX1000

Feel free to reopen if the issue persists.

szha avatar Sep 17 '17 01:09 szha