Apply torch.nn.functional.conv1d to each row of two tensors

mscipio: <p>Hi! First time posting in this forum, and it will be with a rather weird question, as I am attempting to use pytorch for something it’s probably not really designed for.</p>
<p>So, the big picture is that I am trying to use Pytorch’s optimizers to perform <strong>non-linear curve fitting</strong>. I have an overall code that is working, but now I need to tweek things to actually work with the model I am interested in. In details, at some point in my model’s forward function I need to convolve the system impulse response function (which is an exponential), and the system input fucntion (which a known measure), to get the output.</p>
<p>Here is a snippet of code that works:</p>
<pre><code class="lang-auto">out = torch.mul(kep,t)
out = torch.exp(-out)
res = torch.squeeze(torch.nn.functional.conv1d(out.view(Nv, 1, Nt),
                                     torch.flip(Cp,dims=(1,)).view(1, 1, Nt), 
                                     padding=(Nt - 1) ))[:,:Nt]
</code></pre>
<p>Here is a quick outline of the dimensions of each tensor:</p>
<ul>
<li>
<strong><code>kep</code></strong> <code>[Nv,1]</code>
</li>
<li>
<strong><code>t</code></strong> <code>[1,Nt]</code>
</li>
<li>
<strong><code>out</code></strong><code> [Nv,Nt]</code> &lt;-- system’s impulse response function</li>
<li>
<strong><code>Cp</code></strong><code> [1,Nt]</code> &lt;-- system’s input function</li>
<li>
<strong><code>res</code></strong> <code>[Nv,Nt]</code> &lt;-- system’s output function</li>
</ul>
<p>So, if I use the same “kernel” <code>Cp</code> as input for all my <code>Nv</code> time-series, this works fine.<br>
What I need to do, instead, is to allow each time series to have a different input (i.e. <strong>Cp</strong> [Nv,Nt])</p>
<p>If I change my last line as follows:</p>
<pre><code class="lang-auto">out = torch.squeeze(torch.nn.functional.conv1d(out.view(Nv, 1, Nt),
                                     torch.flip(Cp,dims=(1,)).view(Nv, 1, Nt),
                                     padding=(Nt - 1) ))
</code></pre>
<p>the output I get is of shape <code>[Nv,Nv,Nt]</code>.<br>
This makes sense, given that the first dimension of the kernel is interpreted as <em><span class="hashtag">#output</span> channels</em>, but it’s not what I want to do. <strong>I’d like to be able to do a 1d convolution of each row of <code>out</code> with each row of <code>Cp</code>. Is there any way I can do this?</strong></p>

albanD: <p>Hi,</p>
<p>Unfortunately, you won’t be able to give a batch of weights to the conv function as it does not support it.</p>
<p>But you can use the <code>groups</code> parameter. In particular, if your groups = n_channels, then each channel will be only multiplied with one weight. Does that match what you want?</p>

mscipio: <p>Thanks a lot for the hint!<br>
This made the trick:</p>
<pre><code class="lang-auto">out = torch.squeeze(torch.nn.functional.conv1d(out.view(1, Nv, -1),
                                        torch.flip(Cp,dims=(1,)).view(Nv,1, -1), 
                                        padding=(Nt - 1), groups= Nv))
</code></pre>
<p>Now I am haveing problems with the fact the during curve fitting 2 out of 4 parameters are not updated, but I guess that’s another separate issue … -.-"</p>