I’m having an issue: cannot unpack non-iterable float object

Rexedoziem: <pre><code class="lang-auto">train_loss = 0
for bi, d in tqdm(enumerate(data_loader)):
    ids = d['ids']
    mask = d['mask']
    token_type_ids = d['token_type_ids']
    target = d['target']
# putting them into the device

    ids = ids.to(device, dtype=torch.long)
    mask = mask.to(device, dtype=torch.long)
    token_type_ids = token_type_ids.to(device, dtype=torch.long)
    target = target.to(device, dtype=torch.long)
        
    optimizer.zero_grad()
    outputs = model(
        ids = ids,
        mask = mask,
        token_type_ids = token_type_ids
    )
    
    loss = loss_fn(outputs, target)
    loss.backward()
    optimizer.step()
    scheduler.step()
    train_loss += loss.item()
train_loss = train_loss / len(data_loader)

print(f'Train loss: {train_loss:.3f}')
</code></pre>
<p>def eval_fx(data_loader, model, device):<br>
model.eval()<br>
valid_loss = 0</p>
<pre><code class="lang-auto">#final_target = []
#final_outputs = []
with torch.no_grad():
    for bi, d in tqdm(enumerate(data_loader)):
        ids = d['ids']
        mask = d['mask']
        token_type_ids = d['token_type_ids']
        target = d['target']
    # putting them into the device

        ids = ids.to(device, dtype=torch.long)
        mask = mask.to(device, dtype=torch.long)
        token_type_ids = token_type_ids.to(device, dtype=torch.long)
        target = target.to(device, dtype=torch.long)
            
        outputs = model(
            ids = ids,
            mask = mask,
            token_type_ids = token_type_ids
        )
        loss = loss_fn(outputs, target)

        valid_loss += loss.item()
    valid_loss = valid_loss / len(data_loader)

print(f'valid loss: {valid_loss:.3f}')
</code></pre>
<p>def accuracy_metrics(self, outputs, target):<br>
outputs = torch.argmax(outputs).cpu().detach().numpy() # Due to the linear layer in our feedforward function<br>
target = target.cpu().detach().numpy()<br>
return {‘Accuracy’: metrics.accuracy_score(outputs, target)}</p>
<p>from tqdm import tqdm<br>
best_accuracy = 0<br>
for epoch in range(config.epochs):<br>
train_fx(train_data_loader, model, optimizer,scheduler, device)<br>
outputs, target = eval_fx(valid_data_loader, model, device)<br>
accuracy = accuracy_metrics(outputs, target)<br>
print(f’Accuracy score ----- {accuracy}')<br>
if accuracy &gt; best_accuracy:<br>
torch.save(model.state_dict(), config.model_path)<br>
best_accuracy = accuracy_metrics</p>
<p>I’ve checked the shape of my outputs and target for both training and validation loops and it gave me this…….<br>
Training:<br>
Outputs - torch.size([4, 3])<br>
Target - torch.size([4])</p>
<p>Validation:<br>
Outputs - torch.size([1, 3])<br>
Target - torch.size([1])</p>
<p>So I don’t know why it’s showing me errors in my validation loop.</p>

InnovArul: <p>You may have to mention the specific line number in which there is an error, so that, it will be easy to get help here.</p>
<p>On a quick look, <code>eval_fx</code> does not seem to return any values. But the below line seem to expect some return values.</p>
<aside class="quote no-group" data-username="Rexedoziem" data-post="1" data-topic="160061">
<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/rexedoziem/48/52029_2.png" class="avatar"> Rexedoziem:</div>
<blockquote>
<p>outputs, target = eval_fx(valid_data_loader, model, device)</p>
</blockquote>
</aside>
<p>P.S. you can use ``` to format the code neatly.</p>

Rexedoziem: <p>outputs, target = eval_fx(valid_data_loader, model, device)<br>
This is where the problem lies</p>
<p>Even when I returned valid_loss in the code itself, it said cannot unpack float object.</p>

InnovArul: <aside class="quote no-group" data-username="Rexedoziem" data-post="3" data-topic="160061">
<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/rexedoziem/48/52029_2.png" class="avatar"> Rexedoziem:</div>
<blockquote>
<p>outputs, target = eval_fx(valid_data_loader, model, device)</p>
</blockquote>
</aside>
<p>I am not sure if I understand your question correctly.<br>
From your code, I understand that <code>eval_fx</code> is supposed to return two values (<code>outputs, target</code>).</p>

Rexedoziem: <p>Yes, but it’s getting just a single float value.</p>

InnovArul: <p>Then maybe you could change the code to receive a “single float value”?</p>
<pre><code class="lang-auto">loss = eval_fx(valid_data_loader, model, device)
</code></pre>
<p>I think it’s a general python related error and not related to Pytorch.</p>

Rexedoziem: <p>I know the problem is from my code and it’s coming from the eval function. But I don’t know how to tweak it.</p>

ptrblck: <p>Double post from <a href="https://discuss.pytorch.org/t/the-error-is-indexerror-target-2-is-out-of-bounds-i-need-guidance-please/159629/7">here</a> with follow up.</p>