An ambiguous error when converting from Pytorch tensor to numpy

Abdelpakey: <p>Hi,</p>
<p>I have a PyTorch tensor of size torch.Size([49, 49]):</p>
<p>sw_bar_tensor = tensor([[ 1.1895e+01,  4.9031e-01,  3.8824e-01,  …,  1.0092e-01,<br>
3.0263e-02,  9.5312e-03],<br>
[ 4.9031e-01,  1.2864e+01, -1.0768e+00,  …, -9.6299e-02,<br>
-7.1840e-03,  2.4902e-02],<br>
[ 3.8824e-01, -1.0768e+00,  1.3478e+01,  …, -3.1151e-02,<br>
2.2860e-02, -1.0342e-03],<br>
…,<br>
[ 1.0092e-01, -9.6299e-02, -3.1151e-02,  …,  1.7947e-01,<br>
-7.7783e-03,  2.0708e-03],<br>
[ 3.0263e-02, -7.1840e-03,  2.2860e-02,  …, -7.7783e-03,<br>
1.2594e-01,  3.5290e-03],<br>
[ 9.5312e-03,  2.4902e-02, -1.0342e-03,  …,  2.0708e-03,<br>
3.5290e-03,  6.8233e-02]], device=‘cuda:0’, dtype=torch.float64)</p>
<p>I’m calculating its null space using scipy.linalg.null_space function, so I converted the tensor to numpy array  as follows:</p>
<p>arr_numpy= torch.Tensor.numpy(sw_bar_tensor.detach().cpu())<br>
or<br>
arr_numpy = sw_bar_tensor.detach().cpu().numpy()</p>
<h1>Calculate nullspace</h1>
<p>null_space(arr_numpy)</p>
<p>it gives me an  empty array:<br>
Out[544]: array([], shape=(49, 0), dtype=float64)</p>
<p>however, have the same values of the tensor but stored in a numpy array from another function, i.e, it was created as a numpy array at the first place. when I calculate its null space using the same function null_space(a_nump) it gives me a result (not an empty array), it behaves normally.</p>
<p>Does anyone know the problem or am I missing something or this is a bug in Pytorch?</p>

anantguptadbl: <aside class="quote no-group" data-username="Abdelpakey" data-post="1" data-topic="70647">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/e47774/48.png" class="avatar"> Abdelpakey:</div>
<blockquote>
<p>arr_numpy</p>
</blockquote>
</aside>
<p>This has to do with the rcond condition of the scipy null_space function. If you do not provide rcond it takes a weird default</p>
<pre><code class="lang-auto">if rcond is None:
        rcond = numpy.finfo(s.dtype).eps * max(M, N)
</code></pre>
<p>Here is a working code</p>
<pre><code class="lang-auto">import torch
from scipy.linalg import null_space
sw_bar_tensor=torch.FloatTensor(49, 49).to('cuda')
arr_numpy=sw_bar_tensor.detach().cpu().numpy()
null_space(arr_numpy,rcond=1.)
</code></pre>
<p><strong>Result</strong></p>
<pre><code class="lang-auto">array([[-1.74963553e-01,  0.00000000e+00],
       [ 4.51028104e-16, -9.12652367e-16],
       [ 2.33284737e-01,  1.45436634e-15],
       [ 1.38777878e-16, -1.54846174e-16],
       [ 1.16642369e-01,  9.48298648e-16],
       [ 5.55111512e-16, -1.01050836e-15],
       [ 7.07767178e-16,  4.64086501e-16],
       [ 2.26207941e-15, -3.16227766e-01],
       [ 1.74963553e-01,  7.21644966e-16],
</code></pre>

Abdelpakey: <p>Thank you <a class="mention" href="/u/anantguptadbl">@anantguptadbl</a>!<br>
I know that I can use rcond but in this case it will give me all orthogonal basis which I don’t want.</p>
<p>I just figured the problem, in case of anyone experiences this problem, the tensor sw_bar_tensor was  of type of float64 and for some reason when you convert it to lower type (e.g., float 32, float 16) it will work.</p>

anantguptadbl: <p>Thanks for the update <a class="mention" href="/u/abdelpakey">@Abdelpakey</a></p>