Solved by Vaibhav_Raj in post #2 
              
              
                Turns out that with a combination of the “inputs” and “retain_graph”, it works. 
I also noted that the problem was because the updates that I had made to the code were not working live on Colab.