Too many values to unpack (expected 2)

Mahdieh: <p>Hi all,</p>
<p><a class="mention" href="/u/monai">@MONAI</a><br>
I am using MONAI Compose and Dataset to transform my image dataset and train and validate a neural network… However, I am getting the following error…</p>
<p>in train<br>
for batch_idx, (data, target) in enumerate(dataloader):<br>
ValueError: too many values to unpack (expected 2)</p>
<p>here is my code:</p>
<pre><code class="lang-auto">train_ds = Dataset(data=train_files, transform=train_transforms)
    dataloader_train = torch.utils.data.DataLoader(
        train_ds,
        batch_size=2, shuffle=True, num_workers=4, pin_memory=True
    )

val_ds = Dataset(data=val_files, transform=val_transforms)
    dataloader_val = torch.utils.data.DataLoader(
        val_ds, batch_size=1, shuffle=False, num_workers=4, pin_memory=True
    )


def train(model, dataloader, optimizer, loss_func):
    model.train()
    total_loss = 0
    for batch_idx, (data, target) in enumerate(dataloader):
        data, target = data.cuda(), target.cuda()
        optimizer.zero_grad()
        output = model(data)
        loss = loss_func(output, target)
        loss.backward()
        optimizer.step()

        total_loss += loss.item()

    return total_loss/len(dataloader)
</code></pre>
<p>I would greatly appreciate any help with this…Spent so much time…no success !<br>
Thank you,</p>

Abhilash_Srivastava: <aside class="quote no-group" data-username="Mahdieh" data-post="1" data-topic="100431">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/5fc32e/48.png" class="avatar"> Mahdieh:</div>
<blockquote>
<p><code>for batch_idx, (data, target) in enumerate(dataloader):</code></p>
</blockquote>
</aside>
<p>modify this to:</p>
<pre><code class="lang-auto">for batch_idx, sample in enumerate(dataloader):
        data, target = sample['data'].cuda(), sample['target'].cuda() # or something similar
</code></pre>

Mahdieh: <p>Thank you for the comment… I applied this…but I get the following error…</p>
<pre><code class="lang-auto">
</code></pre>
<p>in train<br>
data, target = sample[‘data’].cuda(), sample[‘target’].cuda()<br>
KeyError: ‘data’</p>
<pre><code class="lang-auto"></code></pre>

Abhilash_Srivastava: <p>Of course ‘data’ and ‘target’ are just used as example keys for your dataset.<br>
What are the actual keys to access data from <code>train_ds </code> and <code>val_ds </code>?</p>

Mahdieh: <p>replaced “data” and “target” with actual keys (image and label)…it worked!.. Thank you</p>

Fat_L_GhriBi: <p>hello guys !!<br>
someone help me !!<br>
i try to implement the lda2vec algorithm from <a href="https://github.com/sebkim/lda2vec-pytorch" rel="noopener nofollow ugc"> link</a> with my own data.<br>
when i try model preprocess.py , i’m getting this error.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/c/ec547bbba860b6c63757aff956a55f959fda0da9.png" data-download-href="https://discuss.pytorch.org/uploads/default/ec547bbba860b6c63757aff956a55f959fda0da9" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/c/ec547bbba860b6c63757aff956a55f959fda0da9_2_690x387.png" alt="image" data-base62-sha1="xIFGDDpgLCK34zWCRnqmEvAnfYZ" width="690" height="387" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/c/ec547bbba860b6c63757aff956a55f959fda0da9_2_690x387.png, https://discuss.pytorch.org/uploads/default/optimized/3X/e/c/ec547bbba860b6c63757aff956a55f959fda0da9_2_1035x580.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/e/c/ec547bbba860b6c63757aff956a55f959fda0da9.png 2x" data-dominant-color="121616"><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">1366×768 51 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><br>
how to fix it.</p>