ptyrad.params.model_params#

Defines available options and validation rules for the model_params dictionary.

pydantic model ptyrad.params.model_params.ModelParams[source]#

Bases: BaseModel

optimizer configurations are specified in ‘optimizer_params’, see https://pytorch.org/docs/stable/optim.html for detailed information of available optimizers and configs. update behaviors of optimizable variables (tensors) are specified in ‘update_params’. ‘start_iter’ specifies the iteration at which the variables (tensors) can start being updated by automatic differentiation (AD) ‘lr’ specifies the learning rate for the variables (tensors) Usually slower learning rate leads to better convergence/results, but is also updating slower. The variable optimization has 2 steps, (1) calculate gradient and (2) apply update based on learning rate * gradient ‘start_iter: null’ will disable grad calculation and would not update the variable regardless the learning rate through out the whole reconstruction ‘start_iter: N(int)’ would only calculate the grad when iteration >= N, so no grad will be calculated when iteration < N Therefore, only the variable with non-zero learning rate would be optimized when iteration > start_iter. If you don’t want/need to optimize certain parameters, set their start_iter to null AND learning rate to 0 for faster computation. Typical learning rate is 1e-3 to 1e-4.

Fields:
param detector_blur_std: float | None = None#

This applies Gaussian blur to the forward model simulated diffraction patterns to emulate the PSF of high-energy electrons on detector for experimental data. Typical value is 0-1 px (std) based on the acceleration voltage

param preload_data: bool | None = True#

type: bool. This flag determines how the measurements data is stored and transferred to device during reconstruciton. If true, measurement data will be fully loaded into device memory during model initialization for best performance. However, dataset larger than device memory (i.e., GPU VRAM) would throw Out-Of-Memory error. If ‘preload_data’: false, measurement data is kept on host memory (CPU RAM) and only the mini-batch is transferred to device memory in a streaming way. This would enable reconstruction of large dataset that doesn’t fit into GPU VRAM with a little data transfer overhead. The default is ‘true’ for performance although the difference is negligible on demo datasets.

param optimizer_params: OptimizerParams [Optional]#

Support all PyTorch optimizer. The suggested optimizer is ‘Adam’ with default configs (null). You can load the previous optimizer state by passing the path of model.hdf5 to load_state, this way you can continue previous reconstruciton smoothly without abrupt gradients. (Because lots of the optimizers are adaptive and have history-dependent learning rate manipulation, so loading the optimizer state is necessary if you want to continue the previous optimization trajectory). However, the optimizer state must be coming from previous reconstructions with the same set of optimization variables with identical size of the dimensions otherwise it won’t run.

param scheduler_params: SchedulerParams | None = None#

Optional learning rate scheduler. Set to e.g. {name: CosineAnnealingLR, configs: {T_max: 500}} to decay the learning rate during reconstruction. Supports any class from torch.optim.lr_scheduler (https://pytorch.org/docs/stable/optim.html#how-to-adjust-learning-rate) except ChainedScheduler and SequentialLR because both of them require other scheduler objects as constructor arguments, so you will have to specify them after param loading if you want to use them. ReduceLROnPlateau is handled automatically (mean loss is passed). Set load_state to a model.hdf5 path to resume mid-schedule. Note: not compatible with LBFGS optimizer (scheduler will be ignored with a warning).

param update_params: UpdateParams [Optional]#
pydantic model ptyrad.params.model_params.OptimizerParams[source]#

Bases: BaseModel

Fields:
param name: Literal['Adadelta', 'Adafactor', 'Adagrad', 'Adam', 'AdamW', 'SparseAdam', 'Adamax', 'ASGD', 'LBFGS', 'Muon', 'Nadam', 'RAdam', 'RMSprop', 'Rprop', 'SGD'] = 'Adam'#
param configs: Dict[str, Any] [Optional]#
param load_state: FilePath | None = None#
serialize_model()[source]#

Custom serializer to convert pathlib.Path back to str.

pydantic model ptyrad.params.model_params.SchedulerParams[source]#

Bases: BaseModel

Fields:
param name: Literal['LambdaLR', 'MultiplicativeLR', 'StepLR', 'MultiStepLR', 'ConstantLR', 'LinearLR', 'ExponentialLR', 'PolynomialLR', 'CosineAnnealingLR', 'ReduceLROnPlateau', 'CyclicLR', 'OneCycleLR', 'CosineAnnealingWarmRestarts'] [Required]#
param configs: Dict[str, Any] [Optional]#
param load_state: FilePath | None = None#
param step_unit: Literal['iter', 'batch'] = 'iter'#
serialize_model()[source]#

Custom serializer to convert pathlib.Path back to str.

pydantic model ptyrad.params.model_params.UpdateParams[source]#

Bases: BaseModel

Fields:
param obja: Dict[str, int | float | None] = {'lr': 0.0005, 'start_iter': 1}#
param objp: Dict[str, int | float | None] = {'lr': 0.0005, 'start_iter': 1}#
param obj_tilts: Dict[str, int | float | None] = {'lr': 0.0, 'start_iter': None}#
param slice_thickness: Dict[str, int | float | None] = {'lr': 0.0, 'start_iter': None}#
param probe: Dict[str, int | float | None] = {'lr': 0.0001, 'start_iter': 1}#
param probe_pos_shifts: Dict[str, int | float | None] = {'lr': 0.0005, 'start_iter': 1}#