Since you are getting NaN values as output, it might be due to numerical instability during the training of a neural network.
You can make following changes to solve this issue:
- Try to reduce the learning rate to 0.0001. A smaller learning rate will result in gradual updates to the network weights. This will help in stabilizing the training process by preventing any drastic changes to the network.
- Implement gradient clipping to limit the magnitude of the gradients during backpropagation. This is crucial for preventing the exploding gradient problem, which can cause extremely large updates to the network weights.
You can refer to the following changes I have made to the code, and it is working fine now.
options = trainingOptions('sgdm','InitialLearnRate',0.0001, ...
'MiniBatchSize',2, ...
'MaxEpochs',15,'ExecutionEnvironment','cpu','GradientThreshold', 1);
Hope it helps!