# TypeError: 'DataLoader' object is not callable

**URL:** <https://discuss.pytorch.org/t/typeerror-dataloader-object-is-not-callable/74979>\
**Category:** Uncategorized\
**Created:** [April 1, 2020, 7:29am UTC](https://discuss.pytorch.org/t/typeerror-dataloader-object-is-not-callable/74979 "2020-04-01T07:29:11Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sreekar](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sreekar/32/22221_2.png) [@Sreekar](https://discuss.pytorch.org/u/Sreekar)\
**Post date:** [April 1, 2020, 7:29am UTC](https://discuss.pytorch.org/t/typeerror-dataloader-object-is-not-callable/74979/1 "2020-04-01T07:29:11Z")

</div>

train\_loader = DataLoader(dataset=dataset, batch\_size=40, shuffle=False)  
" This is my train loader variable."  
for epoch in range(num\_epochs):  
for i in enumerate(train\_loader):  
t = train\_loader(-1,1).to(device)  
outputs = model(t)  
loss = criterion(outputs)  
optimizer.zero\_grad()  
loss.backward()  
optimizer.step()

```
    if (i + 1) % 100 == 0:
        print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch + 1, num_epochs, i + 1, total_step, loss.item()))

```

 ![Screenshot 2020-04-01 at 12.57.59 PM](https://discuss.pytorch.org/uploads/default/original/3X/7/5/759e9d80551a842d2cd10d427140c292e50afdc8.png)

I’m getting this error whatever I change. Can anyone help me with this?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![damicoedoardo](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/damicoedoardo/32/21571_2.png) [@damicoedoardo](https://discuss.pytorch.org/u/damicoedoardo)\
**Post date:** [April 1, 2020, 7:41am UTC](https://discuss.pytorch.org/t/typeerror-dataloader-object-is-not-callable/74979/2 "2020-04-01T07:41:59Z")

</div>

I don’t get what you want to do doing:

```auto
t = train_loader(-1,1)

```

you are trying to call the `DataLoader` class which is not callable as suggested by the error.  
What do you want achieve with (-1,1)?

---

<div class="post-metadata">

**Author:** ![Sreekar](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sreekar/32/22221_2.png) [@Sreekar](https://discuss.pytorch.org/u/Sreekar)\
**Post date:** [April 1, 2020, 7:58am UTC](https://discuss.pytorch.org/t/typeerror-dataloader-object-is-not-callable/74979/3 "2020-04-01T07:58:06Z")

</div>

i want to load my data into the model

---

<div class="post-metadata">

**Author:** ![Sreekar](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sreekar/32/22221_2.png) [@Sreekar](https://discuss.pytorch.org/u/Sreekar)\
**Post date:** [April 1, 2020, 8:03am UTC](https://discuss.pytorch.org/t/typeerror-dataloader-object-is-not-callable/74979/4 "2020-04-01T08:03:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![damicoedoardo](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/damicoedoardo/32/21571_2.png) [@damicoedoardo](https://discuss.pytorch.org/u/damicoedoardo)\
**Post date:** [April 1, 2020, 8:55am UTC](https://discuss.pytorch.org/t/typeerror-dataloader-object-is-not-callable/74979/5 "2020-04-01T08:55:53Z")

</div>

You have to iterate on the data loader,  
Check the tutorial: [data loader tutorial](https://pytorch.org/tutorials/beginner/data_loading_tutorial.html#iterating-through-the-dataset)

Basically:  
`for i_batch, sample_batched in enumerate(dataloader): `  
Hope I have been useful 😁

---

<div class="post-metadata">

**Author:** ![braindotai](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/braindotai/32/61373_2.png) [@braindotai](https://discuss.pytorch.org/u/braindotai)\
**Post date:** [April 1, 2020, 9:07am UTC](https://discuss.pytorch.org/t/typeerror-dataloader-object-is-not-callable/74979/6 "2020-04-01T09:07:45Z")

</div>

Few things I found-

- As @damicoedoardo mentioned you should iterate for dataloader as-

```python
for i, batch in enumerate(train_loader):

```

- You should pass only the features of the batch, not the whole batch. In a normal supervised scenario, you will have `len(batch)` = 2, which means `features = batch[0]` and `labels = batch[1]`. And you will calculate predictions as `outputs = model(features)`

> [@Sreekar](#):
>
> loss = criterion(outputs)

- I don’t get how you are minimizing the loss functions here without passing labels? Something like `loss = criterion(outputs, labels)`. Can you tell what criterion you are using?

- And by calling `.(-1, 1)` I think you are trying to reshape the labels or maybe features to (-1, 1). If your cirterion is `torch.nn.CrossEntropyLoss()` then shape of the labels should be (-1), that’s it, no reshaping is required for labels there.  
Also features shape should be (-1, n) where n is number of unique features in your single training example.  
If you want to reshape either you can do as-

- So your final traning loop should like -

```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()

```

---

<div class="post-metadata">

**Author:** ![Sreekar](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sreekar/32/22221_2.png) [@Sreekar](https://discuss.pytorch.org/u/Sreekar)\
**Post date:** [April 1, 2020, 9:37am UTC](https://discuss.pytorch.org/t/typeerror-dataloader-object-is-not-callable/74979/7 "2020-04-01T09:37:19Z")

</div>

I tried your solution and I’m getting this error

 ![Screenshot 2020-04-01 at 3.06.11 PM](https://discuss.pytorch.org/uploads/default/original/3X/4/e/4e8efea2dc390fa410092521330ee3162019af63.png)

---

<div class="post-metadata">

**Author:** ![Sreekar](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sreekar/32/22221_2.png) [@Sreekar](https://discuss.pytorch.org/u/Sreekar)\
**Post date:** [April 1, 2020, 9:43am UTC](https://discuss.pytorch.org/t/typeerror-dataloader-object-is-not-callable/74979/8 "2020-04-01T09:43:25Z")

</div>

I tried it previously and it did not work.

---

<div class="post-metadata">

**Author:** ![braindotai](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/braindotai/32/61373_2.png) [@braindotai](https://discuss.pytorch.org/u/braindotai)\
**Post date:** [April 1, 2020, 10:09am UTC](https://discuss.pytorch.org/t/typeerror-dataloader-object-is-not-callable/74979/9 "2020-04-01T10:09:10Z")

</div>

Show me your complete model definition.  
And print shapes for features and labels.  
Also what is your criterion???

---

<div class="post-metadata">

**Author:** ![Sreekar](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sreekar/32/22221_2.png) [@Sreekar](https://discuss.pytorch.org/u/Sreekar)\
**Post date:** [April 1, 2020, 11:59am UTC](https://discuss.pytorch.org/t/typeerror-dataloader-object-is-not-callable/74979/10 "2020-04-01T11:59:51Z")

</div>

![Screenshot 2020-04-01 at 5.28.52 PM](https://discuss.pytorch.org/uploads/default/original/3X/a/2/a20878a5c60bd67831b71f58686e7b2ba85b0854.png) !

---

<div class="post-metadata">

**Author:** ![Sreekar](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sreekar/32/22221_2.png) [@Sreekar](https://discuss.pytorch.org/u/Sreekar)\
**Post date:** [April 1, 2020, 12:00pm UTC](https://discuss.pytorch.org/t/typeerror-dataloader-object-is-not-callable/74979/11 "2020-04-01T12:00:33Z")

</div>

![Screenshot 2020-04-01 at 5.30.29 PM](https://discuss.pytorch.org/uploads/default/original/3X/7/4/748a2bc64142124df444885bf476ec9fb4b8c734.png)
