Jacobian matrix of right shape, but null everywhere

ErikVi: <p>Dear Pytorch forum,<br>
For a project, I was tasked with the simulation of an MRI sequence using EPG formalism. The latter was a success. For the optimization of the sequence, it is necessary to differentiate the sequence signal with regards to different parameters at play. For the latter, I initially employed the <strong>autograd.grad</strong> function, a method which proved to take a lot more time due to the need for the implementation of loops, if I understood it correctly (see <a href="https://discuss.pytorch.org/t/issues-with-gradient-computed-with-torch-autograd/93412/4">this link</a>).<br>
Consequentially, I move to the usage of <strong>autograd.functional.jacobian</strong>. After some initial tests, I was able to implement it in my code such as to obtain a 500x3 (a.k.a. (time points) x (number of parameters)) array. The problem is though, that the latter is null in all points. Am I missing something conceptually with how the jacobian is to be computed with the pytorch function? Currently, the output() functions spits out the whole time-dependent array of the signal for the whole sequence. Could this be the cause of the issue?<br>
Code is pasted below, and further clarifications will be provided with haste should you find my explanation insufficient. Thanks in advance!</p>
<pre><code class="lang-auto">def dm_dT(params, func):
    values_array = []

    for key, value in params.items():
        if isinstance(value, torch.Tensor):
            values_array.append(value.item())
        else:
            values_array.append(value)

    jacobian = torch.autograd.functional.jacobian(
                func=func,
                inputs=torch.tensor(values_array, dtype = torch.float32),
                create_graph=True
                
            )        
    return jacobian

import numpy as np
import matplotlib.pyplot as plt
import torch




def output(params):
    T1, T2, D = params
    TE = 4
    TR = 15
    TI = 15  
    kg = 35
    T2 = torch.tensor(T2, dtype=torch.float32, requires_grad=True)
    T1 = torch.tensor(T1, dtype=torch.float32, requires_grad=True)
    D = torch.tensor(D, dtype=torch.float32, requires_grad=True)
    TI = torch.tensor(TI, dtype=torch.float32)
    TR = torch.tensor(TR, dtype=torch.float32)
    TE = torch.tensor(TE, dtype=torch.float32)
    kg = torch.tensor(kg, dtype=torch.float32)
    
    magnetization_states  = []
    transversal_data_imag = []
    transversal_data_real = []
    transversal_data_abs  = [] 

    FpFmZ = torch.zeros((3, 500), dtype=torch.complex64)
    FpFmZ[2, 0] = 1.

    FpFmZ = epg.epg_rf(FpFmZ, alpha=torch.tensor(np.pi, dtype=torch.float32), phi=torch.tensor(0, dtype=torch.float32))

    FpFmZ = epg.epg_relax(FpFmZ, T1, T2, TI)

    for a, FA in enumerate(FA_array[:500]):
        alpha = torch.tensor(FA, dtype=torch.float32, requires_grad=True)
        phi = torch.tensor(0, dtype=torch.float32, requires_grad=True)
        
        FpFmZ = epg.epg_rf(FpFmZ, alpha=alpha, phi=phi)    
        FpFmZ = epg.epg_grelax(FpFmZ, T1, T2, TE, kg, D, Gon=0)
        
        transversal_data_abs.append(torch.abs(FpFmZ.clone()[0,0]))
        transversal_data_imag.append(torch.real(FpFmZ.clone()[0,0]))
        transversal_data_real.append(torch.imag(FpFmZ.clone()[0,0]))
        
        magnetization_states.append(transversal_data_abs)    
        FpFmZ = epg.epg_grelax(FpFmZ, T1, T2, TR-TE, kg, D, Gon=1)
    
    # Assuming transversal_data_real is a list, convert it to a tensor
    transversal_data_real_tensor = torch.tensor(transversal_data_real, dtype=torch.float32)

    # Then detach and convert to numpy for plotting
    plt.plot(transversal_data_real_tensor.detach().numpy()) 
    
    return torch.tensor(transversal_data_real)

T1 = 300
T2 = 200
D = 0

grad_r = dm_dT(params={"T1": T1,"T2": T2,"D": D}, func=output)

print(grad_r)
      
</code></pre>

albanD: <p>Hey!</p>
<p>The problem is with the early part of your output() function.<br>
You break the autograd link between the given “params” and the T1/T2 used below by re-wrapping them into new Tensors.<br>
Deleting these 3 lines should fix it:</p>
<pre><code class="lang-auto">
    T2 = torch.tensor(T2, dtype=torch.float32, requires_grad=True)
    T1 = torch.tensor(T1, dtype=torch.float32, requires_grad=True)
    D = torch.tensor(D, dtype=torch.float32, requires_grad=True)
</code></pre>

ErikVi: <p>Hello Alban, thanks for the reply! And sorry for my late one…<br>
After applying your changes, which made perfect sense to me, and which logically should have worked, I unfortunately still get the same result, even when considering one variable in isolation. Could it have something to do with the shape of the output making it impossible to get non-null values?<br>
The function now takes 3 input parameters (T1, T2, D), and outputs the simulated signal at every time point for the whole sequence (500 elements).<br>
The output jacobian has the right shape (500 x 3), such as would suggest the definition of a jacobian pasted below, but when scrolling through <a href="https://pytorch.org/docs/stable/generated/torch.autograd.functional.jacobian.html" rel="noopener nofollow ugc">the documentation of the jacobian function</a>, there were function definitions in the examples which would spit out an array. If this is the error, what kind of changes to the code would you suggest?<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/2/d2e25b812c111e5826f2ea737ed9ec56c4244aa4.png" data-download-href="https://discuss.pytorch.org/uploads/default/d2e25b812c111e5826f2ea737ed9ec56c4244aa4" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/d/2/d2e25b812c111e5826f2ea737ed9ec56c4244aa4.png" alt="image" data-base62-sha1="u5zeIstoZX0P96KRoB9qUcWgGGM" width="690" height="355" data-dominant-color="F3F3F4"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">723×372 25.6 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Update: I ran a small test to see how <strong>torch.autograd.functional.jacobian</strong> handles functions with multiple outputs, and it seems to give the expected result. Consequentially, I am now not really sure what could be causing out the nulling of the matrix elements of the computed jacobian…</p>

albanD: <p>If the value is still 0, I would expect something else is “breaking” the autograd graph, you can use <a href="https://pypi.org/project/torchviz/" class="inline-onebox">torchviz · PyPI</a> and pass it the output of func for the given sample inputs to see what is the autograd graph that gets generated. You can also give it the inputs to see if they are indeed properly linked into the graph.<br>
Feel free to share the image of the graph here if you need help interpreting it.</p>