Skip to content

Model

get_recent_weights_path

get_recent_weights_path(
    exp_dir, exp_mark, weights_name=None
)

Get a model recent weights path.

Parameters:

Name Type Description Default
exp_dir Path

Experiment directory path.

required
exp_mark str

Experiment folder mark.

required
weights_name str, default

Weights filename.

None

Returns:

Type Description
Optional[Path]

Recent weights path if it exists otherwise None object.

Source code in src/trecover/utils/model.py
def get_recent_weights_path(exp_dir: Path,
                            exp_mark: str,
                            weights_name: Optional[str] = None
                            ) -> Optional[Path]:
    """
    Get a model recent weights path.

    Parameters
    ----------
    exp_dir : Path
        Experiment directory path.
    exp_mark : str
        Experiment folder mark.
    weights_name : str, default=None
        Weights filename.

    Returns
    -------
    Optional[Path]:
        Recent weights path if it exists otherwise None object.

    """

    if weights_name:
        return weights_path if (weights_path := exp_dir / exp_mark / weights_name).exists() else None

    if (weights_path := exp_dir / exp_mark / 'weights').exists():
        recent_weights = None
        most_recent_timestamp = 0

        for weights in weights_path.iterdir():
            if timestamp := weights.stat().st_ctime > most_recent_timestamp:
                recent_weights = weights
                most_recent_timestamp = timestamp

        return recent_weights

get_model

get_model(
    token_size,
    pe_max_len,
    num_layers,
    d_model,
    n_heads,
    d_ff,
    dropout,
    device=torch.device("cpu"),
    weights=None,
    silently=False,
)

Get a model with specified configuration.

Parameters:

Name Type Description Default
token_size int

Token (column) size.

required
pe_max_len int

Positional encoding max length.

required
num_layers int

Number of encoder and decoder blocks

required
d_model int

Model dimension - number of expected features in the encoder (decoder) input.

required
n_heads int

Number of encoder and decoder attention heads.

required
d_ff int

Dimension of the feedforward layer.

required
dropout float

Dropout range.

required
device torch.device, default

Device on which to allocate the model.

torch.device('cpu')
weights Path, default

Model weights path for initialization.

None
silently bool, default

Initialize the model silently without any verbose information.

False

Returns:

Name Type Description
model TRecover

Initialized model.

Raises:

Type Description
SystemExit:

If the weight's path is not provided and the cli 'stop' option is selected.

Source code in src/trecover/utils/model.py
def get_model(token_size: int,
              pe_max_len: int,
              num_layers: int,
              d_model: int,
              n_heads: int,
              d_ff: int,
              dropout: float,
              device: torch.device = torch.device('cpu'),
              weights: Optional[Path] = None,
              silently: bool = False
              ) -> TRecover:
    """
    Get a model with specified configuration.

    Parameters
    ----------
    token_size : int
        Token (column) size.
    pe_max_len : int
        Positional encoding max length.
    num_layers : int
        Number of encoder and decoder blocks
    d_model : int
        Model dimension - number of expected features in the encoder (decoder) input.
    n_heads : int
        Number of encoder and decoder attention heads.
    d_ff : int
        Dimension of the feedforward layer.
    dropout : float,
        Dropout range.
    device : torch.device, default=torch.device('cpu')
        Device on which to allocate the model.
    weights : Path, default=None
        Model weights path for initialization.
    silently : bool, default=False
        Initialize the model silently without any verbose information.

    Returns
    -------
    model : TRecover
        Initialized model.

    Raises
    ------
    SystemExit:
        If the weight's path is not provided and the cli 'stop' option is selected.

    """

    model = TRecover(token_size, pe_max_len, num_layers, d_model, n_heads, d_ff, dropout).to(device)

    if weights and weights.exists() and weights.is_file():
        model.load_parameters(weights, device=device)

        if not silently:
            log.project_console.print(f'The below model parameters have been loaded:\n{weights}',
                                      style='bright_green')
        return model

    if weights:
        log.project_console.print(f'Failed to load model parameters: {str(weights)}', style='bold red')
    else:
        log.project_console.print("Model parameters aren't specified", style='bright_blue')

    if silently or Confirm.ask(prompt='[bright_blue]Continue training from scratch?', default=True,
                               console=log.project_console):
        return model
    else:
        raise SystemExit