does pytorch support complex numbers?

Question:

Minimum (not) working example


kernel = Conv2d(in_channels=1, out_channels=1, kernel_size=(3, 2))
data = torch.rand(1, 1, 100, 100).type(torch.complex64)
kernel(data)

yields RuntimeError: "unfolded2d_copy" not implemented for 'ComplexDouble' for 64 and 128 bit complex numbers, while for 32 bit, i get RuntimeError: "copy_" not implemented for 'ComplexHalf'.

Am I missing something, or is pytorch missing support for complex numbers??

note: I’m on macbook, using cpu only.

Asked By: blue_note

||

Answers:

Currently (@ latest stable version – 1.9.0) Pytorch is missing support for such operations on complex tensors (which are a beta feature). See this feature request at Native implementation of convolution for complex numbers

Splitting into convolution on real & image separately, though not ideal, is the way to go for now.

Answered By: Gil Pinsky

Pytorch is increasingly adding support for complex numbers. Some doc here

  • Since v1.6 (28 July 2020), pytorch now supports complex vectors and complex gradients as BETA (no longer as BETA since v1.9 I think).
  • Since v1.12 (28 June 2022), support was added for Complex32 and Complex Convolutions in PyTorch (also as BETA).

PyTorch today natively supports complex numbers, complex autograd,
complex modules, and numerous complex operations, including linear
algebra and Fast Fourier Transform (FFT) operators. Many libraries,
including torchaudio and ESPNet, already make use of complex numbers
in PyTorch, and PyTorch 1.12 further extends complex functionality
with complex convolutions and the experimental complex32 (“complex
half”) data type that enables half precision FFT operations. Due to
the bugs in CUDA 11.3 package, we recommend using CUDA 11.6 package
from wheels if you are using complex numbers.


Tesorflow

Until version v1.12, Tensorflow always had the same or more support for complex numbers. Now, PyTorch has the complex convolution which Tensorflow does not have.


Third-party libraries do exist for implementing Complex-Valued Neural Networks (CVNN):

I believe the best library if you want to use Pytorch is ComplexPytorch

If you are more into Tensorflow, let me recommend you my own library that uses Tensorflow as the back-end, and I emulate (or at least try to emulate) TensorFlow API and UX. I also made a lot of documentation on the library.

Finally, I would recommend this paper, the code used Theano, which is no longer maintained, but it has a lot of theory on how to implement CVNN. I also published my own paper with details about Complex-Valued Neural Networks modules such as activation function, layers, losses, etc.

Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.