Solved by SimonW in post #2 
              
              
                because you redefined x0. the second x0 is not really the x0 you defined the first time. it is not really reusing thje same variable, but redefining a symbol to be a different variable. your new variable here is an intermediate results, which do not retain gradients by default. you can call retain_g…