ValueError: optimizer got an empty parameter list

Hi everyone,

I am new to the field of deep learning. As part of my current project, I am trying to run a simple classification pipeline with pytorch-lightning but get a “ValueError: optimizer got an empty parameter list.” error at training and I am so far unable to figure out where the problem is.

The following is my LightningModule code:

class RainforestPipelinePL(LightningModule):
    def __init__(
                self,
                model,
                metric,
                criterion = FocalLoss,
                lr: float = 0.001,
                random_seed: int = 1234,
                n_mels: int = 224,
                fmin: int = 40,
                fmax: int = 24000):
        
        super(RainforestPipelinePL, self).__init__()
        self.model = model
        self.metric = metric
        self.criterion = criterion 
        self.lr = lr
        self.save_hyperparameters()
        
        
    def forward(self, x, targets, *args, **kwargs):
        return self.model(x, targets)
    
    
    def training_step(self, batch, batch_idx):
        data, targets = batch
        logits = self(data, targets)
        train_loss = self.criterion(logits, targets)
        return train_loss

    def validation_step(self, batch, batch_idx):
        data, targets = batch
        logits = self(data, targets)
        val_loss = self.criterion(logits, targets)

        p = torch.sigmoid(logits)
        
        score_class, weight = self.metric(targets.cpu().numpy(), p.cpu().numpy())
        score = (score_class * weight).sum()

        self.log("val_lrap", score, on_step=False, on_epoch=True, prog_bar=True)
        self.log('val_loss', val_loss, on_step=False, on_epoch=True, prog_bar=True)
        
    
    def configure_optimizers(self):       
        optimizer = torch.optim.SGD(self.parameters(), lr=self.lr, weight_decay=0.0001, momentum=0.9)
        scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=7, gamma=0.4)

        lr_scheduler = {"scheduler": scheduler }
        return [optimizer], [lr_scheduler]

Here is the actual model:

class RainforestModel(nn.Module):
    def __init__(self, num_classes = 24):
        super().__init__()
        model = resnest50(pretrained=True)

        model.fc = nn.Sequential(
            nn.Linear(2048, 1024),
            nn.ReLU(),
            nn.Dropout(p=0.2),
            nn.Linear(1024, 1024),
            nn.ReLU(),
            nn.Dropout(p=0.2),
            nn.Linear(1024, num_classes),
        )

        self.net = model
        
        pos_weights = torch.ones(num_classes)

        self.criterion = FocalLoss()

    def forward(self, x, targets):
        logits = self.net(x)
        
        return logits

And the actual function for training:

def train_fold(experiment_version, fold, max_epochs, train_folds_df, lr=0.001):
    log_dir = "./lightning_logs"
    
    full_log_dir_path = os.path.join(log_dir, "melspec", "version_" + experiment_version, "fold_" + str(fold))
    checkpoint_callback = ModelCheckpoint(monitor = "val_lrap",
                                        dirpath = full_log_dir_path,
                                        filename = "{epoch:02d}-{vr:.3f}-{vp:.3f}-{val_lrap:.3f}",
                                        mode = "max")

    logger = TensorBoardLogger("lightning_logs", name="melspec", version="version_" + experiment_version)

    trainer = Trainer(gpus=1,
                    max_epochs=max_epochs,
                    callbacks=[checkpoint_callback],
                    logger=logger)
    
    dm = RainforestDataModule(train_folds_df = train_folds_df,  fold = fold)
    rfcx_model = RainforestPipelinePL(model = RainforestModel, metric = lwlrap, lr = lr)
    
    trainer.fit(rfcx_model, dm)

    print(
        "Completed training for experiment version: ",
        "version_" + experiment_version,
        "fold: " + str(fold),

Anyone’s help would be greatly appreciated.

looks good. you forgot parentheses here:

rfcx_model = RainforestPipelinePL(model = RainforestModel(), metric = lwlrap, lr = lr)

Thank you for your swift response it worked. Can believe I lost hours on this.