python - 无法使用割炬创建张量

标签 python runtime-error pytorch tensor

我试图如下创建张量。

import torch
t = torch.tensor(2,3)

我收到以下错误。

TypeError Traceback (most recent call last) in () ----> 1 a=torch.tensor(2,3)

TypeError: tensor() takes 1 positional argument but 2 were given



所以,我尝试了以下
import torch
t = torch.Tensor(2,3)
# No error while creating the tensor
# When i print i get an error
print(t)

我收到以下错误

RuntimeError Traceback (most recent call last) in () ----> 1 print(a)

D:\softwares\anaconda\lib\site-packages\torch\tensor.py in repr(self) 55 # characters to replace unicode characters with. 56 if sys.version_info > (3,): ---> 57 return torch._tensor_str._str(self) 58 else: 59 if hasattr(sys.stdout, 'encoding'):

D:\softwares\anaconda\lib\site-packages\torch_tensor_str.py in _str(self) 216 suffix = ', dtype=' + str(self.dtype) + suffix 217 --> 218 fmt, scale, sz = _number_format(self) 219 if scale != 1: 220 prefix = prefix + SCALE_FORMAT.format(scale) + ' ' * indent

D:\softwares\anaconda\lib\site-packages\torch_tensor_str.py in _number_format(tensor, min_sz) 94 # TODO: use fmod? 95 for value in tensor: ---> 96 if value != math.ceil(value.item()): 97 int_mode = False 98 break

RuntimeError: Overflow when unpacking long



但是,根据This SO Post,他能够创建张量。我在这里想念什么吗?另外,为什么我能用Tensor(大写T)创建张量而不用tensor(小t)创建张量

Please look at the error.

最佳答案

torch.tensor()期望使用序列或array_like创建张量,而torch.Tensor()类可以仅使用形状信息创建张量。

这是torch.tensor()的签名:

Docstring:
tensor(data, dtype=None, device=None, requires_grad=False) -> Tensor

Constructs a tensor with :attr:data.

Args:
data (array_like): Initial data for the tensor. Can be a list, tuple, NumPy ndarray, scalar, and other types.

dtype (:class:torch.dtype, optional): the desired data type of returned tensor.



关于RuntimeError:我无法在Linux发行版中重现该错误。从ipython终端打印张量可以很好地工作。

ipython print tensor

仔细检查该错误,这似乎仅在Windows OS中是一个问题。如评论中所述,看看issues/6339: Error when printing tensors containing large values

关于python - 无法使用割炬创建张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51160386/

相关文章:

Python 3.6 谷歌广告类型错误 : cannot use a string pattern on a bytes-like object

c++ - 尝试运行 SFML HelloWorld 代码,但出现错误

android - 相对布局中的循环依赖错误。

java构建工具maven和jar文件

python - 如何在 PyTorch 中使用 TPU?

python - 使用 pytorch/python,覆盖变量还是定义新变量更好?

python - ansible-container 不是 JSON 可序列化的

python - 是否可以改变 turtle 的笔划?

python - 从一维张量中提取前 k 个值索引

python - 如何在 Google Cloud 上部署 Python 3 Flask 项目