How to exclude model __init__
parameters from the lightning CLI?
#19245
-
I have a model that looks something like this: class MyModel(L.LightningModule):
def __init__(
self,
data_mean: float = 0,
data_std: float = 1,
learning_rate: float = 1e-3,
) -> None:
super().__init__()
self.data_mean = data_mean
self.data_std = data_std
self.learning_rate = learning_rate
...
def training_step(self, batch):
normalized_output = self(batch)
denormalized_output = normalized_output * self.data_std + self.data_mean
...
def configure_optimizers(self):
optimizer = torch.optim.Adam(self.parameters(), lr=self.learning_rate)
return optimizer
... Here, My main script looks something like this: def main() -> None:
cli = LightningCLI(MyModel, MyDataModule, run=False)
data = MyDataModule("data")
data.setup()
model = MyModel(
data_mean=data.train_mean.item(),
data_std=data.train_std.item(),
)
trainer = L.Trainer()
trainer.fit(model, data, ...)
if __name__ == "__main__":
main() Here, the idea would be to access the CLI arguments through the Is there a way to exclude such parameters from the inferred CLI options or am I using |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
The purpose of the config file that Note that you have defined these parameters as non-optional. That means that every time an instance of |
Beta Was this translation helpful? Give feedback.
A parameter that is in
__init__
is something that should be specified by the user and not computed. If you compute something based on init params, then internally give it a different name since it is not the same as what gets provided.What you need is argument linking. Have a look at #13403 (comment), also cli argument linking and jsonargparse argument linking. You could link the entire data module. Though, it might be cleaner to link a
@property
that provides whatever needs computing.Note. When an argument is linked, it doesn't show up in the config. Since the value is derived instead of provided.