gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 09:07:09 24 4
gpt4 key购买 nike

我试图如下创建张量。

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com