 # Norm of complex vector

Hey all,

When I do torch.norm(tensor) where tensor is vector shaped with complex values I get the following error:
ValueError: dtype argument is not supported in frobenius norm
Is there a way to take the 2-norm of such a tensor?

ps. I am using pytorch version: 1.7.0.dev20200906

Hi,

Complex support is work in progress I’m afraid see tracker so not everything is supported yet.

Also I can actually take the norm of simple complex vectors. Can you share a small code sample to reproduce your issue?

Hello Schalky and Alban!

The short answer is that you can implement your own correct
complex norm (see below).

Yes, I think this is the core issue.

On 1.6.0 (current stable), I can take the norm of a complex vector,
but the result isn’t correct. (I haven’t tried 1.7.) Pytorch is using the
complex square, rather than `x * x.conj()`.

This illustrates the issue, and shows how you can implement your
own correct version:

``````>>> import torch
>>> torch.__version__
'1.6.0'
>>> cvec = torch.randn (3, dtype = torch.cfloat)
>>> rvec = (cvec + cvec.conj()) / 2.0
>>> cvec
tensor([1.2428-0.6251j, 0.1556+0.8382j, 0.3308+0.8406j])
>>> rvec
tensor([1.2428+0.j, 0.1556+0.j, 0.3308+0.j])
>>> torch.norm (cvec)                 # incorrect, result should be real
tensor(0.5590-0.6590j)
>>> (cvec * cvec).sum()**0.5          # incorrect, no complex conjugate
tensor(0.5590-0.6590j)
>>> (cvec * cvec.conj()).sum()**0.5   # correct norm with complex conjugate
tensor(1.8650-5.5731e-09j)
>>> torch.norm (rvec)
tensor(1.2955+0.j)
>>> (rvec * rvec).sum()**0.5
tensor(1.2955+0.j)
>>> (rvec * rvec.conj()).sum()**0.5
tensor(1.2955+0.j)
``````

Best.

K. Frank

3 Likes

Hello Frank,

Thank you for your reply. I hadn’t found the .conj operation yet but I can definitely make do with this for now!

Best,
Schalky

Hi!

Say I run the following code:

``````import torch

x = torch.randn(2, dtype =torch.cfloat)
print(x)
norm = torch.norm(x)
print(norm)
``````

I get the error:

``````RuntimeError: frobenius norm not supported for complex tensors
``````

So seems strange that you can run it. Are you using the same pytorch version as I am?

I am running off the master branch. You can get a similar behavior by installing the nightly build.