def train_and_save_all(trainsize, num_modes, regs): """Train and save ROMs with the given dimension and regularization. Parameters ---------- trainsize : int Number of snapshots to use to train the ROM(s). num_modes : int or list(int) Dimension of the ROM(s) to train, i.e., the number of retained POD modes (left singular vectors) used to project the training data. regs : float or list(float) regularization parameter(s) to use in the training. """ utils.reset_logger(trainsize) logging.info(f"TRAINING {len(num_modes)*len(regs)} ROMS") for r in num_modes: # Load training data. X_, Xdot_, time_domain, _ = utils.load_projected_data(trainsize, r) # Evaluate inputs over the training time domain. Us = config.U(time_domain) # Train and save each ROM. for reg in regs: with utils.timed_block(f"Training ROM with r={r:d}, reg={reg:e}"): rom = train_rom(X_, Xdot_, Us, reg) if rom: rom.save_model(config.rom_path(trainsize, r, reg), save_basis=False, overwrite=True)
def save_trained_rom(trainsize, r, regs, rom): """Save the trained ROM with the specified attributes. Parameters ---------- trainsize : int Number of snapshots used to train the ROM. r : int Dimension of the ROM. Also the number of retained POD modes (left singular vectors) used to project the training data. regs : two or three non-negative floats regularization hyperparameters (first-order, quadratic, cubic) used in the Operator Inference least-squares problem for training the ROM. rom : rom_operator_inference.InferredContinuousROM Actual trained ROM object. Must have a `save_model()` method. """ save_path = config.rom_path(trainsize, r, regs) rom.save_model(save_path, save_basis=False, overwrite=True) logging.info(f"ROM saved to {save_path}")
def save_best_trained_rom(trainsize, r, reg, rom): """Save the trained ROM with the specified attributes. Parameters ---------- trainsize : int Number of snapshots used to train the ROM. r : int Dimension of the trained ROM, i.e., the number of retained POD modes (left singular vectors) used to project the training data. reg : float > 0 Regularization parameter used in the training. rom : rom_operator_inference.InferredContinuousROM Actual trained ROM object. Must have a `save_model()` method. """ save_path = config.rom_path(trainsize, r, reg) with utils.timed_block(f"Best regularization for r={r:d}: {reg:.0f}"): rom.save_model(save_path, save_basis=False, overwrite=True) logging.info(f"ROM saved to {save_path}")
def load_rom(trainsize, r, regs): """Load a single trained ROM. Parameters ---------- trainsize : int Number of snapshots used to train the ROM. This is also the number of snapshots that were used when the POD basis (SVD) was computed. r : int Dimension of the ROM. Also the number of retained POD modes (left singular vectors) used to project the training data. regs : one, two, or three positive floats Regularization hyperparameters used in the Operator Inference least-squares problem for training the ROM. Returns ------- rom : opinf.InferredContinuousROM The trained reduced-order model. """ # Locate the data. data_path = _checkexists(config.rom_path(trainsize, r, regs)) # Extract the trained ROM. try: rom = opinf.load_model(data_path) except FileNotFoundError as e: raise DataNotFoundError(f"could not locate ROM with {trainsize:d} " f"training snapshots, r={r:d}, and " f"{config.REGSTR(regs)}") from e # Check ROM dimension. if rom.r != r: raise RuntimeError(f"rom.r = {rom.r} != {r}") rom.trainsize, rom.regs = trainsize, regs return rom
def load_rom(trainsize, num_modes, reg): """Load a single trained ROM. Parameters ---------- trainsize : int The number of snapshots used to train the ROM. This is also the number of snapshots that were used when the POD basis (SVD) was computed. num_modes : int The dimension of the ROM. This is also the number of retained POD modes (left singular vectors) used to project the training data. reg : float The regularization factor used in the Operator Inference least-squares problem for training the ROM. Returns ------- rom : roi.InferredContinuousROM The trained reduced-order model. """ # Locate the data. data_path = _checkexists(config.rom_path(trainsize, num_modes, reg)) # Extract the trained ROM. try: rom = roi.load_model(data_path) except FileNotFoundError as e: raise DataNotFoundError(f"could not locate ROM with {trainsize:d} " f"training snapshots, r={num_modes}, and " f"reg={reg:e}") from e # Check data shapes. if rom.r != num_modes: raise RuntimeError(f"rom.r = {rom.r} != {num_modes}") return rom