I applied that by adding a 0.1 to avoid division by 0, now the program takes a bit longer to makes the nan appears but it seems this time related to the SQRT operation (even if I added a small term there as well)  refering to the error message :
Traceback (most recent call last):
File “C:\Users\obouldjedr\Desktop\lastcode4\cp\test_capsnet.py”, line 538, in 
train(capsule_net, optimizer,trainloaderIMUhand, e)
File “C:\Users\obouldjedr\Desktop\lastcode4\cp\test_capsnet.py”, line 223, in train
loss.backward()
File “C:\Users\obouldjedr\Anaconda3\lib\site-packages\torch\tensor.py”, line 245, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs)
File “C:\Users\obouldjedr\Anaconda3\lib\site-packages\torch\autograd_init_.py”, line 145, in backward
Variable._execution_engine.run_backward(
RuntimeError: Function ‘SqrtBackward’ returned nan values in its 0th output.
What do you think the solution will be ?,  thank you in advance for your help