Unable to create a tensor using torch.Tensor

核能气质少年 提交于 2019-12-10 22:40:52

问题


i was trying to create a tensor as below.

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

i got the following error.

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

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

so, i tried the following

import torch
t = torch.Tensor(2,3)
# No error while creating the tensor
# When i print i get an error
print(t)

i get the following error

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

But, according to This SO Post, he was able to create a tensor. Am i missing something here. Also, why was i able to create a tensor with Tensor(capital T) and not with tensor(small t)


回答1:


torch.tensor() expects a sequence or array_like to create a tensor whereas torch.Tensor() class can create a tensor with just shape information.

Here's the signature of 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.


Regarding the RuntimeError: I cannot reproduce the error in Linux distros. Printing the tensor works perfectly fine from ipython terminal.


Taking a closer look at the error, this seems to be a problem only in Windows OS. As mentioned in the comments, have a look at the issues/6339: Error when printing tensors containing large values



来源:https://stackoverflow.com/questions/51160386/unable-to-create-a-tensor-using-torch-tensor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!