Search code examples
pythonpytorchcomplex-numbersstride

How can I get a view of input as a complex tensor? RuntimeError: Tensor must have a last dimension with stride 1


I have a tensor with 64 elements in pytorch and I want to convert it to a complex tensor with 32 elements. Order is important for me and everything should be in PyTorch so I can use it in my customized loss function: the first half in my primary tensor (W) are my real numbers and the second half are my imaginary ones. so my final tensor should be like:

W_final = tensor(W[0]+jW[32], W[1]+jW[33], W[2]+jW[34], W[3]+jW[35], ... , W[31]+jW[63])

I tried this approach:

import torch
W_1 = = torch.reshape(W,(2,32)) #reshape W with shape (64) to W_1 with shape (2,32) 
W_2 = torch.transpose(W_1,0,1) #transpose W_1 to W_2 with shape (32,2), so I can use view_as_complex
W_final = torch.view_as_complex(W_2)

The problem is that with transpose, the stride also changes and I get this error: RuntimeError: Tensor must have a last dimension with stride 1

Do know how can I deal with stride? or is there any way to reshape with different orders same as numpy? or any other way to convert to complex?


Solution

  • It has to do with the non contiguous memory allocation for W_2 after you do reshape. To handle this error you should call .contiguous() on W_2. From Pytorch Docs: " Strides are a list of integers: the k-th stride represents the jump in the memory necessary to go from one element to the next one in the k-th dimension of the Tensor. This concept makes it possible to perform many tensor operations efficiently." Once you call contiguous all dimensions of returned tensor will have stride 1.

    Here is a working sample code:

    import torch
    W = torch.randn(64)
    W_2 = W.view(-1,32).permute(1,0).contiguous()
    W_final = torch.view_as_complex(W_2)
    

    First call view to reshape tensor to shape (2,32), then permute dimensions to transpose the result and call contiguous.