It definitely sounds like your gradients aren’t right. I haven’t looked through your code, but in general, my advice would be to break it up into smaller functions that you can test individually. Good luck!