nn.TransformerEncoder for classification

n8henrie: <p>Hello all,</p>
<p>I’m trying to get the built-in pytorch <code>TransformerEncoder</code> to do a classification task; my eventual goal is to replicate the <code>ToBERT</code> model from <a href="https://arxiv.org/pdf/1910.10781v1.pdf" rel="nofollow noopener">this paper</a> (<a href="https://paperswithcode.com/paper/docbert-bert-for-document-classification" rel="nofollow noopener">paperswithcode</a> is empty). Unfortunately, my model doesn’t seem to learn anything.</p>
<pre><code class="lang-python">import torch.nn as nn

class Net(nn.Module):
    def __init__(
        self,
        embeddings,
        nhead=8,
        nhid=200,
        num_layers=2,
        dropout=0.1,
        classifier_dropout=0.1,
        max_len=256,
    ):

        super().__init__()

        self.d_model = embeddings.size(1)
        assert (
            self.d_model % nhead == 0
        ), "nheads must divide evenly into d_model"

        self.emb = nn.Embedding.from_pretrained(embeddings, freeze=False)
        self.pos_encoder = PositionalEncoding(
            self.d_model, dropout=dropout, vocab_size=embeddings.size(0)
        )

        encoder_layers = nn.TransformerEncoderLayer(
            d_model=self.d_model, nhead=nhead, dim_feedforward=nhid, dropout=dropout
        )
        self.transformer_encoder = nn.TransformerEncoder(
            encoder_layers, num_layers=num_layers
        )
        self.classifier = nn.Sequential(
            # Other layers to go here if needed once things seem to be working
            nn.Linear(self.d_model, 2),
        )

    def forward(self, x):
        x = self.emb(x) * math.sqrt(self.d_model)

        x = self.pos_encoder(x)
        x = self.transformer_encoder(x)  # self.src_mask)
        x = x.mean(dim=1)
        return self.classifier(x)
</code></pre>
<pre><code class="lang-python">import torch.nn as nn
import math

class PositionalEncoding(nn.Module):

    def __init__(self, d_model, vocab_size=5000, dropout=0.1, batch_size=100):
        super().__init__()
        self.dropout = nn.Dropout(p=dropout)

        pe = torch.zeros(vocab_size, d_model)
        position = torch.arange(0, vocab_size, dtype=torch.float).unsqueeze(1)
        div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model))
        pe[:, 0::2] = torch.sin(position * div_term)
        pe[:, 1::2] = torch.cos(position * div_term)
        pe = pe.unsqueeze(0)
        self.register_buffer('pe', pe)

    def forward(self, x):
        x = x + self.pe[:, :x.size(1), :]
        return self.dropout(x)
</code></pre>
<p>The <code>PositionalEncoding</code> layer is taken almost directly from the pytorch language modeling example, with the exception of changing dimensions to match my preference for <code>batch_first=True</code>.</p>
<p>There are <a href="https://discuss.pytorch.org/t/transformer-for-classification/81011">few</a> <a href="https://discuss.pytorch.org/t/how-to-do-sequence-classification-with-nn-transformerencoder/56719">similar</a> <a href="https://discuss.pytorch.org/t/transformer-encoder-model-for-binary-classification/73413">posts</a>, all without definite answers.</p>
<p>I found a couple of examples of transformers for classification:</p>
<ul>
<li><a href="https://github.com/jensjepsen/imdb-transformer" rel="nofollow noopener">https://github.com/jensjepsen/imdb-transformer</a></li>
<li><a href="https://github.com/pbloem/former" rel="nofollow noopener">https://github.com/pbloem/former</a></li>
</ul>
<p>Both of these seem to work with good accuracy, so I’m sure it’s possible, but both also seem to build the transformer “from scratch.” I’d like to figure out why I can’t get it to work with the pytorch <code>TransformerEncoder</code>.</p>
<p>When I run, my loss even on my <em>training</em> set doesn’t go anywhere, so it’s clearly just not learning. I’ve tried going through the <code>PositionalEncoding</code> layer a few times, since that’s where much of the complexity lies and even tried replacing it with the positional encoding strategies used in the libraries above – no difference.</p>
<p>Does anyone see something I’m doing obviously wrong? Am I mistaken that I should be able to use a <code>TransformerEncoder</code> for classification in this way?</p>
<p>Many thanks in advance!</p>

n8henrie: <p>I found <a href="https://buomsoo-kim.github.io/attention/2020/04/22/Attention-mechanism-20.md/" rel="nofollow noopener">another example of someone trying to use <code>nn.TransformerEncoder</code> for sequences classification</a> – unfortunately their model doesn’t seem to be learning anything either, accuracy on IMDB is 53% on the <em>training</em> set. I contacted the author who thinks it may be attributable to suboptimal hyperparameters, I but I’m not so sure. I’ve tried with similar hyperparameters to what the <code>former</code> example above uses with no improvement.</p>
<p>Speaking of which, I was reading through <a class="mention" href="/u/pbloem">@pbloem</a>’s <a href="http://www.peterbloem.nl/blog/transformers" rel="nofollow noopener">excellent article on transformers</a> and realized that he was the author of <code>former</code>, which I’ve been using as an example for a few weeks, and also realized that he seems to be registered on this forum. If you happen to see this post and can spare a minute, and if you can immediately see an obvious reason why <code>nn.TransformerEncoder</code> is failing at classification where your <code>former</code> library does great, I’d be truly grateful if you could point me in the right direction.</p>
<p>EDIT: Also tried asking at <a href="https://www.reddit.com/r/learnmachinelearning/comments/gvkt73/pytorch_nntransformerencoder_for_sequence/" rel="nofollow noopener">r/learnmachinelearning</a>, figured I might as well leave a link in case that gets a response that could help someone.</p>

pbloem: <p>Hi n8henrie,</p>
<p>Thanks for the kind words. I had a look at your code, and I don’t see any obvious problems with it. The only difference with my version is that I use positional embeddings rather than encodings, but I see you already tried using position embeddings.</p>
<p>If I were in your situation, I’d probably try the following:</p>
<ul>
<li>Check that the embedding parameters are getting gradients. You can do this by setting <code>w.retain_grad()</code> on the weight tensor, and then printing the values just after the backward pass.</li>
<li>Check the loss. Your model output is linear, so the loss should apply a softmax (i.e. use cross entropy loss).</li>
<li>Try it with 0 transformer layers (i.e. just train word embeddings). IMDb is simple enough that that should put you well over chance. You can compare to <code>former</code> with 0 layers to see what performance you can expect.</li>
<li>Disable the position encoding. The model should still be able to get some performance, without any position information. If the performance goes up with the positions disabled, you know that the problem is somewhere in the position encodings.</li>
</ul>
<p>You may have tried all of this already, but this is where I’d start.</p>
<p>good luck,<br>
Peter</p>

n8henrie: <p>Thanks so much for your kind words and suggestions.</p>
<p>I’ve made a lot of progress and my model is now learning. I’m not 100% clear what made the difference, but your recommendations were very helpful in sorting things out.</p>
<blockquote>
<p>Check that the embedding parameters are getting gradients</p>
</blockquote>
<p>They were indeed getting gradients.</p>
<blockquote>
<p>Check the loss. Your model output is linear, so the loss should apply a softmax (i.e. use cross entropy loss).</p>
</blockquote>
<p>I’m using <code>nn.CrossEntropyLoss</code>, which includes a softmax.</p>
<blockquote>
<p>Disable the position encoding</p>
</blockquote>
<p>What I did instead was to swap my position encoding implementation into <code>former</code>, and it didn’t hurt its learning.</p>
<blockquote>
<p>Try it with 0 transformer layers (i.e. just train word embeddings).</p>
</blockquote>
<p>This was a great recommendation and was really informative. Both <code>former</code> and my model learned well with 0 transformer layers. Then I found that my model would learn okay with 1 or 2 layers, but at 3 was having trouble. Eventually I found that decreasing my learning rate from 0.001 to 0.0001 and extending my training time from 10 to 50 epochs showed that it was eventually learning. With some tinkering with the gradient clipping, the LambdaLR scheduler, and the initial learning rate, I now have its accuracy on the <strong>training</strong> set eventually reaching high 90s. I know I had taken learning rate down to 1e-5 previously to ensure that wasn’t the issue; my guess is that there were multiple problems and each one masked seeing the improvement when I tinkered with the other.</p>
<p>Now that I can see that my model is capable of learning I consider this issue solved. Thank you so much!</p>
<p>I will update soon to post my actual model in case it’s helpful for future readers.</p>

jipson7: <p>Hi, I’d like to add that I’ve also tried the built in PyTorch TransformerEncoder class for a classification task, it seems to learn nothing, whereas <a href="https://github.com/lucidrains/x-transformers" rel="noopener nofollow ugc">this one</a> by lucidrains works fine. Is there a bug in the native implementation? Or perhaps some missing features that the others may have?</p>

Luca_Guarro: <p>Hi <a class="mention" href="/u/n8henrie">@n8henrie</a> thanks for having tackled this issue already. I was wondering if your code above considers only the case in which padded tokens are not needed? If so in order to extend it to work for padded tokens, I would need to change the mean operation right such that it ignores the encoder embeddings of the padded tokens, right?</p>

n8henrie: <aside class="quote no-group" data-username="n8henrie" data-post="4" data-topic="83021">
<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/n8henrie/48/6212_2.png" class="avatar"> n8henrie:</div>
<blockquote>
<p>I will update soon to post my actual model in case it’s helpful for future readers.</p>
</blockquote>
</aside>
<p>By “soon” I apparently meant a year. Sorry! I wrote up my code and results here, hope it’s helpful:</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://n8henrie.com/2021/08/writing-a-transformer-classifier-in-pytorch/">
  <header class="source">

      <a href="https://n8henrie.com/2021/08/writing-a-transformer-classifier-in-pytorch/" target="_blank" rel="noopener nofollow ugc" title="11:47AM - 24 August 2021">n8henrie.com – 24 Aug 21</a>
  </header>

  <article class="onebox-body">
    

<h3><a href="https://n8henrie.com/2021/08/writing-a-transformer-classifier-in-pytorch/" target="_blank" rel="noopener nofollow ugc">Writing a Transformer Classifier in PyTorch</a></h3>

  <p>Technology, medicine, science, superstition… and having fun. Brought to you by Nathan Henrie.</p>


  </article>

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

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


n8henrie: <aside class="quote no-group" data-username="Luca_Guarro" data-post="6" data-topic="83021">
<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/luca_guarro/48/39471_2.png" class="avatar"> Luca_Guarro:</div>
<blockquote>
<p>I was wondering if your code above considers only the case in which padded tokens are not needed?</p>
</blockquote>
</aside>
<p>Good question, I’m not really sure.</p>
<p>It looks like the torchtext data <em>does</em> contain padding (see below, scroll to the end of the output), and it’s possible that masking (perhaps with <a href="https://pytorch.org/docs/stable/generated/torch.nn.TransformerEncoder.html" rel="noopener nofollow ugc"><code>src_key_padding_mask</code></a>?) would improve performance? Perhaps my example code is just learning to essentially ignore <code>&lt;pad&gt;</code> tokens?</p>
<pre data-code-wrap="python"><code class="lang-python">&gt;&gt;&gt; " ".join(TEXT.vocab.itos[tok] for tok in next(iter(train_iter)).text[0])
"without a doubt this is one of the worst films i've ever wasted money on! the plot is, erm sorry, did i say there was a plot? the scariest moment was &lt;unk&gt; nope can't think of one! the best special effect that had me hiding under the bed covers &lt;unk&gt; nope can't think of one for that either. you knew who the killer was right from the start. there was nothing scary about the whole movie, in fact the only two vaguely interesting bits were when you saw the kid sister, &lt;unk&gt; in the shower and when you saw nurse &lt;unk&gt; take her top off. this film should only be watched to get an idea of how not to make a horror movie!!! &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt; &lt;pad&gt;"
</code></pre>

Abraham_Starosta: <p><a class="mention" href="/u/n8henrie">@n8henrie</a> thanks for sharing your code. i was literally running into the same issue… but glad i got a similar accuracy as you on imdb</p>

Esxyko: <p>Thanks for sharing the code. I suppose the issue was missing a zero_grad() statement for the optimizer?</p>