如何从一个张量中提取张量,并将其转换成一个二维NumPy数组?

huangapple go评论67阅读模式
英文:

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

问题

我正在尝试从一个较大的张量中提取张量,并将其转换为一个二维的NumPy数组。(这个张量包含了通过图神经网络后的节点嵌入)。我正在使用PyTorch(Geometric)进行我的项目。我需要将这些单独的嵌入进一步处理。

这是我的张量:

tensor([[-0.7863,  0.8097],
        [-1.0679,  1.1331],
        ...
        [-0.7821,  0.7521]], grad_fn=<AddmmBackward0>)

这是我编写的代码,用来将嵌入提取为NumPy数组:

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

print(final)

这仍然给我一个张量列表,而不是一个二维的NumPy数组。
只使用element.numpy() 会给我一个错误:

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

有人能告诉我可能出了什么问题吗?

英文:

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=&lt;AddmmBackward0&gt;)

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:
----&gt; 3     element.numpy()
      4     final.append(element)
      6 print(final)

RuntimeError: Can&#39;t call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.

Can someone tell me what might be going wrong?

答案1

得分: 1

不需要遍历条目。你应该可以直接执行:

final = final_embeddings.detach().numpy()
英文:

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

final = final_embeddings.detach().numpy()

huangapple
  • 本文由 发表于 2023年7月17日 14:45:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702045.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定