What is the best way to append scalar to tensor

Ahmed_m: <p>Hi,</p>
<p>I need to know what is the best way (i.e. most efficient) to append a scalar value (i.e. tensor with empty size) to a tensor with multidimensional shape. I tried <code>torch.cat</code> and <code>torch.stack</code> but this requires the dimensions to be matched. I could use <code>unsqueeze</code> to the scalar value but I wonder if there is a better solution.</p>
<p>Thanks</p>

tom: <p>Unsqueezing / viewing seems to be the right thing to do.</p>
<p>You didn’t ask but just in case: It is good to avoid loops of incremental cat’s and instead collect things to cat into a vector and put them in in one go.<br>
An alternative if you know the size in advance is to allocate a <code>torch.empty</code> of the new size and assign to that.</p>

Ahmed_m: <p>Thanks! … the problem is that I am trying to implement a recursive (auto-regressive) filter and unfortunately in Pytorch only FIR filtering (i.e. just multiply and add without auto-regression) is supported via <code>conv1d</code>. That’s why I need to use looping in order to filter the signal on a sample-by-sample basis and append the output sample to manipulate the next samples and so on.</p>

eacousineau: <p>Thank y’all for the discussion! I ended up doing the unsqueeze + squeeze route; needed to understand a problem that a colleague ran into (as I’m sure I’ll run into it myself at some point <img src="https://discuss.pytorch.org/images/emoji/apple/stuck_out_tongue.png?v=9" title=":stuck_out_tongue:" class="emoji" alt=":stuck_out_tongue:">):</p>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/EricCousineau-TRI/repro/blob/53bca048b58b2146e4823c829a3b3e196f1815b7/python/notebooks/torch_gradient.ipynb" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/EricCousineau-TRI/repro/blob/53bca048b58b2146e4823c829a3b3e196f1815b7/python/notebooks/torch_gradient.ipynb" target="_blank" rel="nofollow noopener">EricCousineau-TRI/repro/blob/53bca048b58b2146e4823c829a3b3e196f1815b7/python/notebooks/torch_gradient.ipynb</a></h4>
<pre><code class="lang-ipynb">{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1.6.0'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
</code></pre>

  This file has been truncated. <a href="https://github.com/EricCousineau-TRI/repro/blob/53bca048b58b2146e4823c829a3b3e196f1815b7/python/notebooks/torch_gradient.ipynb" target="_blank" rel="nofollow noopener">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<pre><code class="lang-auto">def scalars_to_tensor_good(*a):
    """
    Will preserve gradient!
    """
    # https://discuss.pytorch.org/t/how-to-concatenate-to-a-tensor-with-a-0-dimension/6478
    # https://discuss.pytorch.org/t/what-is-the-best-way-to-append-scalar-to-tensor/54445/3
    a = [ai.unsqueeze(0) for ai in a]
    return torch.cat(a).squeeze(0)
</code></pre>