Thanks for your reply.
The reason I am doing all this is that after my model’s output, I have done some changes. The Pytorch is calculating the gradient, but the problem is that I have implemented that changes in another way and the value of gradient is different. (with the same initialization, same input and … ) Based on these, I think the Pytorch cannot calculate the gradient correctly, so I used autograd to define my backward too.
I am trying to check the BCELoss’s gradient and output. I have used sigmoid before these layers, but I do not think the problem is Sigmoid here.
I have x and y, and I am trying to compute the loss value and it’s gradient.
The loss value is same in 99% times, but the gradient is never same. I have out the code and the values that I am not getting in here.
In Pytorch implementation, is clamp-log is used for backward too? I mean is this statement right?

if the log(x) is smaller than -100, the gradient is zero
if the log(x) is bigger or equal than -100, the gradient is 1/x

If yes, why I am not getting the same gradient to compare to Pytorch?