TypeError: 'module' object is not callable for loop

Hello,
I’m a beginner of pytorch.
I’m trying to classicification of VGG using CIFAR10.
The last of course, I’m making the train loop as belows,
But I got the typeerror but I don’t what is wrong.
Please help me.

----(code)

for epoch in range(100):
for data, label in train_loader:
optim.zero_grad()
preds = model(data.to(device))

  loss = nn.CrossEntropyLoss()(grads, label.to(device))
  loss.backward()
  optim.step()

if epoch==0 or epoch%10==9:
print(f"epoch{epoch+1} loss:{loss.item()}")

---------(error)

TypeError Traceback (most recent call last)
in
7
8 for epoch in range(100):
----> 9 for data, label in train_loader:
10 optim.zero_grad()
11 preds = model(data.to(device))

4 frames
/usr/local/lib/python3.8/dist-packages/torchvision/datasets/cifar.py in getitem(self, index)
116
117 if self.transform is not None:
→ 118 img = self.transform(img)
119
120 if self.target_transform is not None:

TypeError: ‘module’ object is not callable

Could you post the dataset definition and in particular what you’ve passed as the transform object to it?

Of course!
Here’s my code for datasets!

------------(code)

import torch
import torchvision
from torchvision import datasets, models, transforms

training_data = torchvision.datasets.CIFAR10(root=‘./data’, train=True,
download=True, transform=transforms)
test_data = torchvision.datasets.CIFAR10(root=‘./data’, train=False,
download=True, transform=transforms)

train_loader = DataLoader(training_data, batch_size=32, shuffle=True)

test_loader = DataLoader(test_data, batch_size=32, shuffle=False)

You are passing the transforms module directly, which is causing the issue.
Create actual transformation objects and it should work, e.g.

training_data = torchvision.datasets.CIFAR10(root='./data', train=True,
                                             download=False, transform=transforms.ToTensor())

EDIT: Take a look at these docs for more information how transformations are applied.

2 Likes

Thank you! I solved this problem.