Solved by KFrank in post #2 
              
              
                Hi Aditya! 

I haven’t looked at your code or error messages in any detail. 
But, yes, your “graph is breaking.” 

code, as a function of output, is not (usefully) differentiable, so, 
regardless of what you subsequently do with code, you won’t be 
able to back propagate through it. 
I have no idea …