C++ Front-end: What is the most correct way to compute derivatives of arbitrary functions w.r.t. different parameters

MiguelAlgaba: <p>Hi, I’m starting playing with the C++ Front-end and I wanted to know what would be the most correct way of using auto-differentiation to compute the derivatives or arbitrary functions with respect to different parameters.</p>
<p>I wrote the following minimal toy-example where I just wanted to see how to compute the derivative of <code>pointsInFrame1</code> with respect a change on <code>pointsInFrame0</code> (in particular just the first dimension of the pointsInFrame1 vector). In order to do that I used <code>pointsInFrame1ActiveDerivatives</code> to indicate that I wanted the derivative with respect to the first dimension, however I wonder if there is a cleaner way of doing this?</p>
<pre><code class="lang-auto">#include &lt;torch/torch.h&gt;
#include &lt;iostream&gt;

int main ()
{
    // One tensor containing one homogeneous 3D point in frame0 coordinates
    torch::Tensor pointsInFrame0 = torch::empty(4);
    pointsInFrame0[0] = -12.0;
    pointsInFrame0[1] = 13.0;
    pointsInFrame0[2] = -14.0;
    pointsInFrame0[3] = 1.0;
    pointsInFrame0.set_requires_grad(true);
    std::cout &lt;&lt; "pointsInFrame0:\n" &lt;&lt; pointsInFrame0 &lt;&lt; "\n";
    
    // One tensor containing a 4x4 isometry transformation matrix, transforming
    // world coordinates to frame0 coordinates
    torch::Tensor frame0FromWorld = torch::zeros({4,4});
    frame0FromWorld[0][0] = std::sqrt(3.0)/2.0;
    frame0FromWorld[0][1] = -1.0/2.0;
    frame0FromWorld[1][0] = 1.0/2.0;
    frame0FromWorld[1][1] = std::sqrt(3.0)/2.0;
    frame0FromWorld[2][2] = 1.0;
    frame0FromWorld[3][3] = 1.0;
    frame0FromWorld.set_requires_grad(true);
    std::cout &lt;&lt; "frame0FromWorld:\n" &lt;&lt; frame0FromWorld &lt;&lt; "\n";
    
    // One tensor containing a 4x4 isometry transformation matrix, transforming
    // world coordinates to frame1 coordinates
    torch::Tensor frame1FromWorld = torch::zeros({4,4});
    frame1FromWorld[0][0] = 1.0;
    frame1FromWorld[0][3] = -2.0;
    frame1FromWorld[1][1] = 1.0;
    frame1FromWorld[1][3] = 3.0;
    frame1FromWorld[2][2] = 1.0;
    frame1FromWorld[2][3] = -4.0;
    frame1FromWorld[3][3] = 1.0;
    frame1FromWorld.set_requires_grad(true);
    std::cout &lt;&lt; "frame1FromWorld:\n" &lt;&lt; frame1FromWorld &lt;&lt; "\n";
    
    // One tensor containing the transformed 3D points in frame1 coordinates
    torch::Tensor pointsInFrame1 = at::matmul(frame1FromWorld, at::matmul(torch::inverse(frame0FromWorld), pointsInFrame0));
    std::cout &lt;&lt; "pointsInFrame1:\n" &lt;&lt; pointsInFrame1 &lt;&lt; "\n";
    
    // One tensor computing the partial derivatives dpointsInFrame1/dpointsInFrame0
    torch::Tensor pointsInFrame1ActiveDerivatives = torch::zeros({4});
    pointsInFrame1ActiveDerivatives[0] = 1; // &lt;-- NOTE: my intention is to compute dpointsInFrame1.x/dpointsInFrame0
    pointsInFrame1.backward(pointsInFrame1ActiveDerivatives);
    std::cout &lt;&lt; "dpointsInFrame1/dpointsInFrame0:\n" &lt;&lt; pointsInFrame0.grad() &lt;&lt; "\n";
    
    // Check expected output
    torch::Tensor expectedPointsInFrame1Derivatives = torch::empty({4});
    expectedPointsInFrame1Derivatives[0] = 0.86604;
    expectedPointsInFrame1Derivatives[1] = 0.50002;
    expectedPointsInFrame1Derivatives[2] = 0.00000;
    expectedPointsInFrame1Derivatives[3] = -2.00000;
    assert((pointsInFrame0.grad()-expectedPointsInFrame1Derivatives).norm().item&lt;float&gt;() &lt; 5e-5);
}
</code></pre>
<p>Thanks a lot in advance!</p>