Unable to train RNN

Sanjeev_Ram: <pre><code class="lang-auto">class RNN_Single(nn.Module):

  def __init__(self, input_dim, embed_size, hidden_state_size, classes):
    super(RNN_Single, self).__init__()
    self.embed = nn.Embedding(input_dim, embed_size)
    self.rnn = nn.RNN(embed_size, hidden_state_size)
    self.fc = nn.Linear(hidden_state_size, classes)

  def forward(self, input_batch):

    embedding_batch = self.embed(input_batch)
    output, hidden = self.rnn(embedding_batch)
    hidden = hidden.squeeze(0)
    assert torch.equal(output[-1,:,:], hidden) # Comparing the last time step output vector to the hidden vector and this should be equal
    return self.fc(hidden)

INPUT_DIM = len(NEWS.vocab) 
EMBED_DIM = 128
HIDDEN_UNITS = 512
CLASSES = 4

model = RNN_Single(INPUT_DIM, EMBED_DIM, HIDDEN_UNITS, CLASSES)
</code></pre>
<pre><code class="lang-auto">def accuracy(preds, true):

  _, index = torch.max(preds, dim = 1)

  return (index == true).sum().float() / len(preds)

def train(model, iterator, optimizer, criterion):

  e_loss = 0

  e_acc = 0

  model.train()

  for batch in iterator:

    optimizer.zero_grad()

    preds = model(batch.title)# Call using the column name

    acc = accuracy(preds,  batch.cat)

    loss = criterion(preds.squeeze(1), batch.cat)

    acc = accuracy(preds,  batch.cat)

    loss.backward()

    optimizer.step()

    e_loss += loss.item()

    e_acc += acc.item()

  return e_loss/len(iterator), e_acc/len(iterator)

def eval(model, iterator):

  e_loss = 0

  e_acc = 0

  model.eval()

  for batch in iterator:

    preds = models(batch.title)

    loss = criterion(preds.squeeze(1), batch.cat)

    acc = accuracy(preds,  batch.cat)

    e_loss += loss.item()

    e_acc += acc.item()

  return e_loss/len(iterator), e_acc/len(iterator)
</code></pre>
<pre><code class="lang-auto">N_EPOCHS = 5

best_valid_loss = float('inf')

for epoch in range(N_EPOCHS):

    start_time = time.time()
    
    train_loss, train_acc = train(model, train_iterator, optimizer, criterion)
    valid_loss, valid_acc = evaluate(model, test_iterator, criterion)
    
    end_time = time.time()

    epoch_mins, epoch_secs = epoch_time(start_time, end_time)
    
    if valid_loss &lt; best_valid_loss:
        best_valid_loss = valid_loss
        torch.save(model.state_dict(), 'tut1-model.pt')
    
    print(f'Epoch: {epoch+1:02} / {N_EPOCHS} | Epoch Time: {epoch_mins}m {epoch_secs}s')
    print(f'\tTrain Loss: {train_loss:.3f} | Train Acc: {train_acc*100:.2f}%')
    print(f'\t Val. Loss: {valid_loss:.3f} |  Val. Acc: {valid_acc*100:.2f}%')
</code></pre>
<p><strong>The error is:</strong></p>
<p><strong>TypeError                                 Traceback (most recent call last)</strong><br>
<strong> in ()</strong><br>
**      7     start_time = time.time()**<br>
**      8 **<br>
<strong>----&gt; 9     train_loss, train_acc = train(model, train_iterator, optimizer, criterion)</strong><br>
**     10     valid_loss, valid_acc = evaluate(model, test_iterator, criterion)**<br>
**     11 **</p>
<p><strong>1 frames</strong><br>
<strong> in train(model, iterator, optimizer, criterion)</strong><br>
**     12     preds = model(batch.title)# Call using the column name**<br>
**     13     acc = accuracy(preds,  batch.cat)**<br>
<strong>—&gt; 14     loss = criterion(preds.squeeze(1), batch.cat)</strong><br>
**     15     acc = accuracy(preds,  batch.cat)**<br>
**     16     loss.backward()**</p>
<p>*<em>/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in _call_impl(self, <em>input, <strong>kwargs)</strong><br>
**    725             result = self._slow_forward(<em>input, <strong>kwargs)</strong><br>
**    726         else:</em></em><br>
<em><em>–&gt; 727             result = self.forward(<em>input, <strong>kwargs)</strong><br>
**    728         for hook in itertools.chain(</em></em><br>
**    729                 _global_forward_hooks.values(),</em></em></p>
<p><strong>TypeError: forward() takes 2 positional arguments but 3 were given</strong></p>

ptrblck: <p>What kind of <code>criterion</code> are you using? Based on the stack trace it seems you might be using a custom <code>nn.Module</code>? If so, could you check its expected arguments and make sure that both tensors are accepted?</p>

Sanjeev_Ram: <p>Criterion is CrossEntropyLoss.This is a multi class problem with 4 classes. I am inheriting the default nn.Module.</p>

ptrblck: <p>Are you using some old function definitions, where the passed <code>criterion</code> can be mapped to another argument?<br>
In your code snippets you are e.g. using <code>evaluate</code>, while the definition seems to be <code>eval()</code>.</p>

Sanjeev_Ram: <p>Sorry, <a class="mention" href="/u/ptrblck">@ptrblck</a> that was a blunder I made. I corrected and still the same error. I am new to PyTorch and DL please bear with me.</p>
<pre><code class="lang-auto">import torch.optim as optim
optimizer = optim.SGD(model.parameters(), lr = 1e-3)
criterion = nn.CrossEntropyLoss()
model = model.to(device)
criterion = model.to(device)
</code></pre>

ptrblck: <p>Something might still be overwriting the definition of <code>criterion</code>, so could you post an executable code snippet to reproduce this issue, please?</p>

Sanjeev_Ram: <p>hi <a class="mention" href="/u/ptrblck">@ptrblck</a>. You can find my code here <a href="https://colab.research.google.com/drive/1stzMYrUPDXjWwPRZvJJsKAxtQ6P1ydUr?usp=sharing" rel="noopener nofollow ugc">https://colab.research.google.com/drive/1stzMYrUPDXjWwPRZvJJsKAxtQ6P1ydUr?usp=sharing</a></p>

ptrblck: <p>Thanks for the code.<br>
You are overwriting the <code>criterion</code> after its definition:</p>
<pre><code class="lang-python">criterion = nn.CrossEntropyLoss()
model = model.to(device)
criterion = model.to(device)
</code></pre>