Using torch.fft module to update torch.fft() deprecated functions

Viewed 20

I'm trying to update a repo since I was getting this error:

AttributeError: module 'torch' has no attribute 'rfft'

For instance, I replaced the line:

fftfull = torch.rfft(x,2)

by:

import torch.fft

fftfull = torch.fft.rfft2(x)

But now I'm getting this new error:

RuntimeError: The size of tensor a (129) must match the size of tensor b (256) at non-singleton dimension 5

in the last line of the following block:

xreal = fftfull[... , 0]
xim = fftfull[... ,1]
x = torch.cat((xreal.unsqueeze(1), xim.unsqueeze(1)), 1 ).unsqueeze( -3 )
x = torch.index_select( x, -2, self.indF[0] )
x = self.hl0 * x 

when I try to do element wise multiplication (*) with two tensors.

Since the problem is about tensor sizes, I guess the problem is the replacement I did to update is not generating the same result than before... but that is what I found on the internet to update these deprecated functions... I hope someone can help me.

Notice that the original x is an image converted to a tensor, but I don't know if that is important....

Update

OK, now I think I only needed to change the two first lines of the block by:

xreal = fftfull.real #fftfull[... , 0]
xim = fftfull.imag #fftfull[... ,1]

And now it works!

But there are other parts of the code I had to modify, and I'm not sure if I did property, for instance:

output[n] = torch.irfft(output[n], 2, signal_sizes = sig_size)

by

output[n] = torch.fft.irfftn(torch.view_as_complex(output[n]))

What happen with the parameters 2 and signal_sizes?

Finally I changed

imagefft = torch.rfft(im,2,onesided=False)

by

 imagefft = torch.fft.rfft2(im)

This last modification gives me the error

RuntimeError: The size of tensor a (129) must match the size of tensor b (2) at non-singleton dimension 4

When multiplying:

c = imagefft * filter.unsqueeze(-1).repeat(1,1,1,1,2)

and if I change im by torch.view_as_complex(im):

RuntimeError: Tensor must have a last dimension of size 2
0 Answers
Related