Many to many RNN with variable output size

learner47: <p>Hi,</p>
<p>I am trying to implement a many to many RNN network. I have defined the model and forward prop like below:</p>
<pre><code class="lang-auto">class ManyToManyRNN(nn.Module):
    def __init__(self, hid_size=100, output_dim):
        super(ManyToManyRNN, self).__init__()
        self.hidden_size = hid_size
        self.output_dim = output_dim
        self.rnn = nn.LSTM(input_size=4, hidden_size=self.hidden_size, num_layers=1, batch_first=True)
        self.fc1 = nn.Linear(self.hidden_size, self.output_dim)

    def forward(self, inputs):
        outputs = []
        rnn_out, _ = self.rnn(inputs)
        output = self.fc1(rnn_out[:, -1])
        return output
</code></pre>
<p>I can set the value of <code>output_dim</code> while initializing the class and then the value gets fixed. However, in my application, the value of the output dimension keeps changing. I can’t fix the value of <code>output_dim</code> before hand.</p>
<p>My question is, how can one construct a many-to-many RNN with output dimension that is flexible? I am open to changing the architecture of the model to achieve this functionality. Any help is appreciated.</p>

marvosyntactical: <p>You need to fix the value of output_dim beforehand so you can make use of the same output weight matrix always (and so that weight updates are informative). The way I would do this is to make a guess at the largest output size you will need (max of training data, 1.5 times that, …?) and then mask the output for smaller target values. This means that you multiply the last k scalars of your output by 0, the ones which you dont need. The specifics of when to mask depend on when you know about your current output size. Simple example: If you already know you have a target tensor of a specific size beforehand, you could do:</p>
<pre><code class="lang-auto">max_size = big
for x, y in batches:
    mask_last_k = max_size-y.shape[-1]
    mask = torch.ones(max_size)
    mask[mask_last_k:] = 0
    
    predictions = model(x)
    predictions_masked = predictions * mask
    
    loss = loss_fn(predictions_masked, y)
    loss.backward()
    optimizer.step()
</code></pre>
<p>If you actually somehow generate output tensors of different sizes, you can of course also <em>pad</em> the difference up to the required length:</p>
<pre><code class="lang-auto">    pad_last_k = y.shape[-1] - x.shape[-1]
    pad = torch.zeros(max_size)
    
    predictions = model(x)
    predictions_padded = torch.cat([predictions, mask], dim=-1)
</code></pre>
<p>If you get autograd errors for creating the pad/ mask tensor yourself, create them in the scope of with torch.no_grad().<br>
Hope this helps</p>

learner47: <p>Hey <a class="mention" href="/u/marvosyntactical">@marvosyntactical</a>, thank you so much for your reply!</p>
<p>I already know the maximum and minimum size of the output. What I wonder is that, RNNs are powerful, in that, they can be trained using a value of input sequence length and they’ll perform for shorter or longer input sequence lengths. I was trying to find out if such a thing can be said for the output of an RNN or any other architecture.</p>
<p>An example being, if the architecture learns to predict three numbers in a sequence while training, is it possible for the network to predict, maybe, five numbers during inference.</p>

marvosyntactical: <p>Oh then I misunderstood. What you ask is still doable pretty much the way I described, for example you can save the outputs at each step in a list, and then create a tensor from that, and pad it to a standard length if you need to calculate your loss on the entire sequence. In many applications, this isn’t needed though, for example in language models you usually calculate the loss on each step (greedy training), so in that case you dont need to deal with variable sizes at all.</p>

learner47: <p><a class="mention" href="/u/marvosyntactical">@marvosyntactical</a> perfect, this is what I came up with before you wrote this answer. I have one question though. Suppose my input is of the size <code>(batch_size, seq_len, num_inputs)</code> and suppose I need a length <code>len</code> at the output. More clearly, the expected output size is <code>(batch_size, len)</code>. I run a loop for <code>len</code> number of times and in each iteration roll my input to the RNN (same input would produce the same output, so I had to do something to the input <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"> ). Do you recommend this way of doing it? Or is there a better way to change the inputs to get the required sequence length?</p>
<p>Thanks a lot for your time!</p>

marvosyntactical: <p>So If I understand correctly seq_len does not necesarily vary but len can vary and you know this ahead of time. I think it depends on the task: Is there some kind of termination criterion in your task? In language, for example, we predict discrete tokens, and have a special termination token, which, if it is generated, terminates the sequence, but we still use a maximum sequence length as I described above and pad the predictions that go over the limit given by your ‘len’ or the termination token.<br>
I would recommend looking at MAX_LENGTH here <a href="https://pytorch.org/tutorials/intermediate/seq2seq_translation_tutorial.html" class="inline-onebox" rel="noopener nofollow ugc">NLP From Scratch: Translation with a Sequence to Sequence Network and Attention — PyTorch Tutorials 1.7.1 documentation</a></p>