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

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

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

问题

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

这是我的张量:

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

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

  1. final = []
  2. for element in final_embeddings:
  3. element.detach().numpy()
  4. final.append(element)
  5. print(final)

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

  1. 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:

  1. tensor([[-0.7863, 0.8097],
  2. [-1.0679, 1.1331],
  3. [-1.8162, 1.9160],
  4. [ 2.0584, -2.2741],
  5. [-1.8818, 1.9333],
  6. [ 0.7870, -0.8974],
  7. [ 6.1731, -6.8074],
  8. [ 7.3219, -8.0852],
  9. [-0.9933, 0.9439],
  10. [ 4.6217, -5.1856],
  11. [-1.3747, 1.4614],
  12. [ 4.6429, -5.0861],
  13. [ 3.1141, -3.4420],
  14. [ 2.6417, -2.9173],
  15. [-2.9696, 3.0740],
  16. [ 4.0654, -4.5340],
  17. [ 1.7143, -1.9558],
  18. [-1.7497, 1.8496],
  19. [-1.9055, 1.9934],
  20. [ 3.9273, -4.3356],
  21. [ 4.0350, -4.4137],
  22. [ 1.2770, -1.4225],
  23. [-1.7447, 1.8458],
  24. [ 1.3937, -1.5936],
  25. [ 3.2471, -3.5991],
  26. [ 2.2516, -2.6034],
  27. [ 1.3096, -1.4573],
  28. [-1.7823, 1.8775],
  29. [ 0.9923, -1.2175],
  30. [-1.1818, 1.2430],
  31. [ 1.0997, -1.2466],
  32. [ 0.4841, -0.5800],
  33. [ 4.1609, -4.5518],
  34. [ 3.6211, -3.9535],
  35. [-1.6287, 1.7216],
  36. [ 2.1960, -2.5067],
  37. [ 1.9977, -2.2448],
  38. [-0.9295, 0.9438],
  39. [ 2.2512, -2.5578],
  40. [-2.5360, 2.6436],
  41. [-1.8890, 1.9787],
  42. [ 2.4500, -2.7050],
  43. [ 3.5502, -3.9974],
  44. [ 7.8740, -8.7413],
  45. [ 1.9768, -2.2287],
  46. [-0.9723, 1.0192],
  47. [ 5.3840, -5.9153],
  48. [-1.2483, 1.2866],
  49. [-1.4501, 1.5467],
  50. [-1.0471, 1.0899],
  51. [ 2.3409, -2.5763],
  52. [ 3.1816, -3.5639],
  53. [-1.8847, 1.9865],
  54. [-2.2041, 2.2781],
  55. [-2.7572, 2.8656],
  56. [-2.3390, 2.4441],
  57. [ 3.0862, -3.3945],
  58. [ 1.0977, -1.2327],
  59. [-1.7125, 1.7395],
  60. [ 2.8744, -3.2442],
  61. [ 1.8027, -2.0044],
  62. [-0.7821, 0.7521]], grad_fn=&lt;AddmmBackward0&gt;)

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

  1. final = []
  2. for element in final_embeddings:
  3. element.detach().numpy()
  4. final.append(element)
  5. print(final)

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

  1. RuntimeError Traceback (most recent call last)
  2. Cell In[139], line 3
  3. 1 final = []
  4. 2 for element in final_embeddings:
  5. ----&gt; 3 element.numpy()
  6. 4 final.append(element)
  7. 6 print(final)
  8. 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

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

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

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

  1. 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:

确定