Data parallelism error for pretrained model


I am trying to run my model on multiple GPUs for data parallelism but receiving this error:

AttributeError: 'DataParallel' object has no attribute 'fc'

I have defined the following pretrained model :

def resnet50(num_classes, device):
    model = models.resnet50(pretrained=True)
    model = torch.nn.DataParallel(model)
    for p in model.parameters():
        p.requires_grad = False
    num_ftrs = model.fc.in_features
    model.fc = nn.Linear(num_ftrs, num_classes)

    model =
    return model

It’s unclear to me where I can add “module”


If you are trying to access the fc layer in the resnet50 wrapped by the DataParallel model, you can use model.module.fc, as DataParallel stores the provided model as self.module:

Great, thanks. I added .module to everything before .fc including the optimizer.