Skip to content

Can't convert numpy to tensor #12093

@Darel13712

Description

@Darel13712

Issue description

torch.tensor() fails at certain long arrays

Code example

>>> import torch
>>> import numpy as np
>>> a = np.random.random(1239*25).reshape(-1, 25).astype(np.float16)
>>> torch.tensor(a[:40])

works just fine:

tensor([[0.7393, 0.1298, 0.9209, 0.3218, 0.0799, 0.0392, 0.3503, 0.8389, 0.5161,
         0.4705, 0.3870, 0.8120, 0.7759, 0.3667, 0.9966, 0.4863, 0.9233, 0.4229,
         0.8965, 0.8398, 0.8677, 0.7915, 0.5654, 0.3433, 0.3972],
...

But...

>>> torch.tensor(a[:41])

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/usr/anaconda3/lib/python3.6/site-packages/IPython/core/formatters.py in __call__(self, obj)
    700                 type_pprinters=self.type_printers,
    701                 deferred_pprinters=self.deferred_printers)
--> 702             printer.pretty(obj)
    703             printer.flush()
    704             return stream.getvalue()

/usr/anaconda3/lib/python3.6/site-packages/IPython/lib/pretty.py in pretty(self, obj)
    393                             if callable(meth):
    394                                 return meth(obj, self, cycle)
--> 395             return _default_pprint(obj, self, cycle)
    396         finally:
    397             self.end_group()

/usr/anaconda3/lib/python3.6/site-packages/IPython/lib/pretty.py in _default_pprint(obj, p, cycle)
    508     if _safe_getattr(klass, '__repr__', None) is not object.__repr__:
    509         # A user-provided repr. Find newlines and replace them with p.break_()
--> 510         _repr_pprint(obj, p, cycle)
    511         return
    512     p.begin_group(1, '<')

/usr/anaconda3/lib/python3.6/site-packages/IPython/lib/pretty.py in _repr_pprint(obj, p, cycle)
    699     """A pprint that just redirects to the normal repr function."""
    700     # Find newlines and replace them with p.break_()
--> 701     output = repr(obj)
    702     for idx,output_line in enumerate(output.splitlines()):
    703         if idx:

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

/usr/anaconda3/lib/python3.6/site-packages/torch/_tensor_str.py in _str(self)
    254             suffix += ', dtype=' + str(self.dtype)
    255 
--> 256         formatter = _Formatter(get_summarized_data(self) if summarize else self)
    257         tensor_str = _tensor_str(self, indent, formatter, summarize)
    258 

/usr/anaconda3/lib/python3.6/site-packages/torch/_tensor_str.py in get_summarized_data(self)
    214             return self
    215     if self.size(0) > 2 * PRINT_OPTS.edgeitems:
--> 216         start = [get_summarized_data(self[i]).view(-1) for i in range(0, PRINT_OPTS.edgeitems)]
    217         end = ([get_summarized_data(self[i]).view(-1)
    218                for i in range(len(self) - PRINT_OPTS.edgeitems, len(self))])

/usr/anaconda3/lib/python3.6/site-packages/torch/_tensor_str.py in <listcomp>(.0)
    214             return self
    215     if self.size(0) > 2 * PRINT_OPTS.edgeitems:
--> 216         start = [get_summarized_data(self[i]).view(-1) for i in range(0, PRINT_OPTS.edgeitems)]
    217         end = ([get_summarized_data(self[i]).view(-1)
    218                for i in range(len(self) - PRINT_OPTS.edgeitems, len(self))])

/usr/anaconda3/lib/python3.6/site-packages/torch/_tensor_str.py in get_summarized_data(self)
    210     if dim == 1:
    211         if self.size(0) > 2 * PRINT_OPTS.edgeitems:
--> 212             return torch.cat((self[:PRINT_OPTS.edgeitems], self[-PRINT_OPTS.edgeitems:]))
    213         else:
    214             return self

RuntimeError: _cat is not implemented for type torch.HalfTensor

Latest Pytorch

# conda list pytorch
# packages in environment at /usr/anaconda3:
#
# Name                    Version                   Build  Channel
pytorch                   0.4.1            py36ha74772b_0

Metadata

Metadata

Assignees

No one assigned

    Labels

    todoNot as important as medium or high priority tasks, but we will work on these.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions