I don't understand the reason for the error:

username: Development_IT_in_Me
cooked: <p>When the model is trained after evaluating the validation loss, this error occurs:  TypeError: cannot unpack non-iterable NoneType object</p>
<p>Why does this error occur? What do I need to fix? Thank you!</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/a/8ac36be2cd672123e9e7301b60a62e1fd99b512b.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/8ac36be2cd672123e9e7301b60a62e1fd99b512b" title="error"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/8/a/8ac36be2cd672123e9e7301b60a62e1fd99b512b_2_618x500.jpeg" alt="error" data-base62-sha1="jNyzVe78bChCsUuV3YN6AxOhKoH" width="618" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/8/a/8ac36be2cd672123e9e7301b60a62e1fd99b512b_2_618x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/8/a/8ac36be2cd672123e9e7301b60a62e1fd99b512b_2_927x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/8/a/8ac36be2cd672123e9e7301b60a62e1fd99b512b.jpeg 2x" data-dominant-color="F5F5F5"><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">error</span><span class="informations">1022×826 98.7 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>Here is code:</p>
<pre><code class="lang-auto">def train():
    model.train()
    total_loss, total_accuracy = 0, 0
    total_preds = []
    
    for step, batch in tqdm(enumerate(train_dataloader), total = len(train_dataloader)):
        batch = [r.to(device) for r in batch]
        sent_id,mask,labels = batch
        model.zero_grad()
        preds = model(sent_id, mask)
        loss = cross_entropy(preds, labels)
        total_loss += loss.item()
        loss.backward()
        torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
        optimizer.step()
        preds = preds.detach().cpu().numpy()
        total_preds.append(preds)
        
    avg_loss = total_loss / len(train_dataloader)
    total_preds = np.concatenate(total_preds, axis = 0)
    
    return avg_loss, total_preds

def evaluate():
    model.eval()
    total_loss, total_accuracy = 0,0
    total_preds = []

    for step, batch in tqdm(enumerate(val_dataloader), total = len(val_dataloader)):
        batch = [t.to(device) for t in batch]
        sent_id, mask, labels = batch
        
        with torch.no_grad():
            preds = model(sent_id, mask)
            loss = cross_entropy(preds, labels)
            total_loss = total_loss + loss.item()
            preds = preds.detach().cpu().numpy()
            total_preds.append(preds)

    avg_loss = total_loss / len(val_dataloader)
    total_preds = np.concatenate(total_preds, axis = 0)

best_valid_loss = float('inf')

train_losses = []
valid_losses = []

for epoch in range(epochs):
    print('\n Epoch{:} / {:}'.format(epoch+1, epochs))
    
    train_loss, _ = train()
    valid_loss, _ = evaluate()
    
    if valid_loss &lt; best_valid_loss:
        best_valid_loss = valid_loss
        torch.save(model.state_dict(), 'saved_weights.pt')
    
    train_losses.append(train_loss)
    valid_losses.append(valid_loss)
    print(f'\nTraining loss: {train_loss:.3f}')
    print(f'Validation loss: {valid_loss:.3f}')

</code></pre>

username: AlphaBetaGamma96
cooked: <p>your <code>evaluate</code> function doesn’t return anything, hence the <code>None</code> error</p>

username: Development_IT_in_Me
cooked: <p>OMG,   I lost:   return avg_loss, total_preds</p>
<p>Thank you  AlphaBetaGamma96 !  It’s all right now  ::):</p>