TypeError: 'DataLoader' object is not callable

Sreekar: <p>train_loader = DataLoader(dataset=dataset, batch_size=40, shuffle=False)<br>
" This is my train loader variable."<br>
for epoch in range(num_epochs):<br>
for i in enumerate(train_loader):<br>
t = train_loader(-1,1).to(device)<br>
outputs = model(t)<br>
loss = criterion(outputs)<br>
optimizer.zero_grad()<br>
loss.backward()<br>
optimizer.step()</p>
<pre><code>    if (i + 1) % 100 == 0:
        print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch + 1, num_epochs, i + 1, total_step, loss.item()))
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/5/759e9d80551a842d2cd10d427140c292e50afdc8.png" data-download-href="https://discuss.pytorch.org/uploads/default/759e9d80551a842d2cd10d427140c292e50afdc8" title="Screenshot 2020-04-01 at 12.57.59 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/7/5/759e9d80551a842d2cd10d427140c292e50afdc8_2_690x259.png" alt="Screenshot 2020-04-01 at 12.57.59 PM" data-base62-sha1="gMvH7Z65UuxaWpJ2wZvfpDh7RC8" width="690" height="259" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/7/5/759e9d80551a842d2cd10d427140c292e50afdc8_2_690x259.png, https://discuss.pytorch.org/uploads/default/optimized/3X/7/5/759e9d80551a842d2cd10d427140c292e50afdc8_2_1035x388.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/7/5/759e9d80551a842d2cd10d427140c292e50afdc8.png 2x" data-dominant-color="333132"><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">Screenshot 2020-04-01 at 12.57.59 PM</span><span class="informations">1214×456 39.2 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>I’m getting this error whatever I change. Can anyone help me with this?</p>
<p>Thanks in advance</p>

damicoedoardo: <p>I don’t get what you want to do doing:</p>
<pre><code class="lang-auto">t = train_loader(-1,1)
</code></pre>
<p>you are trying to call the <code>DataLoader</code> class which is not callable as suggested by the error.<br>
What do you want achieve with (-1,1)?</p>

Sreekar: <p>i want to load my data into the model</p>

Sreekar: <p>This is sample data<br>
13 0  -1<br>
13 0  -1<br>
13 0  -1<br>
16 0 -1<br>
12 0 -1<br>
I converted them to tensor and i want train the data by passing them to model<br>
and i’m unable to load the data into model.</p>

damicoedoardo: <p>You have to iterate on the data loader,<br>
Check the tutorial: <a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html#iterating-through-the-dataset" rel="nofollow noopener">data loader tutorial</a></p>
<p>Basically:<br>
<code>for i_batch, sample_batched in enumerate(dataloader): </code><br>
Hope I have been useful <img src="https://discuss.pytorch.org/images/emoji/apple/grin.png?v=9" title=":grin:" class="emoji" alt=":grin:"></p>

braindotai: <p>Few things I found-</p>
<ul>
<li>As <a class="mention" href="/u/damicoedoardo">@damicoedoardo</a> mentioned you should iterate for dataloader as-<pre data-code-wrap="python"><code class="lang-python">for i, batch in enumerate(train_loader):
</code></pre>
</li>
<li>You should pass only the features of the batch, not the whole batch. In a normal supervised scenario, you will have <code>len(batch)</code> = 2, which means <code>features = batch[0]</code> and <code>labels = batch[1]</code>. And you will calculate predictions as <code>outputs = model(features)</code></li>
</ul>
<aside class="quote no-group" data-username="Sreekar" data-post="1" data-topic="74979">
<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/sreekar/48/22221_2.png" class="avatar"> Sreekar:</div>
<blockquote>
<p>loss = criterion(outputs)</p>
</blockquote>
</aside>
<ul>
<li>
<p>I don’t get how you are minimizing the loss functions here without passing labels? Something like <code>loss = criterion(outputs, labels)</code>. Can you tell what criterion you are using?</p>
</li>
<li>
<p>And by calling <code>.(-1, 1)</code> I think you are trying to reshape the labels or maybe features to (-1, 1). If your cirterion is <code>torch.nn.CrossEntropyLoss()</code> then shape of the labels should be (-1), that’s it, no reshaping is required for labels there.<br>
Also features shape should be (-1, n) where n is number of unique features in your single training example.<br>
If you want to reshape either you can do as-</p>
<pre data-code-wrap="python"><code class="lang-python">features = features.reshape(-1, n) # only if features's shape is not this already (put the value of n here)
labels = labels.reshape(-1, 1) # only if labels's shape is not this already
</code></pre>
</li>
<li>
<p>So your final traning loop should like -</p>
</li>
</ul>
<pre data-code-wrap="python"><code class="lang-python">for epoch in range(num_epochs):
    for i, batch in enumerate(train_loader):
        features = batch[0]
        labels  = batch[1]
        features = features.reshape(-1, n) # only if features's shape is not this already, put the value of n here!!
        labels = labels.reshape(-1, 1) # only if labels's shape is not this already
        outputs = model(features)
        loss = criterion(outputs, labels)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
</code></pre>

Sreekar: <p>I tried your solution and I’m getting this error</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/e/4e8efea2dc390fa410092521330ee3162019af63.png" data-download-href="https://discuss.pytorch.org/uploads/default/4e8efea2dc390fa410092521330ee3162019af63" title="Screenshot 2020-04-01 at 3.06.11 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/4/e/4e8efea2dc390fa410092521330ee3162019af63_2_690x64.png" alt="Screenshot 2020-04-01 at 3.06.11 PM" data-base62-sha1="bcXBEIO1NwUqowuND0xt9i2D8k3" width="690" height="64" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/4/e/4e8efea2dc390fa410092521330ee3162019af63_2_690x64.png, https://discuss.pytorch.org/uploads/default/optimized/3X/4/e/4e8efea2dc390fa410092521330ee3162019af63_2_1035x96.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/4/e/4e8efea2dc390fa410092521330ee3162019af63_2_1380x128.png 2x" data-dominant-color="323238"><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">Screenshot 2020-04-01 at 3.06.11 PM</span><span class="informations">2018×190 34.8 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>

Sreekar: <p>I tried it previously and it did not work.</p>

braindotai: <p>Show me your complete model definition.<br>
And print shapes for features and labels.<br>
Also what is your criterion???</p>

Sreekar: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/2/a20878a5c60bd67831b71f58686e7b2ba85b0854.png" data-download-href="https://discuss.pytorch.org/uploads/default/a20878a5c60bd67831b71f58686e7b2ba85b0854" title="Screenshot 2020-04-01 at 5.28.52 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/2/a20878a5c60bd67831b71f58686e7b2ba85b0854_2_690x389.png" alt="Screenshot 2020-04-01 at 5.28.52 PM" data-base62-sha1="n7puZp9hE2M392gzsGIgT1tGKmE" width="690" height="389" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/2/a20878a5c60bd67831b71f58686e7b2ba85b0854_2_690x389.png, https://discuss.pytorch.org/uploads/default/optimized/3X/a/2/a20878a5c60bd67831b71f58686e7b2ba85b0854_2_1035x583.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/a/2/a20878a5c60bd67831b71f58686e7b2ba85b0854_2_1380x778.png 2x" data-dominant-color="303031"><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">Screenshot 2020-04-01 at 5.28.52 PM</span><span class="informations">1476×834 83.5 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>

Sreekar: <p><img src="https://discuss.pytorch.org/uploads/default/original/3X/7/4/748a2bc64142124df444885bf476ec9fb4b8c734.png" alt="Screenshot 2020-04-01 at 5.30.29 PM" data-base62-sha1="gCXpPgchSdK4ZyqdA2RGCZlNmxS" width="590" height="186"></p>