okay_All_Right:

any rl framework they are parasites


Well they do help quite a lot to avoid having such issues.
You don’t have to spend days computing gradients by hand and even more debugging them.
From looking at your code, I’m not sure how you define the gradient from the loss function, and given that your optimizer is already flipping the sign of the gradient, you should not do it there.
Also your tanh formula is wrong as its missing the chain rule that mutiplies it by the flowing gradients.
There might be other issues though…