# Appending to a tensor

**URL:** <https://discuss.pytorch.org/t/appending-to-a-tensor/2665>\
**Category:** Uncategorized\
**Created:** [May 4, 2017, 10:10pm UTC](https://discuss.pytorch.org/t/appending-to-a-tensor/2665 "2017-05-04T22:10:57Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arash](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/da6949/32.png) [@Arash](https://discuss.pytorch.org/u/Arash)\
**Post date:** [May 4, 2017, 10:10pm UTC](https://discuss.pytorch.org/t/appending-to-a-tensor/2665/1 "2017-05-04T22:10:57Z")

</div>

Is there a way of appending a tensor to another tensor in pytorch? I can use x = torch.cat((x, out), 0) for example, but it creates a new copy of x which is time-consuming.  
Thanks!

---

<div class="post-metadata">

**Author:** ![ypxie](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/y/43a26b/32.png) [@ypxie](https://discuss.pytorch.org/u/ypxie)\
**Post date:** [May 4, 2017, 10:11pm UTC](https://discuss.pytorch.org/t/appending-to-a-tensor/2665/2 "2017-05-04T22:11:38Z")

</div>

it’s no that time-consuming.

---

<div class="post-metadata">

**Author:** ![smth](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/smth/32/13_2.png) [@smth](https://discuss.pytorch.org/u/smth)\
**Post date:** [May 5, 2017, 2:07am UTC](https://discuss.pytorch.org/t/appending-to-a-tensor/2665/3 "2017-05-05T02:07:16Z")

</div>

torch.cat is super efficient, and basically bandwidth bound. it’s not time consuming.

---

<div class="post-metadata">

**Author:** ![utkudiril](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/u/a698b9/32.png) [@utkudiril](https://discuss.pytorch.org/u/utkudiril)\
**Post date:** [January 21, 2018, 10:05pm UTC](https://discuss.pytorch.org/t/appending-to-a-tensor/2665/4 "2018-01-21T22:05:08Z")

</div>

torch.cat is fast when you are doing it once. But if you are preparing data and doing cat in each iteration, it gets really slow when the tensor you are generating gets very large. My solution was to cat into a temp tensor and move it to the real tensor every N iterations. Not sure if there is a more elegant solution to this.

---

<div class="post-metadata">

**Author:** ![michaelklachko](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/michaelklachko/32/541_2.png) [@michaelklachko](https://discuss.pytorch.org/u/michaelklachko)\
**Post date:** [August 16, 2018, 7:23pm UTC](https://discuss.pytorch.org/t/appending-to-a-tensor/2665/5 "2018-08-16T19:23:55Z")

</div>

I need to transform each input tensor in the following manner, for each iteration:

```auto
#input_batch shape: (64, in_channels, 224, 224)
outputs = []

for ch in range(in_channels):
    tensor = my_fn(input_batch[:,ch,:,:]) #transform from (64, 1, 224, 224) to (64, 32, 224, 224)
    outputs.append(tensor)
result = torch.cat(outputs, dim=1) #shape (64, 32*in_channels, 224, 224)

```

in\_channels is typically 3, but can be more.

Is appending to list better than doing torch.cat incrementally inside the loop?

---

<div class="post-metadata">

**Author:** ![holiv](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/h/c77e96/32.png) [@holiv](https://discuss.pytorch.org/u/holiv)\
**Post date:** [November 2, 2018, 12:51am UTC](https://discuss.pytorch.org/t/appending-to-a-tensor/2665/6 "2018-11-02T00:51:59Z")

</div>

Dear smth, is there anyway i can concatenate list tensors inside the loop without using stack/cat?

This the example of my case: I want to manipulate output, but because it is a list of tensors i want to change it to one tensor. So as the loop becomes too long the memory becomes full because the stack operation.

```auto
output = []
start=time.time()
for i in range(2):
        hx= rnn(input[:,i,:], (hx_0))
        output.append(hx)
        outs1 = torch.stack(output,1) 
         hx_0=operation(outs1)

print(output)

```

---

<div class="post-metadata">

**Author:** ![smth](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/smth/32/13_2.png) [@smth](https://discuss.pytorch.org/u/smth)\
**Post date:** [November 2, 2018, 1:53am UTC](https://discuss.pytorch.org/t/appending-to-a-tensor/2665/7 "2018-11-02T01:53:07Z")

</div>

@holiv you can pre-allocate a larger Tensor, and then just copy into a slice.

```auto
max_output_size = 10
output_cat = None

start=time.time()
for i in range(2):
        hx= rnn(input[:,i,:], (hx_0))
        output.append(hx)

        if output_cat is None:
            output_cat_size = list(hx.size())
            output_cat_size.insert(1, max_output_size)
            output_cat = torch.empty(*output_cat_size, dtype=hx.dtype, device=hx.device)

        output_cat[:, i] = hx
        hx_0 = operation(output_cat[:, 0:i])

print(output_cat)

```

---

<div class="post-metadata">

**Author:** ![holiv](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/h/c77e96/32.png) [@holiv](https://discuss.pytorch.org/u/holiv)\
**Post date:** [November 2, 2018, 11:59am UTC](https://discuss.pytorch.org/t/appending-to-a-tensor/2665/8 "2018-11-02T11:59:04Z")

</div>

Dear @smth thank you for your reply. However, **output\_cat** does not contain anything . Because my operation have inside unsqueeze, then error is thrown **cannot unsqueeze empty tensor**. I addtion, I don’t see the connection between both **output\_cat** and **output.append**. Also, if i try to replace **output.append** with **output\_cat.append** , the error says that ' **‘NoneType’ object has no attribute ‘append’**.  
Thank you.

---

<div class="post-metadata">

**Author:** ![solsol](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/solsol/32/9840_2.png) [@solsol](https://discuss.pytorch.org/u/solsol)\
**Post date:** [February 19, 2019, 5:45am UTC](https://discuss.pytorch.org/t/appending-to-a-tensor/2665/9 "2019-02-19T05:45:06Z")

</div>

What if we want to concate two inputs?

outputs = []

for i in range(in\_channels):  
for j in range(in\_channels):  
tensor = torch.cat(input\_batch1[:,i,:,:], input\_batch2[:,j,:,:], dim=1) #transform from (64, 1, 224, 224) to (64, 32, 224, 224)  
outputs.append(tensor)  
result = torch.cat(outputs, dim=1)

---

<div class="post-metadata">

**Author:** ![Ashima\_Garg](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@Ashima\_Garg](https://discuss.pytorch.org/u/Ashima_Garg)\
**Post date:** [January 10, 2020, 6:56am UTC](https://discuss.pytorch.org/t/appending-to-a-tensor/2665/10 "2020-01-10T06:56:26Z")

</div>

Hi, Is there a way to declare output = [] in your code as torch.tensor and append in it similar to python numpy list?

---

<div class="post-metadata">

**Author:** ![akib62](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/3bc359/32.png) [@akib62](https://discuss.pytorch.org/u/akib62)\
**Post date:** [October 9, 2020, 10:02pm UTC](https://discuss.pytorch.org/t/appending-to-a-tensor/2665/11 "2020-10-09T22:02:32Z")

</div>

Hi @michaelklachko, I am trying to do something like that though I am not sure (I am new in this field) and trying to learn. I wrote something like that (below) and it took almost 2H (hanged) and I have to shut down my laptop (forced shutdown). Is it ok to write something like that?  
I have a test\_dataloader and it contains 14000 tensors. My test images are 28000 and I am taking batch = 2. I have a pre-trained model and now I am trying to test my existing model.

```auto
final_output = []
for i, data in enumerate(test_data): //test_data is a DataLoader
    data = data.unsqueeze(1)
    output = model(data)
    final_output.append(output)
result = torch.cat(final_output, dim=1) 

```

Could you tell me what I have to do?

---

<div class="post-metadata">

**Author:** ![michaelklachko](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/michaelklachko/32/541_2.png) [@michaelklachko](https://discuss.pytorch.org/u/michaelklachko)\
**Post date:** [October 9, 2020, 11:43pm UTC](https://discuss.pytorch.org/t/appending-to-a-tensor/2665/12 "2020-10-09T23:43:31Z")

</div>

Try removing output = model(data) line and see how fast it goes

---

<div class="post-metadata">

**Author:** ![RicoSuaveGuapo](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ricosuaveguapo/32/39858_2.png) [@RicoSuaveGuapo](https://discuss.pytorch.org/u/RicoSuaveGuapo)\
**Post date:** [December 23, 2020, 8:45am UTC](https://discuss.pytorch.org/t/appending-to-a-tensor/2665/13 "2020-12-23T08:45:50Z")

</div>

Like this one?

```python
# note that a is in dtype float32 (by default)
# the one to be cat has to be also 
# with dtype of float32.
a = torch.tensor(())
for i in range(3):
    # if i = torch.tensor(1)
    # it cannot be cat, since it has 
    # zero dimention.
    # Also use .float() to make sure that they 
    # are in the same dtype
    i = torch.tensor([i]).float()
    a = torch.cat((a, i), 0)
print(a)
tensor([1., 2., 3.])

```
