Search code examples
pythonnumpypytorchtensor

How do I extract tensors in a tensor, into a 2D-numpy array?


I'm trying to extract tensors in a larger tensor, into a 2D-numpy array. (The tensor of tensors holds node embeddings after passing through a graph neural network). I'm using PyTorch (Geometric) for my project. I need the individual embeddings to go further.

This is my tensor:

tensor([[-0.7863,  0.8097],
        [-1.0679,  1.1331],
        [-1.8162,  1.9160],
        [ 2.0584, -2.2741],
        [-1.8818,  1.9333],
        [ 0.7870, -0.8974],
        [ 6.1731, -6.8074],
        [ 7.3219, -8.0852],
        [-0.9933,  0.9439],
        [ 4.6217, -5.1856],
        [-1.3747,  1.4614],
        [ 4.6429, -5.0861],
        [ 3.1141, -3.4420],
        [ 2.6417, -2.9173],
        [-2.9696,  3.0740],
        [ 4.0654, -4.5340],
        [ 1.7143, -1.9558],
        [-1.7497,  1.8496],
        [-1.9055,  1.9934],
        [ 3.9273, -4.3356],
        [ 4.0350, -4.4137],
        [ 1.2770, -1.4225],
        [-1.7447,  1.8458],
        [ 1.3937, -1.5936],
        [ 3.2471, -3.5991],
        [ 2.2516, -2.6034],
        [ 1.3096, -1.4573],
        [-1.7823,  1.8775],
        [ 0.9923, -1.2175],
        [-1.1818,  1.2430],
        [ 1.0997, -1.2466],
        [ 0.4841, -0.5800],
        [ 4.1609, -4.5518],
        [ 3.6211, -3.9535],
        [-1.6287,  1.7216],
        [ 2.1960, -2.5067],
        [ 1.9977, -2.2448],
        [-0.9295,  0.9438],
        [ 2.2512, -2.5578],
        [-2.5360,  2.6436],
        [-1.8890,  1.9787],
        [ 2.4500, -2.7050],
        [ 3.5502, -3.9974],
        [ 7.8740, -8.7413],
        [ 1.9768, -2.2287],
        [-0.9723,  1.0192],
        [ 5.3840, -5.9153],
        [-1.2483,  1.2866],
        [-1.4501,  1.5467],
        [-1.0471,  1.0899],
        [ 2.3409, -2.5763],
        [ 3.1816, -3.5639],
        [-1.8847,  1.9865],
        [-2.2041,  2.2781],
        [-2.7572,  2.8656],
        [-2.3390,  2.4441],
        [ 3.0862, -3.3945],
        [ 1.0977, -1.2327],
        [-1.7125,  1.7395],
        [ 2.8744, -3.2442],
        [ 1.8027, -2.0044],
        [-0.7821,  0.7521]], grad_fn=<AddmmBackward0>)

This is the code I wrote to get the embeddings as numpy arrays:

final = []
for element in final_embeddings:
    element.detach().numpy()
    final.append(element)

print(final)

This still gives me a list of tensors, not a 2D-numpy array. Using just element.numpy() gives me an error:

RuntimeError                              Traceback (most recent call last)
Cell In[139], line 3
      1 final = []
      2 for element in final_embeddings:
----> 3     element.numpy()
      4     final.append(element)
      6 print(final)

RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.

Can someone tell me what might be going wrong?


Solution

  • There is no need to iterate through the entries. You should be able to just do:

    final = final_embeddings.detach().numpy()