拆包时溢出 - Pytorch

时间:2018-05-31 06:24:50

标签: python-3.x deep-learning pytorch

我正在运行以下代码

import torch
from __future__ import print_function
x = torch.empty(5, 3)
print(x)
在CPU模式下的Ubuntu机器上的

,它给出了以下错误,原因是什么以及如何修复

      x = torch.empty(5, 3)
----> print(x)

/usr/local/lib/python3.6/dist-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'):

/usr/local/lib/python3.6/dist-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

/usr/local/lib/python3.6/dist-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

1 个答案:

答案 0 :(得分:4)

因为,torch.empty()给出了未初始化的内存,所以你可能会或者可能不会从中获得大的值。试试

x = torch.rand(5, 3)
print(x)

这会给出回应。