FMTrainer¶
- class FMTrainer¶
Bases:
TrainerTrainer for Factorization Machine models.
Methods
Initializes the trainer for Factorization Machine models.
Converts a trained model to QUBO model as amplify.Poly.
Trains the model using the provided dataset.
Attributes
Returns the batch size for training.
Returns the number of epochs for training.
The loss function name (only
"mse"is supported).Returns the lower percentile correlation coefficients of the last trained model.
The learning rate scheduler name.
The current scheduler parameters (with the defaults filled in).
Returns the trained Factorization Machine model.
Returns the number of factors in the model.
The optimizer name (only
"adam"is supported).The current optimizer parameters (with the defaults filled in).
Returns the lower percentile cutoffs for the trainer.
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.
Noneis 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_sizeis less than 1 (steplr) orT_maxis 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 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", orNoneto disable decay (constantlr;lr_scheduler_paramsis ignored). Each scheduler uses its own set oflr_scheduler_paramskeys; seelr_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 (constantlr).
Note that
gammais applied everystep_sizeepochs for"steplr"but every epoch for"exponential", so suitable values differ between the two.step_sizeandT_maxmust be at least 1 (checked bytrain()).
- property model: FactorizationMachine | None¶
Returns the trained Factorization Machine model.