Smoothed loss becomes negative while loss is always positive
During training of a network with Euclidean loss layers using the adam solver the following happens in some cases when the initial loss is high:
I0904 14:04:10.916004 21891 solver.cpp:242] Iteration 0, loss = 2.38658e+18
I0904 14:04:10.916059 21891 solver.cpp:258] Train net output #0: loss = 2.38647e+18 (* 1 = 2.38647e+18 loss)
I0904 14:04:10.916079 21891 solver.cpp:258] Train net output #1: loss2 = 2.79839e+13 (* 4 = 1.11936e+14 loss)
I0904 14:04:10.916112 21891 solver.cpp:571] Iteration 0, lr = 0.0001
...
...
...
I0904 14:05:02.294297 21891 solver.cpp:242] Iteration 60, loss = 1.28282e+11
I0904 14:05:02.294333 21891 solver.cpp:258] Train net output #0: loss = 1.68715e+11 (* 1 = 1.68715e+11 loss)
I0904 14:05:02.294359 21891 solver.cpp:258] Train net output #1: loss2 = 2.02409e+07 (* 4 = 8.09637e+07 loss)
I0904 14:05:02.294389 21891 solver.cpp:571] Iteration 60, lr = 0.0001
...
...
...
I0904 14:05:53.597456 21891 solver.cpp:242] Iteration 120, loss = 3.33163e+10
I0904 14:05:53.597492 21891 solver.cpp:258] Train net output #0: loss = 5.66763e+10 (* 1 = 5.66763e+10 loss)
I0904 14:05:53.597517 21891 solver.cpp:258] Train net output #1: loss2 = 7.28106e+06 (* 4 = 2.91242e+07 loss)
I0904 14:05:53.597535 21891 solver.cpp:571] Iteration 120, lr = 0.0001
I0904 14:06:10.668745 21891 solver.cpp:242] Iteration 140, loss = 1.32058e+10
I0904 14:06:10.668792 21891 solver.cpp:258] Train net output #0: loss = 5.58073e+10 (* 1 = 5.58073e+10 loss)
I0904 14:06:10.668810 21891 solver.cpp:258] Train net output #1: loss2 = 9.37254e+06 (* 4 = 3.74902e+07 loss)
I0904 14:06:10.668829 21891 solver.cpp:571] Iteration 140, lr = 0.0001
I0904 14:06:27.734812 21891 solver.cpp:242] Iteration 160, loss = -1.63327e+09
I0904 14:06:27.734845 21891 solver.cpp:258] Train net output #0: loss = 2.92904e+10 (* 1 = 2.92904e+10 loss)
I0904 14:06:27.734870 21891 solver.cpp:258] Train net output #1: loss2 = 5.31872e+06 (* 4 = 2.12749e+07 loss)
I0904 14:06:27.734889 21891 solver.cpp:571] Iteration 160, lr = 0.0001
I0904 14:06:44.812893 21891 solver.cpp:242] Iteration 180, loss = -1.87728e+10
I0904 14:06:44.812940 21891 solver.cpp:258] Train net output #0: loss = 1.63414e+10 (* 1 = 1.63414e+10 loss)
I0904 14:06:44.812959 21891 solver.cpp:258] Train net output #1: loss2 = 2.0389e+06 (* 4 = 8.1556e+06 loss)
I0904 14:06:44.812979 21891 solver.cpp:571] Iteration 180, lr = 0.0001
I suspect a conversion issue in solver.cpp due to the large error values, because this error only occurs around 1.0e+10. However, I cannot find an obvious reason how this behavior might be introduced. The training continues and the individual loss values seem unaffected.
Could you solve your problem? I have the same problem with very small numbers. I checked the calculations of smooth_loss. Seems OK and cannot get negative mathematically but I was thinking that it is somehow related to floating point calculations and overflow.
Happened to me twice today, however the loss tends to be around -5 in my log. At one time it bounced up to 30000, fell to 0.2 and then back to negative. The motion seems to stay relative to the individual losses though.
I tried enabling average_loss, but that didn't help. This is with averaging set to 40:
I1107 21:25:39.014717 7283 solver.cpp:280] Learning Rate Policy: step
I1107 21:25:39.125702 7283 solver.cpp:228] Iteration 0, loss = 0.358664
I1107 21:25:39.125725 7283 solver.cpp:244] Train net output #0: loss_shift = 0.358664 (* 1 = 0.358664 loss)
I1107 21:25:39.125741 7283 sgd_solver.cpp:106] Iteration 0, lr = 0.01
I1107 21:25:50.893544 7283 solver.cpp:228] Iteration 50, loss = 1361.15
I1107 21:25:50.893568 7283 solver.cpp:244] Train net output #0: loss_shift = 59.7591 (* 1 = 59.7591 loss)
I1107 21:25:50.893573 7283 sgd_solver.cpp:106] Iteration 50, lr = 0.01
I1107 21:26:02.762877 7283 solver.cpp:228] Iteration 100, loss = 81.0927
I1107 21:26:02.762902 7283 solver.cpp:244] Train net output #0: loss_shift = 2.113 (* 1 = 2.113 loss)
I1107 21:26:02.762908 7283 sgd_solver.cpp:106] Iteration 100, lr = 0.01
I1107 21:26:14.668378 7283 solver.cpp:228] Iteration 150, loss = -4.36309
I1107 21:26:14.668436 7283 solver.cpp:244] Train net output #0: loss_shift = 0.283964 (* 1 = 0.283964 loss)
I1107 21:26:14.668440 7283 sgd_solver.cpp:106] Iteration 150, lr = 0.01
I1107 21:26:28.248845 7283 solver.cpp:228] Iteration 200, loss = -5.07713
I1107 21:26:28.248867 7283 solver.cpp:244] Train net output #0: loss_shift = 0.205489 (* 1 = 0.205489 loss)
I1107 21:26:28.248872 7283 sgd_solver.cpp:106] Iteration 200, lr = 0.01
I1107 21:26:41.890175 7283 solver.cpp:228] Iteration 250, loss = -5.10282
I1107 21:26:41.890197 7283 solver.cpp:244] Train net output #0: loss_shift = 0.220666 (* 1 = 0.220666 loss)
and then later there's events like this one:
I1107 21:39:58.146901 7283 solver.cpp:244] Train net output #0: loss_shift = 0.207623 (* 1 = 0.207623 loss)
I1107 21:39:58.146905 7283 sgd_solver.cpp:106] Iteration 3200, lr = 0.01
I1107 21:40:11.529955 7283 solver.cpp:228] Iteration 3250, loss = -5.1188
I1107 21:40:11.529976 7283 solver.cpp:244] Train net output #0: loss_shift = 0.200859 (* 1 = 0.200859 loss)
I1107 21:40:11.529980 7283 sgd_solver.cpp:106] Iteration 3250, lr = 0.01
I1107 21:40:24.945363 7283 solver.cpp:228] Iteration 3300, loss = -5.11607
I1107 21:40:24.945442 7283 solver.cpp:244] Train net output #0: loss_shift = 0.260779 (* 1 = 0.260779 loss)
I1107 21:40:24.945447 7283 sgd_solver.cpp:106] Iteration 3300, lr = 0.01
I1107 21:40:38.305057 7283 solver.cpp:228] Iteration 3350, loss = 0.874486
I1107 21:40:38.305076 7283 solver.cpp:244] Train net output #0: loss_shift = 1.59409 (* 1 = 1.59409 loss)
I1107 21:40:38.305081 7283 sgd_solver.cpp:106] Iteration 3350, lr = 0.01
I1107 21:40:51.693547 7283 solver.cpp:228] Iteration 3400, loss = -4.83926
I1107 21:40:51.693574 7283 solver.cpp:244] Train net output #0: loss_shift = 0.373092 (* 1 = 0.373092 loss)
I1107 21:40:51.693580 7283 sgd_solver.cpp:106] Iteration 3400, lr = 0.01
I1107 21:41:04.935509 7283 solver.cpp:228] Iteration 3450, loss = -5.0124
I1107 21:41:04.935565 7283 solver.cpp:244] Train net output #0: loss_shift = 0.319194 (* 1 = 0.319194 loss)
I1107 21:41:04.935571 7283 sgd_solver.cpp:106] Iteration 3450, lr = 0.01
I1107 21:41:18.193572 7283 solver.cpp:228] Iteration 3500, loss = -5.05116
@sunsided Have you solved the problem? I met the problem.
@CHUNYUWANG Sadly not. I have the feeling that this is related to too high learning rates though (maybe gradient clipping could help). The values are related to the problem at hand of course, but I could always trigger this when experimenting with base learning rates of 0.1 when 0.001 would have been enough. The overflow assumption might be correct, it seems that at some point the gradient gets to steep and this is when the combined loss goes haywire. Not sure if learning continues correctly after that, I usually stopped the process after noticing this and started over.
@sunsided Thanks for your reply. According to my understanding, loss is not directly related to learning rate. Whatever the learning rate is, the loss should not be negative. Besides, it should not be a overflow problem as a float number will not overflow to negative. I think it is related to how they compute the smoothed loss:
template
void Solver
int average_loss) {
if (losses_.size() < average_loss) {
losses_.push_back(loss);
int size = losses_.size();
smoothed_loss_ = (smoothed_loss_ * (size - 1) + loss) / size;
} else {
int idx = (iter_ - start_iter) % average_loss;
smoothed_loss_ += (loss - losses_[idx]) / average_loss;
losses_[idx] = loss;
}
}
In the else part, if loss is continuously smaller than losses_[idx], smoothed_loss_ will go negative. I don't quite understand the logic behind this function though.
@CHUNYUWANG Ah yes, this is the effect I saw. When I set high learning rates, the loss would jump from some initial value in the range of 1e-1 .. 1e+2 to 1e+30 and then fall. That's the moment when it flips over.
I think the idea of the code is to have a ring buffer of previous losses and a memory (smoothed_loss_). For each iteration, the current loss is added to the memory and one item from the buffer is removed (+= losses - losses[idx]); the result is then averaged and the selected buffer slot is overwritten. Due to the if part, smoothed_loss_ contains the mean of all previous average_loss values of loss.