Torch.empty doesn't work

When I try to create a tensor using torch.empty() i get the following:

>>> torch.empty(5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/diego/anaconda3/lib/python3.6/site-packages/torch/tensor.py", line 57, in __repr__
    return torch._tensor_str._str(self)
  File "/home/diego/anaconda3/lib/python3.6/site-packages/torch/_tensor_str.py", line 218, in _str
    fmt, scale, sz = _number_format(self)
  File "/home/diego/anaconda3/lib/python3.6/site-packages/torch/_tensor_str.py", line 96, in _number_format
    if value != math.ceil(value.item()):
RuntimeError: Overflow when unpacking long

What am I doing wrong?

PD: I am using Pytorch 0.4.0

You’re not doing anything wrong! You can carry on and keep using your tensor. This is a tensor printing bug.

So I can apply operations to it, but I can’t print its value? Why does this happens? And is there any workaround. Thank you

There will probably be a new nightly package sometime that will fix this printing bug.

And yes, that is correct. You can apply operations but can’t print the value.

This happens because torch.empty initializes your tensor with “un-initialized” data. Some of this data happened to have very, very large float values. The tensor printing code attempts to convert this number to an int (to see if it would be better printed as an integer), causing an overflow because there is a limit on how large a python int/long can be.

2 Likes

It might be noted that the newbie “beginner-blitz” tutorial leads off with:
x = torch.empty(5, 3)
print(x)
which, of course, fails.

Might be worth tweaking the tutorial, to avoid putting off people checking out the package.

This bug should be fixed by now. Are you still getting an error?

Thanks for the reply.

Yes; starting with a blank notebook:

from future import print_function
import torch
x = torch.empty(5, 3)
print(x)

Generates the following:
AttributeError: module ‘torch’ has no attribute ‘empty’

I got a series of other errors early on in the tutorial as well:

x = torch.zeros(5, 3, dtype=torch.long)
AttributeError: module ‘torch’ has no attribute ‘long’

x = torch.tensor([5.5, 3])
Only works with torch.Tensor([5.5, 3]) (capitalized)

I’m using a fresh installation of PyTorch for a fastai course – have posted there to see if they’re using a modified or old version. They seem really enthusiastic and dedicated to PyTorch (and recommend the tutorial), so I was surprised at the many immediate problems. Any advice much appreciated.

Your PyTorch version is most likely pre 0.4.0.
Could you check it with:

import torch
print(torch.__version__)

I don’t know which version the fast.ai wrapper is using and I’m not sure, if the latest stable release is already implemented.

CC @jphoward

yep, that’s the problem: 0.3.1.post2

will check with them to see if it can be updated

thanks much