3

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)

Please look at the error.

20
  • pytorch.org/docs/stable/torch.html#torch.tensor suggests that tensor's first argument is data and its second is dtype. I don't think it makes much sense to do t = torch.tensor(2,3), because 3 isn't a type. If you want both 2 and 3 to be considered data, then they probably need to be in a list together. (Or a list of lists? Something like that) Commented Jul 3, 2018 at 18:04
  • 1
    I know, but it's important to keep the details straight or we'll end up going in circles. The other guy successfully did torch.Tensor(2,3), but he never tried torch.tensor(2,3). So we can't use the other question to inform our expectations about whether torch.tensor(2,3) should work. Commented Jul 3, 2018 at 18:13
  • 1
    @UmangGupta, i have edited the post with screenshot Commented Jul 3, 2018 at 18:17
  • 1
    Ok! This looks like a bug in pytorch on windows. Seems like error is in print not in torch.Tensor. Worth posting to pytorch discussion forum discuss.pytorch.org Commented Jul 3, 2018 at 18:19
  • 1
    Are you running your script straight from an ordinary command prompt? Some IDEs have extra-fancy object introspection powers that can sometimes inadvertently break fragile objects that don't expect to get prodded. Try running torch.Tensor(2,3) a different environment. Commented Jul 3, 2018 at 18:22

1 Answer 1

0

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.

ipython print tensor


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

Sign up to request clarification or add additional context in comments.

13 Comments

Ok, that explains why torch.tensor(2,3) didn't work. But why does torch.Tensor(2,3) cause a RuntimeError?
@Kevin, exactly. that was one of my question too
@kmario23, i just closed and tried it again. getting the same error
This could be caused by too big values in the initialized memory torch.Tensor() uses. See this for more information: github.com/pytorch/pytorch/issues/6339
No, it works perfectly fine even with running it as a script instead of in ipython
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.