Concatenate a column to a tensor with different dimensions

username: zahra
cooked: <p>Hi,</p>
<p>I have a tensor like this:</p>
<pre><code class="lang-auto">tensor([[[[0., 0., 0.],
          [0., 0., 0.]]],

        [[[0., 0., 0.],
          [0., 0., 0.]]]])
</code></pre>
<p>I want to concatenate a column of 1 to tensor <strong>without any loop</strong> like this:</p>
<pre><code class="lang-auto">tensor([[[[1., 0., 0., 0.],
          [1., 0., 0., 0.]]],

        [[[1., 0., 0., 0.],
          [1., 0., 0., 0.]]]])
</code></pre>
<p>I try this code but I receive an error:</p>
<pre><code class="lang-auto">import torch
temp = torch.ones((2, 1, 2, 1))
input = torch.zeros((2, 1, 2, 3))
print(torch.cat((temp, input), 0))
</code></pre>
<p>Do you have any idea?</p>
<p>Many thanks</p>

username: LeviViana
cooked: <p>I think <code>print(torch.cat((temp, input), 3))</code> gives the desired results.</p>

username: zahra
cooked: <aside class="quote no-group" data-username="LeviViana" data-post="2" data-topic="51272">
<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/leviviana/48/9548_2.png" class="avatar"> LeviViana:</div>
<blockquote>
<p>print(torch.cat((temp, input), 3))</p>
</blockquote>
</aside>
<p>Thanks, you are right. What is 3?</p>

username: LeviViana
cooked: <p>3 is the dimension on which you want to concatenate.</p>

username: zahra
cooked: <p>Thanks a lot, based on what yo said, I wrote this code, but I do not know what are the numbers -1 and -2, do you have any idea about it?</p>
<pre><code class="lang-auto">import torch
input = torch.zeros((2, 1, 4, 2))
print(input)

temp = torch.ones((input.shape[0], input.shape[1], input.shape[2], 1))
#temp = torch.ones((2, 1, 4, 1))

input = torch.cat((temp, input), 3)
print(input)

input = torch.cat((input, temp), -1)
print(input)

temp = torch.ones((input.shape[0], input.shape[1], 1, input.shape[3]))

input = torch.cat((input, temp), -2)
print(input)

input = torch.cat((temp, input), -2)
print(input)

</code></pre>
<p>The result is this:</p>
<pre><code class="lang-auto">tensor([[[[1., 1., 1., 1.],
          [1., 0., 0., 1.],
          [1., 0., 0., 1.],
          [1., 0., 0., 1.],
          [1., 0., 0., 1.],
          [1., 1., 1., 1.]]],


        [[[1., 1., 1., 1.],
          [1., 0., 0., 1.],
          [1., 0., 0., 1.],
          [1., 0., 0., 1.],
          [1., 0., 0., 1.],
          [1., 1., 1., 1.]]]])
</code></pre>

username: LeviViana
cooked: <p>These negative dimension indexes are taken <code>mod input.ndimension</code> by <code>torch.cat</code>. In your first example, <code>-1 = 3 mod(4)</code>, where <code>input.ndimension() == 4</code>. In your second example, <code>-2 = 2 mod(4)</code> for the same reason.</p>

username: zahra
cooked: <p>Many thanks. So, for every one who have the same problem, we could rewrite the code so simple as bellow:</p>
<pre><code class="lang-auto">import torch
input = torch.zeros((2, 1, 4, 2))
print(input)

temp = torch.ones((input.shape[0], input.shape[1], input.shape[2], 1))
#temp = torch.ones((2, 1, 4, 1))

input = torch.cat((temp, input), 3)
print(input)

input = torch.cat((input, temp), 3)
print(input)

temp = torch.ones((input.shape[0], input.shape[1], 1, input.shape[3]))

input = torch.cat((input, temp), 2)
print(input)

input = torch.cat((temp, input), 2)
print(input)
</code></pre>
<p>Thanks again</p>