Norm of complex vector

Schalky: <p>Hey all,</p>
<p>When I do torch.norm(tensor) where tensor is vector shaped with complex values I get the following error:<br>
ValueError: dtype argument is not supported in frobenius norm<br>
Is there a way to take the 2-norm of such a tensor?</p>
<p>ps. I am using pytorch version:  1.7.0.dev20200906</p>

albanD: <p>Hi,</p>
<p>Complex support is work in progress I’m afraid see <a href="https://github.com/pytorch/pytorch/issues/33152">tracker</a> so not everything is supported yet.</p>
<p>Also I can actually take the norm of simple complex vectors. Can you share a small code sample to reproduce your issue?</p>

KFrank: <p>Hello Schalky and Alban!</p>
<p>The short answer is that you can implement your own correct<br>
complex norm (see below).</p>
<aside class="quote no-group" data-username="albanD" data-post="2" data-topic="97803" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/48/215_2.png" class="avatar"> albanD:</div>
<blockquote>
<p>Complex support is work in progress I’m afraid</p>
</blockquote>
</aside>
<p>Yes, I think this is the core issue.</p>
<aside class="quote no-group">
<blockquote>
<p>Also I can actually take the norm of simple complex vectors.</p>
</blockquote>
</aside>
<p>On 1.6.0 (current stable), I can take the norm of a complex vector,<br>
but the result isn’t correct.  (I haven’t tried 1.7.)  Pytorch is using the<br>
complex square, rather than <code>x * x.conj()</code>.</p>
<p>This illustrates the issue, and shows how you can implement your<br>
own correct version:</p>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'1.6.0'
&gt;&gt;&gt; cvec = torch.randn (3, dtype = torch.cfloat)
&gt;&gt;&gt; rvec = (cvec + cvec.conj()) / 2.0
&gt;&gt;&gt; cvec
tensor([1.2428-0.6251j, 0.1556+0.8382j, 0.3308+0.8406j])
&gt;&gt;&gt; rvec
tensor([1.2428+0.j, 0.1556+0.j, 0.3308+0.j])
&gt;&gt;&gt; torch.norm (cvec)                 # incorrect, result should be real
tensor(0.5590-0.6590j)
&gt;&gt;&gt; (cvec * cvec).sum()**0.5          # incorrect, no complex conjugate
tensor(0.5590-0.6590j)
&gt;&gt;&gt; (cvec * cvec.conj()).sum()**0.5   # correct norm with complex conjugate
tensor(1.8650-5.5731e-09j)
&gt;&gt;&gt; torch.norm (rvec)
tensor(1.2955+0.j)
&gt;&gt;&gt; (rvec * rvec).sum()**0.5
tensor(1.2955+0.j)
&gt;&gt;&gt; (rvec * rvec.conj()).sum()**0.5
tensor(1.2955+0.j)
</code></pre>
<p>Best.</p>
<p>K. Frank</p>

Schalky: <p>Hello Frank,</p>
<p>Thank you for your reply. I hadn’t found the .conj operation yet but I can definitely make do with this for now!</p>
<p>Best,<br>
Schalky</p>

Schalky: <p>Hi!</p>
<p>Say I run the following code:</p>
<pre><code class="lang-auto">import torch

x = torch.randn(2, dtype =torch.cfloat)
print(x)
norm = torch.norm(x)
print(norm)
</code></pre>
<p>I get the error:</p>
<pre><code class="lang-auto">RuntimeError: frobenius norm not supported for complex tensors
</code></pre>
<p>So seems strange that you can run it. Are you using the same pytorch version as I am?</p>

albanD: <p>I am running off the master branch. You can get a similar behavior by installing the nightly build.</p>