TypeError: 'module' object is not callable for loop

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.


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

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

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


TypeError Traceback (most recent call last)
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)
117 if self.transform is not None:
→ 118 img = self.transform(img)
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!


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.


Thank you! I solved this problem.