Given input size of (5 x 2) how to get feedforward network to output size (1 x 20)?

Mole_Turner: <p>I’m trying to create a model that takes a matrix of 5 x 2 (5 samples of 2 channel audio) and outputs something that is 1 x 20. At present this isn’t happening and it’s outputting 5 x 20, and I think/know this is down to how matrix multiplication works - but I was wondering how do I get it outputting my desired output shape. Below is some pseudo-code for my model.</p>
<pre><code class="lang-auto">class FFN(nn.Module):
   def __init__(self):
       super(FFN, self).__init__()
       self.hidden1 = nn.Linear(2, 128)
       self.hidden2 = nn.Linear(128, 128)
       self.output = nn.Linear(128, 20)

   def forward(self, x):
       output = self.output(F.relu(self.hidden2(F.relu(self.hidden1(x)))))
       return output  
</code></pre>
<p>So I think the reason it’s giving me a 5 x 20 output is that the matrix multiplication is working as below.<br>
A = 5 x 2<br>
B = 2 x 128<br>
C = 128 x 20</p>
<p>AB = 5 x 128<br>
(AB)C = 5 x 20.</p>

AlphaBetaGamma96: <p>If you just want the shape to (1x20), you could just do <code>return output.mean(dim=0)</code>. It should be noted that taking the mean with make your network dependent on the number of samples!</p>
<p>But shouldn’t the network take in <code>N</code> samples and return <code>N</code> outputs? Then your loss function with sum over the number of samples and return a single value?</p>
<aside class="quote no-group" data-username="Mole_Turner" data-post="1" data-topic="119611">
<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/mole_turner/48/37337_2.png" class="avatar"> Mole_Turner:</div>
<blockquote>
<p>At present this isn’t happening and it’s outputting 5 x 20</p>
</blockquote>
</aside>
<p>The reason why your network returns 5x20 as the size is due to matrix multiplication. For example with <code>A</code> times <code>B</code> the shapes must be ‘compatible’. So, the number of number of columns in <code>A</code> must match the number of rows in <code>B</code>, (the inner two dimensions in your example, [5,2] x [2,128]), and the remaining dimensions give the output shape from the multiplication (which is a matrix of shape [5,128]).</p>

Mole_Turner: <p>Thanks for the reply.</p>
<aside class="quote no-group" data-username="AlphaBetaGamma96" data-post="2" data-topic="119611">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/3da27b/48.png" class="avatar"> AlphaBetaGamma96:</div>
<blockquote>
<p>But shouldn’t the network take in <code>N</code> samples and return <code>N</code> outputs? Then your loss function with sum over the number of samples and return a single value?</p>
</blockquote>
</aside>
<p>In this instance, I want to predict the nth sample but using samples <code>n-2:n+2</code>, so that’s why I want it to take in 5 x 2 but output 1 x 20. Taking the mean might actually be useful as it would act as a moving average filter  - and I’m not sure why I didn’t think of that to begin with.</p>

AlphaBetaGamma96: <aside class="quote no-group" data-username="Mole_Turner" data-post="3" data-topic="119611">
<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/mole_turner/48/37337_2.png" class="avatar"> Mole_Turner:</div>
<blockquote>
<p>I want to predict the nth sample but using samples <code>n-2:n+2</code>,</p>
</blockquote>
</aside>
<p>But wouldn’t that include the <code>nth</code> sample in the input, so the network would just learn to disregard the other 4 samples and return the central sample?</p>

Mole_Turner: <p>Apologies, I’m not sure I explained clearly enough what my model is intended for. I’m looking to make a transformation on the signal. So I’m not looking to predict the next value in a sequence.</p>
<p>My train of thought was that it would use the samples on either side to learn some kind of temporal relationship between the desired transformed output value at <code>n</code> and the samples on either side of its original form.</p>

AlphaBetaGamma96: <p>No need to apologise! If you’re not predicting the next sample, but refining the current samples then this might work!</p>