How to extract value from multi dimensional tensor without losing backward info - PyTorch
I am trying to extract some specific columns of a multi-dimensional tensor. I can take these values in the concept of multi-dimensional access; however I am losing the "grad_fn" option of the tensor when I create new values.

My actual multi-dimensional tensor, x, is:

tensor([[[-8.5780, -7.1091, -8.9204,  ..., -8.0616, -8.4115, -7.6345],
     [-7.9776, -7.3767, -8.2914,  ..., -7.9634, -9.1003, -7.5687],
     [-7.7192, -7.4307, -8.4294,  ..., -7.8605, -8.1345, -7.0781],
     ...,
     [-8.3652, -7.5910, -8.6671,  ..., -8.0487, -8.5826, -7.8624],
     [-8.1572, -7.1679, -8.8977,  ..., -8.1059, -8.1500, -7.7310],
     [-8.1821, -7.5455, -9.2328,  ..., -8.7435, -8.4430, -7.2503]]],
   grad_fn=&lt;LogSoftmaxBackward&gt;)


with shape (1,10,2000).

For example, I want to extract its specific 10 columns and finally create an array with a shape (1,10,10) or (10,10).

Currently, of course, I can extract these values into an array:

    for i in range(x.size(0)):

        values = np.array([x[i][1:, 1].detach().numpy()])

        idx_to_get = [1,5,7,25,37,44,720,11,25,46]

        for idx in idx_to_get:
            values = np.append(sentence_scores, np.array([np.array(x[0][1:,idx].detach().numpy())]), axis=0)

        values = np.delete(values, 0, axis=0)

        print(torch.from_numpy(values))


Running the code above gave me this output for the values:

tensor([[-7.5589, -6.7990, -7.2068, -7.4451, -7.6688, -7.2991, -7.1398, -7.4362,
     -7.4959, -8.0101, -7.5106, -8.0425, -7.6203, -7.7266, -7.9249, -7.6479,
     -7.6684],
    [-7.2831, -7.7666, -7.8302, -7.3651, -7.2184, -6.7932, -7.1968, -7.6590,
     -7.4033, -6.9504, -7.0767, -7.5366, -7.8364, -7.5935, -8.1235, -7.3222,
     -7.8096],
    [-7.5589, -6.7990, -7.2068, -7.4451, -7.6688, -7.2991, -7.1398, -7.4362,
     -7.4959, -8.0101, -7.5106, -8.0425, -7.6203, -7.7266, -7.9249, -7.6479,
     -7.6684],
    [-7.5650, -7.6627, -7.4230, -7.4726, -7.5621, -7.4489, -7.8344, -7.6130,
     -7.9440, -7.6158, -7.1895, -7.8070, -7.2306, -7.6364, -7.7390, -7.6832,
     -7.5931],
    [-7.5589, -6.7990, -7.2068, -7.4451, -7.6688, -7.2991, -7.1398, -7.4362,
     -7.4959, -8.0101, -7.5106, -8.0425, -7.6203, -7.7266, -7.9249, -7.6479,
     -7.6684],
    [-7.5589, -6.7990, -7.2068, -7.4451, -7.6688, -7.2991, -7.1398, -7.4362,
     -7.4959, -8.0101, -7.5106, -8.0425, -7.6203, -7.7266, -7.9249, -7.6479,
     -7.6684],
    [-7.2831, -7.7666, -7.8302, -7.3651, -7.2184, -6.7932, -7.1968, -7.6590,
     -7.4033, -6.9504, -7.0767, -7.5366, -7.8364, -7.5935, -8.1235, -7.3222,
     -7.8096],
    [-8.3559, -8.3751, -8.2082, -8.6825, -8.4860, -8.4156, -8.4683, -8.8760,
     -8.7354, -8.6155, -8.7544, -8.4527, -8.3690, -8.5795, -8.6023, -8.2267,
     -8.4736],
    [-7.4392, -7.4694, -7.4094, -7.5062, -7.7691, -7.9009, -7.7664, -7.1755,
     -8.0641, -7.6327, -7.6413, -7.9604, -7.9520, -7.8893, -7.8119, -7.8718,
     -8.0961],
    [-8.2182, -8.0280, -8.1398, -8.0258, -7.9951, -8.0664, -8.1976, -7.6182,
     -8.0356, -8.0293, -7.7228, -7.7847, -7.4966, -7.6925, -7.5268, -7.0476,
     -7.2920]])


But the values also should have grad_fn.
What should I do?

I know, using values.requires_grad_(True) worked, but I believe that with using that function, I loss the LogSoftmaxBackward on x.

The problem is that you can not use numpy functions to get this done AND retain the graph. You must use PyTorch functions only.

x = torch.rand((1,10,2000), requires_grad=True)
idx_to_get = [1,5,7,25,37,44,720,11,25,46]
values = x[0,1:,idx_to_get]
values


tensor([[0.6669, 0.1121, 0.1323, 0.7351, 0.0252, 0.2551, 0.3044, 0.3986, 0.7351,
         0.1060],
        [0.6169, 0.7715, 0.2829, 0.2860, 0.6810, 0.2485, 0.8585, 0.5284, 0.2860,
         0.8363],
        [0.6877, 0.0899, 0.6346, 0.7018, 0.7357, 0.1477, 0.2073, 0.3877, 0.7018,
         0.0226],
        [0.9241, 0.7883, 0.8442, 0.1831, 0.0551, 0.0209, 0.5300, 0.6909, 0.1831,
         0.2950],
        [0.5141, 0.5072, 0.4354, 0.3998, 0.5152, 0.9183, 0.2200, 0.5955, 0.3998,
         0.8631],
        [0.9630, 0.3542, 0.8435, 0.8299, 0.8614, 0.5029, 0.8672, 0.4985, 0.8299,
         0.2207],
        [0.6399, 0.5128, 0.2131, 0.4255, 0.9318, 0.6598, 0.8478, 0.7902, 0.4255,
         0.9080],
        [0.8920, 0.0357, 0.8957, 0.7379, 0.0191, 0.6750, 0.8326, 0.8535, 0.7379,
         0.9087],
        [0.5243, 0.7446, 0.4278, 0.3542, 0.1104, 0.2999, 0.0132, 0.8218, 0.3542,
         0.6761]], grad_fn=&lt;IndexBackward&gt;)

