Search code examples
pytorchtensor

How to convert a tensor scalar to a tensor list without detach it


I have a tensor.long scalar named x. how can I make a list of tensor tensor[(x.detached().numpy())] without converting x to numpy


Solution

  • If I understand correctly, you want to add another dimension (by converting to a list, you'd have an iterable container/wrapper like a dimensional tensor; unless you want any list-specific operations, most of the time you want to stick to a tensor object for further processing).

    To add a dim (making the tensor 1-D), you can use unsqueeze:

    x.unsqueeze(dim=0)
    

    view/reshape/index-ing can be used as well:

    x.view(-1)
    x.reshape(-1)
    x[None]