[Python] Unable to create a tensor using torch.Tensor

Discussão em 'Python' iniciado por Stack, Julho 18, 2025.

  1. Stack

    Stack Membro Participativo

    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)

    [​IMG]

    Continue reading...

Compartilhe esta Página