The Transpose Method Arguments

username: gbox
cooked: <p>What does it mean in the <a href="https://pytorch.org/docs/stable/generated/torch.transpose.html" rel="noopener nofollow ugc">transpose method</a>:<br>
“dim0 - the first dimension to be transposed”<br>
“dim1 - the second dimension to be transposed”<br>
which values do they get?</p>
<pre><code class="lang-auto">a = torch.tensor(np.arange(1,7).reshape(2,3))
print(a)
b = torch.transpose(c,0,1)
print(b)
</code></pre>
<p>and</p>
<pre><code class="lang-auto">a = torch.tensor(np.arange(1,7).reshape(2,3))
print(a)
b = torch.transpose(c,1,0)
print(b)
</code></pre>
<p>are the same and is the known transpose row to column and vice-versa (A_ij=A_ji)<br>
but</p>
<pre><code class="lang-auto">a = torch.tensor(np.arange(1,7).reshape(2,3))
print(a)
b = torch.transpose(c,1,1)
print(b)
</code></pre>
<p>and</p>
<pre><code class="lang-auto">a = torch.tensor(np.arange(1,7).reshape(2,3))
print(a)
b = torch.transpose(c,0,0)
print(b)
</code></pre>
<p>do nothing.<br>
Thanks</p>

username: pascal_notsawo
cooked: <p>By definition the transpose permutes two dimensions of a vector.<br>
For example if I have a tensor <code>t1</code> of dimensions <code>(0, 1, ..., i, ..., j, ..., n-1) ~ (dim_0, dim_1, ..., dim_i, ..., dim_j, ..., dim_n-1)</code>, transposing the dimensions <code>i</code> and <code>j</code> is the same as transforming <code>t1</code> into a tensor <code>t2</code> of dimension <code>(0, 1, ..., i, ..., j, ..., n-1) ~ (dim_0, dim_1, ..., dim_j, ..., dim_i, ..., dim_n-1)</code></p>
<p>This is done with pytorch as follows: <code>t2 = torch.transpose(t1,i,j) # or t1.transpose(i,j)</code></p>

username: gbox
cooked: <pre><code class="lang-auto">a = torch.tensor(np.arange(1,7))
b = torch.transpose(a,0,2)
print(b)
</code></pre>
<p>returns Dimension out of range (expected to be in range of [-1, 0], but got 2)</p>

username: pascal_notsawo
cooked: <p>I think your problem is more mathematical than pytorch.</p>
<p>The only possible transpose of a vector is itself, since it is of dimension in <code>R</code> (I say of dimension, the vector can itself be an element of <code>R^n, n &gt;= 1</code>).<br>
For example <code>a = torch.arange(1, 7)</code> just creates a vector of <code>R^6</code>, but remains of dimension in <code>R</code>, a scalar (<code>a.shape = torch.Size([6]) ~ 6 € IR</code>), so the only possible transpose is <code>a.transpose(0,0)</code> and is <code>a</code>: we have only one dimension, the dimension 0 (<code>1-1</code>)</p>
<p>But <code>a = torch.rand((2, 4, 3))</code> is an element of <code>R^2 x R^4 x R^3</code>, so of dimension in <code>R^3</code> (<code>a.shape = torch.Size([2, 3, 7])</code>), so we can transpose the dimension :</p>
<ul>
<li>0 with 1 (<code>a.transpose(0, 1) = a.transpose(1, 0), of shape = torch.Size([4, 2, 3])</code>) or 2 (<code>a.transpose(0,2) = a.transpose(2,0)</code>)</li>
<li>1 with 2 (a.transpose(1, 2) = a.transpose(2, 1), of shape = torch.Size([2, 3, 4])`)</li>
<li>we can’t go beyond <code>2 = 3-1</code>, because the created tensor is of dimension in <code>R^3</code> (I say of dimension) : take some linear algebra course if necessary <img src="https://discuss.pytorch.org/images/emoji/apple/grinning.png?v=9" title=":grinning:" class="emoji" alt=":grinning:">
</li>
</ul>