How can I do calculations on tensors that have "requires_grad = true"?
I have this program you see below.
import torch


def dht_calculate_transformation_of_single_joint(para_dht_parameters):
    var_a = para_dht_parameters[0]
    var_d = para_dht_parameters[1]
    var_alpha = para_dht_parameters[2]
    var_theta = para_dht_parameters[3]

    var_transformation = torch.tensor(data=[
        [torch.cos(var_theta), -1 * torch.sin(var_theta) * torch.cos(var_alpha), torch.sin(var_theta) * torch.sin(var_alpha), var_a * torch.cos(var_theta)],
        [torch.sin(var_theta), torch.cos(var_theta) * torch.cos(var_alpha), -1 * torch.cos(var_theta) * torch.sin(var_alpha), var_a * torch.sin(var_theta)],
        [0, torch.sin(var_alpha), torch.cos(var_alpha), var_d],
        [0, 0, 0, 1]
    ], dtype=torch.float32, requires_grad=True)

    return var_transformation


def dht_calculate_positions_of_all_joints(para_all_transformations_of_joints):
    var_all_positions_of_joints = torch.zeros(size=[27], dtype=torch.float32, requires_grad=True)
    var_index_all_positions_of_joints = 0
    var_transformation_to_joint = torch.zeros(size=[4, 4], dtype=torch.float32, requires_grad=True)

    for var_index_of_transformation_of_joint, var_transformation_of_joint in enumerate(para_all_transformations_of_joints):
        if var_index_of_transformation_of_joint == 0:
            var_transformation_to_joint = var_transformation_of_joint
        else:
            var_transformation_to_joint = torch.matmul(var_transformation_to_joint, var_transformation_of_joint)

        var_all_positions_of_joints[var_index_all_positions_of_joints + 0] = var_transformation_to_joint[0][3]
        var_all_positions_of_joints[var_index_all_positions_of_joints + 1] = var_transformation_to_joint[1][3]
        var_all_positions_of_joints[var_index_all_positions_of_joints + 2] = var_transformation_to_joint[2][3]
        var_index_all_positions_of_joints += 3

    return var_all_positions_of_joints


def dht_complete_calculation(para_input):
    var_input_reshaped = para_input.view(-1, 9, 4)
    var_output = torch.zeros(size=[para_input.shape[0], 27], dtype=torch.float32, requires_grad=True)  # Tensor ist x Zeilen (Datenreihen) * 27 Spalten (Positionen von Joints) groß.

    for var_index_of_current_row, var_current_row in enumerate(var_input_reshaped):
        var_all_transformations_of_joints = torch.zeros(size=[9, 4, 4], dtype=torch.float32, requires_grad=True)
        for var_index_of_current_column, var_current_column in enumerate(var_current_row):
            var_all_transformations_of_joints[var_index_of_current_column] = dht_calculate_transformation_of_single_joint(var_current_column)

        var_output[var_index_of_current_row] = dht_calculate_positions_of_all_joints(var_all_transformations_of_joints)

    return var_output


if __name__ == &quot;__main__&quot;:
    inp = torch.tensor(data=
        [
            [5.1016, 5.2750, 5.0043, 5.2184,
             4.8471, 5.3377, 5.0113, 5.0789,
             4.8800, 5.0455, 5.0394, 4.9092,
             4.6609, 5.5003, 5.1327, 4.7121,
             4.9442, 5.0918, 4.8083, 4.3548,
             5.0163, 4.8840, 4.7491, 4.8089,
             4.8919, 5.0975, 4.9931, 5.0999,
             4.6400, 5.0069, 4.7420, 5.3347,
             4.6725, 5.0338, 5.0310, 5.0470],
            [4.9628, 5.0113, 5.0834, 4.7143,
             5.0336, 5.1864, 5.4348, 5.0918,
             5.1570, 4.8881, 4.5411, 4.6745,
             4.6072, 4.9938, 4.9655, 5.2279,
             5.5559, 5.1952, 5.2229, 5.0727,
             5.1382, 4.7613, 4.6449, 4.3832,
             5.1866, 5.6650, 4.9886, 4.8088,
             4.9390, 5.3506, 5.1028, 4.4640,
             5.1076, 5.0772, 4.8219, 5.1303]
        ]
    , requires_grad=True)

    t1 = dht_complete_calculation(inp)
    print(&quot;Endergebins \n&quot;, t1, t1.shape)


I get the following message when I execute the main:
Traceback (most recent call last):
  File &quot;dht.py&quot;, line 77, in &lt;module&gt;
    t1 = dht_complete_calculation(inp)
  File &quot;dht.py&quot;, line 46, in dht_complete_calculation
    var_all_transformations_of_joints[var_index_of_current_column] = dht_calculate_transformation_of_single_joint(var_current_column)
RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation.

The thing is that the &quot;dht_complete_calculation&quot; function will be used with a neural Network (which isn't in the code fragment and isn't relevant to the question). The Output of the neural Network will be inputted in the &quot;dht_complete_calculation&quot; function. That is why the outputtensor and every tensor used in the calculation need to have &quot;requires_grad = true&quot;.
The &quot;dht_complete_calculation&quot; function gets a tensor with x rows and 36 columns as input and should output a tensor with x rows and 27 columns. The calculations you see there are correct, because if I remove &quot;requires_grad = true&quot; from every tensor it works.
This is the desired output:
tensor([[ 2.4727e+00, -4.4623e+00,  5.2750e+00,  6.6468e+00, -4.1351e+00,
          1.1145e+01,  1.3516e+01, -4.3618e+00,  1.2571e+01,  1.7557e+01,
         -1.0147e+01,  1.4048e+01,  1.8344e+01, -1.2500e+01,  2.0697e+01,
          2.4276e+01, -1.4575e+01,  2.3784e+01,  2.6110e+01, -2.0825e+01,
          2.6521e+01,  2.6707e+01, -2.4291e+01,  3.2371e+01,  3.1856e+01,
         -2.4376e+01,  3.6915e+01],
        [ 9.4848e-03, -4.9628e+00,  5.0113e+00,  3.1514e+00, -6.8211e+00,
          1.1249e+01,  9.8675e+00, -6.9772e+00,  1.3564e+01,  1.1752e+01,
         -9.6508e+00,  1.9519e+01,  1.1553e+01, -8.3219e+00,  2.7006e+01,
          1.4205e+01, -2.2681e+00,  2.9327e+01,  1.6872e+01, -2.0226e+00,
          3.6526e+01,  1.2353e+01, -5.7472e-01,  4.2049e+01,  1.0814e+01,
          3.8157e+00,  4.7547e+01]]) torch.Size([2, 27])

Process finished with exit code 0

However with &quot;requires_grad = true&quot; removed the network wouldn't learn anything, which is not what I want.
Can you help me to understand which part of the code triggers this error and how to fix it?

The problem here is not that you are doing computations on a requires_grad=True tensor. After all this is how one gets gradients! By doing computations on such tensors :)
The issue is that you are doing what are called in-place operations.
By in-place we mean that a previously existing variable's memory location is now replaced with a new variable. As a result the computational graph is broken, and thus, no gradient backpropagation can be achieved.
How does this look like? I found a few quick examples in this Pytorch-forum question
In particular:
&gt;&gt;&gt; x = torch.rand(1)
&gt;&gt;&gt; y = torch.rand(1)
&gt;&gt;&gt; x
tensor([0.2738])
&gt;&gt;&gt; id(x)
140736259305336
&gt;&gt;&gt; x = x + y   # Normal operation
&gt;&gt;&gt; id(x)
140726604827672 # New location
&gt;&gt;&gt; x += y
&gt;&gt;&gt; id(x)
140726604827672 # Existing location used (in-place)

So, you might then ask, where do you do that?
One such place is
var_all_positions_of_joints[var_index_all_positions_of_joints + 0] = var_transformation_to_joint[0][3]
    var_all_positions_of_joints[var_index_all_positions_of_joints + 1] = var_transformation_to_joint[1][3]
    var_all_positions_of_joints[var_index_all_positions_of_joints + 2] = var_transformation_to_joint[2][3]

Instead of doing that, you should instead collect all var_transofrmation_to_joint variables in a list and then do torch.stack or torch.cat depending on your situation. Alternatively, if in the future you seek to re-arrange the location of elements in a tensor, I recommend using something like einops for a highly-efficient and framework-independent solution.
