How do I chop an image into a sequence of vectors? (for input into GRU layer)

username: miturian
cooked: <p>I am a beginner, who have jumped into the deep end, and am trying to learn things that way. I have tried to find answers by using the official resources, but I feel like if you’re not trying to classify mnist, then the tutorials are less useful (and I already got my first toy network to work). So, after staring into the screen for a few hours now, I hope I can ask my (hopefully) simple questions here…</p>
<p>Thanks for reading on <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>Mission:<br>
I want to try to implement a simple version of this network (because it’s relevant for what I eventually want to do):</p>
<aside class="onebox pdf" data-onebox-src="https://arxiv.org/pdf/1809.10932.pdf">
  <header class="source">

      <a href="https://arxiv.org/pdf/1809.10932.pdf" target="_blank" rel="noopener nofollow ugc">arxiv.org</a>
  </header>

  <article class="onebox-body">
    <a href="https://arxiv.org/pdf/1809.10932.pdf" target="_blank" rel="noopener nofollow ugc"><span class="pdf-onebox-logo"></span></a>

<h3><a href="https://arxiv.org/pdf/1809.10932.pdf" target="_blank" rel="noopener nofollow ugc">1809.10932.pdf</a></h3>

  <p class="filesize">1270.21 KB</p>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>My network:<br>
My input is a series of spectrograms, so it is size: [N,1,129,29]<br>
I wish to first filter that through a conv2d layer with 20 filters:<br>
self.conv1 = nn.Conv2d(1,20,(5, 5), stride=(1, 1), padding=(2, 2))<br>
Output is [N, 20, 129,29]<br>
Then the point is to take each of the 29 ‘columns’ in my output and pass them into a bidirectional GRU-layer, which will then treat them as a sequence of 29 vectors, and iterate over them. But how do I actually do that? I assume I have to somehow split the conv2d output into a ‘minibatch’ with 29 elements, and then pass that to a GRU layer with input size 129?<br>
In the paper, they write:</p>
<blockquote>
<p>For convenience, we interpret the image X after the filterbank layers as a sequence of T feature vectors X ≡ (x1, x2, . . . , xT ) where each xt , 1 ≤ t ≤ T , is the image column at time index t. We then aim to read the sequence of feature vectors into a single feature vector using the attention-based bidirectional RNN. The forward and backward recurrent layers of the RNN iterate over individual feature vectors of the sequence in opposite directions and compute forward and backward sequences of hidden state vectors Hf = (hf1 , hf2 , . . . , hfT ) and Hb = (hb1 , hb2 , . . . , hbT ),</p>
</blockquote>

username: ptrblck
cooked: <p>I’m unsure where the 29 comes from. Do you mean 129 or 20 or are the “29 columns” created in a special way?<br>
If it’s a typo and you would like to use the 20 channels as the temporal dimension, you could initialize the RNN via <code>batch_first=True</code>, which would expect an input in the shape <code>[batch_size, seq_len, features]</code> and could flatten the conv activation via: <code>x = x.view(x.size(0), x.size(1), -1)</code>.<br>
Alternatively, you could also use the default setup in the RNN, which would then expect the input to have the shape <code>[seq_len, batch_size, features]</code> and you would have to <code>permute</code> the tensor into the right shape after flattening the feature dimension.</p>

username: miturian
cooked: <p>thanks a lot <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>the 29 comes from the last dimension in the input (which is still there after the conv2d layer). So the 29 should be the temporal dimension. I think a combination of permute and view is what I am after.</p>