Produce output of given size

akhilSoni: <p>Here inputs are sequence of indices with shape (35, 8, 1) and targets of size (35).<br>
input_size = 1<br>
output_size = 28<br>
hidden_size = 64<br>
rnn = CharRNN(input_size, hidden_size, output_size, num_layers=5)</p>
<p>class CharRNN(nn.Module):<br>
def <strong>init</strong>(self, input_size, hidden_size, output_size, num_layers=1):<br>
super(CharRNN, self).<strong>init</strong>()<br>
self.hidden_size = hidden_size<br>
self.rnn = nn.RNN(input_size, hidden_size, num_layers, batch_first=True)<br>
self.fc = nn.Linear(hidden_size, output_size)</p>
<p>def forward(self, x, h_state):<br>
x, h_state = self.rnn(x, h_state)<br>
print(x.size())<br>
out = self.fc(x)<br>
return out, h_state<br>
Here I am getting output of size (35, 8, 28) but I want it to be of size (35, 28) as I am trying to generate 28 probabilities for each of 35 sequences and getting the index having maximum value from those 28 probabilities for each sequence. Can anyone please help me?</p>

KFrank: <p>Hi Akhil!</p>
<aside class="quote no-group" data-username="akhilSoni" data-post="1" data-topic="201667" data-full="true">
<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/b3f665/48.png" class="avatar"> akhilSoni:</div>
<blockquote>
<p>inputs are sequence of indices with shape (35, 8, 1)</p>
</blockquote>
</aside>
<p>As I understand it, you have a batch size of 35, a sequence length of 8, and<br>
an <code>input_size</code> (the number of “features” that each “item” in a sequence has)<br>
of 1.</p>
<aside class="quote no-group">
<blockquote>
<p>def forward(self, x, h_state):<br>
x, h_state = self.rnn(x, h_state)</p>
</blockquote>
</aside>
<p>The output <code>x</code> of <code>self.rnn()</code> still has the sequence-length dimension of <code>8</code> (as<br>
well as the batch dimension of <code>35</code>).</p>
<aside class="quote no-group">
<blockquote>
<pre><code>out = self.fc(x)
</code></pre>
</blockquote>
</aside>
<p><code>self.fc</code> is a <code>Linear</code>.  <code>Linear</code> is applied just to the final dimension of its input,<br>
with any preceding dimensions being passed through unchanged.  So the output<br>
of <code>self.fc</code> also has the sequence-length dimension (as well as the batch<br>
dimension).</p>
<p>This is why your final output has the unwanted sequence-length dimension of 8.</p>
<p>How best to fix this depends on the logic of your use case.  But two possibilities<br>
could be:</p>
<p>Pass just the last “item” in the sequence output by <code>self.rnn</code> to <code>self.fc</code>:</p>
<pre data-code-wrap="python"><code class="lang-python">out = self.fc (x[:, -1, :])
</code></pre>
<p>Or <code>flatten()</code> the sequence-length and <code>hidden_size</code> dimensions of the output<br>
of <code>self.fc</code> together and pass that to a <code>Linear</code> with a larger <code>in_features</code>:</p>
<pre data-code-wrap="python"><code class="lang-python">self.fc = nn.Linear (8 * hidden_size, output_size)
...
out = self.fc (x.flatten (1, 2))
</code></pre>
<p>Both of these will give you <code>out</code> with shape <code>[batch_size, output_size]</code>, which<br>
equals <code>[35, 28]</code>.</p>
<p>Best.</p>
<p>K. Frank</p>

akhilSoni: <p>Hey KFrank!</p>
<p>First of all thank you for such a well explained answer. Yes, I have also made the same changes and it is working now without any error.<br>
Also you have explained me why self.fc has all the preceding dimensions.<br>
I come to know another method for doing the same which is using ‘flatten’.<br>
Thanks</p>
<p>Best.<br>
Akhil</p>