FMTrainer¶

class FMTrainer¶

Bases: Trainer

Trainer for Factorization Machine models.

Methods

__init__

Initializes the trainer for Factorization Machine models.

to_poly

Converts a trained model to QUBO model as amplify.Poly.

train

Trains the model using the provided dataset.

Attributes

batch_size

Returns the batch size for training.

epochs

Returns the number of epochs for training.

loss_class

The loss function name (only "mse" is supported).

low_percentile_corrcoefs

Returns the lower percentile correlation coefficients of the last trained model.

lr_scheduler_class

The learning rate scheduler name.

lr_scheduler_params

The current scheduler parameters (with the defaults filled in).

model

Returns the trained Factorization Machine model.

num_factors

Returns the number of factors in the model.

optimizer_class

The optimizer name (only "adam" is supported).

optimizer_params

The current optimizer parameters (with the defaults filled in).

percentile_cutoffs

Returns the lower percentile cutoffs for the trainer.

train_data_split_ratio

Returns the ratio of training data split for validation.

__init__(num_threads: int | None = None, seed: int | None = None) → None¶

Initializes the trainer for Factorization Machine models.

Parameters:
  • num_threads – Deprecated and ignored (now auto-threaded). Defaults to None.

  • seed – Seed for parameter init and data split/shuffle. None is random.

to_poly(x: PolyArray[tuple[N]]) → Poly¶

Converts a trained model to QUBO model as amplify.Poly.

Parameters:

x (amplify.PolyArray[amplify.Dim1]) – The variables in amplify.PolyArray.

Raises:
  • ValueError – If the input array is not 1D.

  • ValueError – If the input length does not match the number of features.

  • ValueError – If the surrogate model has not been trained.

Returns:

The resulting QUBO polynomial.

Return type:

amplify.Poly

train(dataset: Dataset) → None¶

Trains the model using the provided dataset.

Parameters:

dataset (Dataset) – The dataset to train on.

Raises:
  • ValueError – If the dataset is empty.

  • ValueError – If the number of input values and output values are not the same.

  • ValueError – If the input dataset is not 2D.

  • ValueError – If step_size is less than 1 (steplr) or T_max is less than 1 (cosine).

__abstractmethods__ = frozenset({})¶
__dict__ = mappingproxy({'__module__': 'amplify_bbopt.trainer', '__doc__': 'Trainer for Factorization Machine models.', '__init__': <function FMTrainer.__init__>, 'model': <property object>, 'num_factors': <property object>, 'batch_size': <property object>, 'epochs': <property object>, 'loss_class': <property object>, 'optimizer_class': <property object>, 'optimizer_params': <property object>, 'lr_scheduler_class': <property object>, 'lr_scheduler_params': <property object>, 'train_data_split_ratio': <property object>, 'train': <function FMTrainer.train>, 'to_poly': <function FMTrainer.to_poly>, '__abstractmethods__': frozenset(), '_abc_impl': <_abc._abc_data object>, '__annotations__': {'_fm': 'FactorizationMachine | None', '_num_factors': 'int', '_batch_size': 'int', '_epochs': 'int', '_train_data_split_ratio': 'float', '_optimizer_params': 'dict[str, Any]', '_loss_name': 'LossName', '_optimizer_name': 'OptimizerName', '_lr_scheduler_name': 'LRSchedulerName | None', '_lr_scheduler_params': 'dict[str, Any]', '_percentile_cutoffs': 'list[int]', '_low_percentile_corrcoefs': 'dict[int, float] | None'}})¶
__slots__ = ()¶
__weakref__¶

list of weak references to the object (if defined)

property batch_size: int¶

Returns the batch size for training.

property epochs: int¶

Returns the number of epochs for training.

property loss_class: Literal['mse']¶

The loss function name (only "mse" is supported).

property low_percentile_corrcoefs: dict[int, float] | None¶

Returns the lower percentile correlation coefficients of the last trained model.

property lr_scheduler_class: Literal['steplr', 'exponential', 'cosine'] | None¶

The learning rate scheduler name.

One of "steplr" / "exponential" / "cosine", or None to disable decay (constant lr; lr_scheduler_params is ignored). Each scheduler uses its own set of lr_scheduler_params keys; see lr_scheduler_params.

property lr_scheduler_params: dict[str, Any]¶

The current scheduler parameters (with the defaults filled in).

The keys depend on lr_scheduler_class (defaults in parentheses):

  • "steplr": step_size (100), gamma (0.8). lr = lr0 * gamma ** (epoch // step_size).

  • "exponential": gamma (0.1). lr = lr0 * gamma ** epoch.

  • "cosine": T_max (2000), eta_min (0.0). lr = eta_min + (lr0 - eta_min) * (1 + cos(pi * epoch / T_max)) / 2.

  • None: empty (constant lr).

Note that gamma is applied every step_size epochs for "steplr" but every epoch for "exponential", so suitable values differ between the two. step_size and T_max must be at least 1 (checked by train()).

property model: FactorizationMachine | None¶

Returns the trained Factorization Machine model.

property num_factors: int¶

Returns the number of factors in the model.

property optimizer_class: Literal['adam']¶

The optimizer name (only "adam" is supported).

property optimizer_params: dict[str, Any]¶

The current optimizer parameters (with the defaults filled in).

Keys (defaults in parentheses): lr (0.5), betas ((0.9, 0.999)), eps (1e-8), weight_decay (0.01), amsgrad (False).

property percentile_cutoffs: list[int]¶

Returns the lower percentile cutoffs for the trainer.

property train_data_split_ratio: float¶

Returns the ratio of training data split for validation.