I made my own Neural Network from scratch in unity with C# and I am using it as DQN. I set up my network which has 4 layers: 9 input values, 20 nodes in the second layer, 15 nodes in the third layer, and 8 outputs. I set all the layers' activation functions as Identity (which has no impact on the value), and the loss function as MSE I scaled my inputs and rewards, so my input values are between 0-1, and targets are between -1 and 1.

But after the first training, the weights become NaN. On another project I have a much simpler network which is 5-4-3-2 nodes in layers. And there it works perfectly. But when I increase the nodes for layers it immediately makes the weights NaN. If I add a coefficient factor to Identity something like 0.1 it doesn't give NaN but this time the 8 output values become very similar to each other.

I debugged the delta values and after 6 or 7 epochs later the weights go to very large numbers like 2345221. Than after that they become NaN, I think due to large values.

With other activation function(TanH, sigmoid) they don't give that NaN values but the outputs are become very close to 1(I understand that because the network has very nodes and the outputs are larger than 3 or 4 and this functions converge to 1 with larger values than 2). But this NaN thing happens with the ReLU functions too.

My question is: Is it normal to have this numbers or something is wrong with my code? If it is normal what can I do because the other functions also give bad results, I am trying to predict the cumulative reward here.


1 Answer 1


Having NaN might be because of the exploding gradient problem. This happens when your model learns very slowly and perhaps the training stagnates at a very early stage just after a few iterations.

  • 2
    $\begingroup$ I don't think it is because of slow learning because when I made the learning rate 0.001, it stopped giving NaN values. I think it is the opposite. $\endgroup$
    – Ege
    Aug 30, 2023 at 11:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .