How share kernel parameters between rows in conv2?

jayz: <p>Hello,</p>
<p>so, imagine we have a 2d matrix input of shape m rows x n columns to a conv2d layer. My goal is to preserve the number of rows of the matrix while running the same kernel along each row.</p>
<p>In more detail, say, we apply a conv2d layer with a kernel (1,k) to the matrix, then, the layer would in my understanding learn one kernel of size (1,k) with individual parameters per row (and per feature map), resulting in kernel k1, …, km. Instead, what i would like to do is to share the parameters of a single kernel of size (1,k) across all rows. In other words, the same kernel is run along each row and produces and output “at the end of the row”. These outputs will be further transformed and fed to the loss function, where they ultimately result in a gradient that we can backpropagate to update the k parameters of the kernel which has been jointly run over each individual row in the matrix.</p>
<p>The idea behind this approach is to (i) make the training more efficient, as more parameters are shared (ii) learn a very generalized kernel that can understand each of the rows and learns their features jointly. Importantly, dont imagine my input matrix as being a picture (for which this approach would probably not make sense). Rather imagine each row represents a series of features and all the series are highly correlated amongst each other (/across the columns). Then, the general kernel should be able to apply to all rows simulatenously.</p>
<p>The problem: I have no idea how to modify the conv2d such that it makes use of only a single (1,k) that is used in each row. Can somebody give me a hint here? <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>Thanks!</p>
<p>Best, JZ</p>

KFrank: <p>Hi Jay!</p>
<aside class="quote no-group" data-username="jayz" data-post="1" data-topic="146042" data-full="true">
<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/jayz/48/46742_2.png" class="avatar"> jayz:</div>
<blockquote>
<p>say, we apply a conv2d layer with a kernel (1,k) to the matrix, then, the layer would in my understanding learn one kernel of size (1,k) with individual parameters per row (and per feature map), resulting in kernel k1, …, km. Instead, what i would like to do is to share the parameters of a single kernel of size (1,k) across all rows.</p>
</blockquote>
</aside>
<p>Is it possible that you misunderstand how <code>Conv2d</code> works?</p>
<p>Consider:</p>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'1.10.2'
&gt;&gt;&gt; conv = torch.nn.Conv2d (1, 1, kernel_size = (1, 3))
&gt;&gt;&gt; conv.weight
Parameter containing:
tensor([[[[-0.2149,  0.2000, -0.0982]]]], requires_grad=True)
&gt;&gt;&gt; conv.bias
Parameter containing:
tensor([-0.0693], requires_grad=True)
</code></pre>
<p><code>Conv2d</code> has only one kernel, in this case of shape <code>[1, 1, 1, 3]</code>, so<br>
there is only one such kernel that can be “learned” and that one kernel<br>
is used for all the rows of the input.</p>
<p>Best.</p>
<p>K. Frank</p>

jayz: <p>Hey K,</p>
<p>it is very likely that I misunderstood, then ^^. I somehow had the thought that one kernel would be learned for each row when the kernel height is 1, which now, as I reviewed the theory doesn’t make much sense. Thanks much for clarifying it!</p>
<p>Best, JZ</p>