It works like a charm.
I did a numerical derivative by hand which returned same result.
Can’t say thank you enough. I tried to do the same in tensorflow for weeks but couldn’t. Just one day switched to pytorch and my problem solved.