How to create a rank 3 tensor in Pytorch?

pradeep_vasudev: <p>How to create a rank 3 tensor in Pytorch?<br>
Please share the co</p>

J_Johnson: <pre><code class="lang-auto">import torch

x=torch.rand(8)

print(x.size())

x=x.view(2,2,2)
print(x.size())
</code></pre>
<p>You can also use <code>.reshape()</code> depending on the application.</p>

pradeep_vasudev: <p><a class="mention" href="/u/j_johnson">@J_Johnson</a>  But when I checked the rank of the tensor that you provided using <strong>torch.linalg.matrix_rank()</strong> function it shows like <strong>tensor([2, 2])</strong>.<br>
Here is the code</p>
<p>import torch<br>
x=torch.rand(8)<br>
print(x.size())<br>
x=x.view(2,2,2)<br>
print(x.size())<br>
print(torch.linalg.matrix_rank(x))</p>
<p>Ideally, it should be <strong>3</strong> right if the rank is 3?</p>

J_Johnson: <aside class="quote no-group" data-username="pradeep_vasudev" data-post="3" data-topic="168770" 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/pradeep_vasudev/48/54343_2.png" class="avatar"> pradeep_vasudev:</div>
<blockquote>
<p><a class="mention" href="/u/j_johnson">@J_Johnson</a> But when I checked the rank of the tensor that you provided using <strong>torch.linalg.matrix_rank()</strong> function it shows like <strong>tensor([2, 2])</strong>.<br>
Here is the code</p>
<p>import torch<br>
x=torch.rand(8)<br>
print(x.size())<br>
x=x.view(2,2,2)<br>
print(x.size())<br>
print(torch.linalg.matrix_rank(x))</p>
<p>Ideally, it should be <strong>3</strong> right if the rank is 3?</p>
</blockquote>
</aside>
<p>That’s not the <strong>tensor</strong> rank.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/c/4c9560a61b138c5dc3f364c693092dc1c461cb6a.png" data-download-href="https://discuss.pytorch.org/uploads/default/4c9560a61b138c5dc3f364c693092dc1c461cb6a" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/4/c/4c9560a61b138c5dc3f364c693092dc1c461cb6a_2_690x164.png" alt="image" data-base62-sha1="aVukjtcwWTTm62KcEttjzsBVKqu" width="690" height="164" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/4/c/4c9560a61b138c5dc3f364c693092dc1c461cb6a_2_690x164.png, https://discuss.pytorch.org/uploads/default/original/3X/4/c/4c9560a61b138c5dc3f364c693092dc1c461cb6a.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/4/c/4c9560a61b138c5dc3f364c693092dc1c461cb6a.png 2x" data-dominant-color="F5F5F6"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">942×224 20.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p><a href="https://pytorch.org/docs/stable/generated/torch.linalg.matrix_rank.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/generated/torch.linalg.matrix_rank.html</a></p>
<p>See also:</p>
<aside class="onebox stackexchange" data-onebox-src="https://math.stackexchange.com/questions/2245849/is-the-rank-of-a-tensor-different-from-the-rank-of-a-matrix">
  <header class="source">

      <a href="https://math.stackexchange.com/questions/2245849/is-the-rank-of-a-tensor-different-from-the-rank-of-a-matrix" target="_blank" rel="noopener nofollow ugc">math.stackexchange.com</a>
  </header>

  <article class="onebox-body">
      <a href="https://math.stackexchange.com/users/439207/chanwcom" target="_blank" rel="noopener nofollow ugc">
    <img alt="chanwcom" src="https://www.gravatar.com/avatar/ff8bc1d961191331430e5e93886b1826?s=256&amp;d=identicon&amp;r=PG" class="thumbnail onebox-avatar" width="256" height="256">
  </a>

<h4>
  <a href="https://math.stackexchange.com/questions/2245849/is-the-rank-of-a-tensor-different-from-the-rank-of-a-matrix" target="_blank" rel="noopener nofollow ugc">Is the rank of a Tensor different from the rank of a Matrix?</a>
</h4>

<div class="tags">
  <strong>matrices, tensors, tensor-rank</strong>
</div>

<div class="date">
  asked by
  
  <a href="https://math.stackexchange.com/users/439207/chanwcom" target="_blank" rel="noopener nofollow ugc">
    chanwcom
  </a>
  on <a href="https://math.stackexchange.com/questions/2245849/is-the-rank-of-a-tensor-different-from-the-rank-of-a-matrix" target="_blank" rel="noopener nofollow ugc">01:10AM - 22 Apr 17 UTC</a>
</div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


pradeep_vasudev: <p><a class="mention" href="/u/j_johnson">@J_Johnson</a><br>
Thank you.<br>
If so what is the corresponding torch function or method to see the rank of a tensor?</p>

J_Johnson: <p>There is no built in method I’m aware of. But if you want the number of dims(i.e. tensor rank), you can just use <code>len(x.size())</code>.</p>

pradeep_vasudev: <p><a class="mention" href="/u/j_johnson">@J_Johnson</a>  Thank you</p>

tom: <aside class="quote no-group" data-username="J_Johnson" data-post="6" data-topic="168770">
<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/j_johnson/48/55494_2.png" class="avatar"> J_Johnson:</div>
<blockquote>
<p><code>len(x.size())</code>.</p>
</blockquote>
</aside>
<p><code>x.dim()</code></p>
<p>Best regards</p>
<p>Thomas</p>