This is a template class, that should be inherited by any neural net emulator model. Methods that need to be implemented by your concrete NN model (just as if you would define a torch.nn.Module): - __init__(.) - forward(.) The other methods may be overridden as needed.
| 21 | |
| 22 | |
| 23 | class BaseModel(LightningModule): |
| 24 | """ |
| 25 | This is a template class, that should be inherited by any neural net emulator model. |
| 26 | Methods that need to be implemented by your concrete NN model (just as if you would define a torch.nn.Module): |
| 27 | - __init__(.) |
| 28 | - forward(.) |
| 29 | |
| 30 | The other methods may be overridden as needed. |
| 31 | It is recommended to define the attribute |
| 32 | - self.example_input_array = torch.randn(<YourModelInputShape>) # batch dimension can be anything, e.g. 7 |
| 33 | |
| 34 | ------------ |
| 35 | A LightningModule organizes your PyTorch code into 5 sections: |
| 36 | - Computations (init). |
| 37 | - Train loop (training_step) |
| 38 | - Validation loop (validation_step) |
| 39 | - Test loop (test_step) |
| 40 | - Optimizers (configure_optimizers) |
| 41 | |
| 42 | Read the docs: |
| 43 | https://pytorch-lightning.readthedocs.io/en/latest/common/lightning_module.html |
| 44 | """ |
| 45 | |
| 46 | def __init__(self, |
| 47 | datamodule_config: DictConfig = None, |
| 48 | optimizer: Optional[DictConfig] = None, |
| 49 | scheduler: Optional[DictConfig] = None, |
| 50 | monitor: Optional[str] = None, |
| 51 | mode: str = "min", |
| 52 | loss_function: str = "mean_squared_error", |
| 53 | downwelling_loss_contribution: float = 0.5, |
| 54 | upwelling_loss_contribution: float = 0.5, |
| 55 | heating_rate_loss_contribution: float = 0.0, |
| 56 | input_transform: Optional[AbstractTransform] = None, |
| 57 | output_normalizer: Optional[Dict[str, NormalizationMethod]] = None, |
| 58 | out_layer_bias_init: Optional[np.ndarray] = None, |
| 59 | name: str = "", |
| 60 | verbose: bool = True, |
| 61 | ): |
| 62 | super().__init__() |
| 63 | self.log_text = get_logger(name=self.__class__.__name__ if name == '' else name) |
| 64 | self.name = name |
| 65 | self.verbose = verbose |
| 66 | if not self.verbose: |
| 67 | self.log_text.setLevel(logging.WARN) |
| 68 | if input_transform is None or isinstance(input_transform, AbstractTransform): |
| 69 | self.input_transform = input_transform |
| 70 | else: |
| 71 | self.input_transform = hydra.utils.instantiate(input_transform) |
| 72 | if datamodule_config is not None: |
| 73 | input_output_dimensions = get_data_dims(exp_type=datamodule_config.get("exp_type")) |
| 74 | self.raw_input_dim = input_output_dimensions['input_dim'] |
| 75 | self.raw_output_dim = input_output_dimensions['output_dim'] |
| 76 | self.raw_spatial_dim = input_output_dimensions['spatial_dim'] |
| 77 | self.num_layers = self.raw_spatial_dim[LAYERS] |
| 78 | self.num_levels = self.raw_spatial_dim[LEVELS] |
| 79 | |
| 80 | self.output_normalizer = output_normalizer.copy() if output_normalizer is not None else None |
nothing calls this directly
no outgoing calls
no test coverage detected