admin管理员组文章数量:1320661
>>> a = torch.tensor([[1,2,3],[2,3,4],[3,4,5]])
>>> a
tensor([[1, 2, 3],
[2, 3, 4],
[3, 4, 5]])
If we convert this tensor to a numpy array and try to print it, this gives an error
>>> a = a.numpy()
>>> a
AttributeError: module 'numpy.core.multiarray' has no attribute 'generic'
The above exception was the direct cause of the following exception:
...
--> 794 output = repr(obj)
795 lines = output.splitlines()
796 with p.group():
RuntimeError: Unable to configure default ndarray.__repr__
>>> a = torch.tensor([[1,2,3],[2,3,4],[3,4,5]])
>>> a
tensor([[1, 2, 3],
[2, 3, 4],
[3, 4, 5]])
If we convert this tensor to a numpy array and try to print it, this gives an error
>>> a = a.numpy()
>>> a
AttributeError: module 'numpy.core.multiarray' has no attribute 'generic'
The above exception was the direct cause of the following exception:
...
--> 794 output = repr(obj)
795 lines = output.splitlines()
796 with p.group():
RuntimeError: Unable to configure default ndarray.__repr__
Share
Improve this question
asked Jan 18 at 15:59
GouherDanishGouherDanish
1361 silver badge9 bronze badges
2 Answers
Reset to default 1This happens because of the numpy version > 1.24
% pip list | grep numpy
numpy 1.26.4
Let's downgrade the numpy version
% pip install "numpy<1.24"
Installing collected packages: numpy
Attempting uninstall: numpy
Found existing installation: numpy 1.26.4
Uninstalling numpy-1.26.4:
Successfully uninstalled numpy-1.26.4
Successfully installed numpy-1.23.5
If we rerun our code, it will convert the tensor to numpy array properly
>>> a = a.numpy()
>>> a
array([[1, 2, 3],
[2, 3, 4],
[3, 4, 5]])
If you are really bothered by the version issue but not going to make any changes on the packages, probably you can detour a bit to achieve the goal, e.g.,
import numpy as np
np.array(a.tolist())
and then you will obtain
array([[1, 2, 3],
[2, 3, 4],
[3, 4, 5]])
本文标签: pythonConverting torch tensor to numpy array using numpy method gives an errorStack Overflow
版权声明:本文标题:python - Converting torch tensor to numpy array using .numpy method gives an error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742065907a2418832.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论