Pattern for specifying modules in argument parser versus CLI #12574
-
With the new experimental CLI, I've been trying to think of how to reconcile using the Let's say we have an abstract from ast import literal_eval
from torch import nn
import pytorch_lightning as pl
class LitModule(pl.LightningModule):
def __init__(self, input_dim: int, output_dim: int, activation: str):
# cross your fingers it resolves, something like `nn.SilU`
act_class = literal_eval(activation)
# instantiate the activation function object
self.model = nn.Sequential(nn.Linear(input_dim, output_dim), act_class()) With ...
@staticmethod
def add_model_specific_args(parent_parser):
parser = parent_parser.add_argument_group("LitModule")
parser.add_argument("--input_dim", type=int)
parser.add_argument("--output_dim", type=int)
parser.add_argument("--activation", type=str, default="nn.SiLU", help="Reference to activation function class in `torch.nn`.") And in my training script: from torch import nn
parser = ArgumentParser()
parser = LitModule.add_model_specific_args(parser)
args = parser.parse_args()
model = LitModule(args) With the new CLI, the better option would be to use class LitModule(pl.LightningModule):
def __init__(self, input_dim: int, output_dim: int, activation: nn.Module):
# don't need to use `literal_eval` anymore
self.model = nn.Sequential(nn.Linear(input_dim, output_dim), activation()) ...and in my YAML config: model:
class_path: mymodule.LitModule
init_args:
input_dim: 8
output_dim: 2
activation: torch.nn.SiLU I can't think of any way to implement this same pattern with |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
If you truly want to have both, you could do Regarding "i.e. putting the components together yourself, multiple optimizers, etc. which has a bit more flexibility" best if you give more details about what you want to do, and then see if it makes sense or not to use |
Beta Was this translation helpful? Give feedback.
If you truly want to have both, you could do
activation: Union[str, nn.Module]
and use theliteral_eval
only if anstr
is received. But even if you do this, there is little reason to keep usingadd_model_specific_args
since withLightningCLI
you could also give anstr
instead of theclass_path
andinit_args
pair.Regarding "i.e. putting the components together yourself, multiple optimizers, etc. which has a bit more flexibility" best if you give more details about what you want to do, and then see if it makes sense or not to use
LightningCLI
.