'NoneType' object has no attribute 'parameters'

Below is the training function I am writing, yet, there is this error: ‘NoneType’ object has no attribute ‘parameters’. I don’t really know how to solve it

def train(net, train_loader, test_loader, logger):    
    # TODO: Define the SGD optimizer here. Use hyper-parameters from cfg
    
    optimizer = optim.SGD(net.parameters(),lr=cfg['lr'],momentum=cfg['momentum'],weight_decay=cfg['weight_decay'], nesterov=cfg['nesterov'])
    # TODO: Define the criterion (Objective Function) that you will be using
    criterion = nn.CrossEntropyLoss()

It seems you are passing the net argument as a None so check where net is created and that it holds a valid nn.Module object.
My guess would be that you might be using a convenient function to create the model and might have forgotten to return the model itself. E.g. this would break with the same error:

def create_model():
    net = nn.Linear(1, 1)
    # remove the '#' and it'll work
    # return net  

net = create_model()
net.parameters()
> AttributeError: 'NoneType' object has no attribute 'parameters'
1 Like