Is there a way to pass an padded input into a nn.Linear layer

tommywolfheart: <p>Hi.</p>
<p>I’m implementing a transformer for time series classification. For the embedding input into the transformer, I am passing the sequence into a linear layer as done in <a href="https://arxiv.org/abs/2001.08317" rel="noopener nofollow ugc">Deep Transformer Models for Time Series Forecasting: The Influenza Prevalence Case</a> and shown below:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/4/d48d30edadc672115a30ed772435ac3bb99b8c17.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/d48d30edadc672115a30ed772435ac3bb99b8c17" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/d/4/d48d30edadc672115a30ed772435ac3bb99b8c17.jpeg" alt="image" data-base62-sha1="ukjIUjSVcdhqPMum3L2jKac5fan" width="690" height="386" data-dominant-color="BBBBBB"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">720×403 32.2 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>However, for variable sequence lengths, I have to pad the input sequence to a fixed size before passing it into the input layer.</p>
<p>Is there any way to make the linear layer ignore the paddings I’ve added i.e. a mask of some sort?</p>

vdw: <p>Yes, the linear layer expects a fixed size input, so you need to pad shorter sequences.</p>
<p>The “ignoring” needs to be done as part of the self attention layer. You obviously don’t want any input token to attend to a padding token. This is done via masking, here using a padding mask. All off-the-shell transformer models support this. I have a <a href="https://github.com/chrisvdweth/nus-cs4246x/blob/master/3-neural-nlp/Section%204.2%20-%20Transformer%20Machine%20Translation.ipynb" rel="noopener nofollow ugc">Jupyter notebook</a> that tries to explain it a bit. Maybe it’s useful.</p>

tommywolfheart: <p>Thanks Chris. Yes the notebook is incredibly useful!</p>
<p>May I please clarify, it seems in this case the masking applies to the embedding i.e. the embedding would be the one that is padded and then the masking corresponds to the padded embedding.</p>
<p>In my case I would like to pad the input sequence then generate the embedding via the linear layer. So the masking needs to correspond to the padding of the input sequence which my model is not happy with because it want the masking shape to correspond to that of the embedding.</p>
<p>Thank you.</p>

vdw: <p>Not sure if I understood your question correctly.</p>
<p>What is padded is the input. For NLP tasks, the input are typically sentences in term of sequence of words, where each word is represented by a unique identifier (i.e., some integer) referring to some vocabulary. Padding means to add a special word/token to each sentence, often represented by reserving id 0 to represent this padding token (but can be anything as long as it’s consistent). So in the end you might have a padding sequence looking like [32, 6, 88, 542, 56, 34, 511, 0, 0, 0].</p>
<p>This input is pushed through the embedding layer to map each word (index) it it’s corresponding embedding vector. This means there is this one embedding vector that represents the passing token. Note that an embedding layer is just a linear layer with some conveniences.</p>
<p>The masking is typically done in two locations:</p>
<ul>
<li>
<p>Attention layer: In the simplest case, the attention between to tokens is the dot product between the corresponding embedding vectors, which is just a value and independent from any embedding size. The masking ensures the the dot product between a normal token and a padding token is ignored.</p>
</li>
<li>
<p>Output layer: Intuitively, the loss should not be calculated based on padding tokens. So again, you want to ignore those.</p>
</li>
</ul>
<p>In short, the padding mask only depends on the input, i.e., which parts of the sequences are padded using this special padding token index. The embedding layer is independent from that.</p>

tommywolfheart: <p>I see. So I’ve definitely been doing the wrong thing then. I padded  the sequence then passed the entire sequence into the linear layer i.e.</p>
<pre><code class="lang-auto">def __init__(self, padded_sequence_length, ...):
    self.fc = nn.Linear(padded_sequence_length, 512)
    ...

def forward(self, batch):
    # x is a batch of sequences e.g. x = [[32, 6, 88, 542, 56, 34, 511, 0, 0, 0], ...]    
    x, y = batch
    
    input_embedding = self.fc(x)
    pos_encoder_output = self.pos_encoder(input_embedding)
    transformer_output = self.transformer_encoder(pos_encoder_output)
    ...
</code></pre>
<p>I got this from <a href="https://towardsdatascience.com/how-to-make-a-pytorch-transformer-for-time-series-forecasting-69e073d4061e" rel="noopener nofollow ugc">here</a>. However, I’m realising that I should probably research more into the nn.Embedding layer.</p>
<p>Should I rather iterate over the sequence feeding each timestep into a linear layer and then append to a tensor i.e.:</p>
<pre><code class="lang-auto">def __init__(self, ...):
    self.fc = nn.Linear(1, 512)
    ...

def forward(self, batch):
    # x is a batch of sequences e.g. x = [[32, 6, 88, 542, 56, 34, 511, 0, 0, 0], ...]    
    x, y = batch
    all_input_embeddings = []
    for example in x:
        single_example_input_embeddings = [self.fc(timestep) for timestep in x]
        all_input_embeddings.append(single_example_input_embeddings)

    all_input_embeddings = torch.cat(all_input_embeddings)
    ...
</code></pre>
<p>Something like that?</p>
<p>Looking over the notebook more carefully now.</p>

vdw: <p>Both code snippets seem to perform the same step, and the first one is certainly the preferred one over the loop in the second snippet. Your batch <code>x</code> has shape of <code>(batch_size, padded_sequence_length)</code> and after pushing it through <code>self.fc</code> the shape will be <code>(batch_size, padded_sequence_length, 512)</code>. This looks alright.</p>
<p>However, at some point, you need to compute the padding matrix for your input (cf. method <code>create_mask()</code> in the notebook) and pass it to the encoder.</p>

tommywolfheart: <p>Awesome, thanks.</p>
<p>My apologies for taking too much of your time <img src="https://discuss.pytorch.org/images/emoji/apple/sweat_smile.png?v=12" title=":sweat_smile:" class="emoji" alt=":sweat_smile:" loading="lazy" width="20" height="20"> . I think this is where I’m getting confused about the linear layer. In the first snippet, wouldn’t the shape become <code>(batch_size, 512)</code> i.e.:</p>
<pre><code class="lang-auto">batch_size, seq_length, linear_out_length = 2, 4, 10
input_ = torch.rand(batch_size, seq_length)
fc = torch.nn.Linear(seq_length, linear_out_length)
fc(input_).shape
&gt;&gt; torch.Size([2, 10]) # batch_size, linear_out_length
</code></pre>
<p>Thus I can’t if I pad my input to a <code>seq_length = 6</code>, I will always get an output of <code>2, 10</code> therefore, I don’t quite know what the mask should be.</p>
<p>Looking at the nn.Embedding, you’re right that it must generate an embedding for each timestep then the paddings will have their embeddings in the tensor which I can then mask.</p>