Step 4: Training

In lambeq, all low-level processing that takes place in training is hidden in the training package, which provides convenient high-level abstractions for all important supervised learning scenarios with the toolkit, classical and quantum. More specifically, the training package contains the following high-level/abstract classes and several concrete implementations for them:

  • Dataset: A class that provides functionality for easy management and manipulation of datasets, including batching, shuffling, and preparation based on the selected backend (tket, NumPy, PyTorch).

  • Model: The abstract interface for lambeq models. A model bundles the basic attributes and methods used for training, given a specific backend. It stores the symbols and the corresponding weights, and implements the forward pass of the model. Concrete implementations are the PytorchModel, TketModel, and NumpyModel classes (for more details see Section Choosing a model below).

  • Optimizer: a lambeq optimizer calculates the gradient of a given loss function with respect to the parameters of a model. It contains a step() method to modify the model parameters according to the optimizer’s update rule. Currently, we support the SPSA algorithm by [Spa1998], implemented in the SPSAOptimizer class.

  • Trainer: The main interface for supervised learning in lambeq. A trainer implements the (quantum) machine learning routine given a specific backend, using a loss function and an optimizer. Concrete implementations are the PytorchTrainer and QuantumTrainer classes.

The process of training a model involves the following steps:

  1. Instantiate the Model.

  2. Instantiate a Trainer, passing to it a model, a loss function, and an optimizer.

  3. Create a Dataset for training, and optionally, one for evaluation.

  4. Train the model by handing the dataset to the fit() method of the trainer.

Note

lambeq covers a wide range of training use cases, which are described in detail under lambeq use cases. Depending on your specific use case (e.g., classical or (simulated) quantum machine learning, etc.), you can choose from a variety of models and their according trainers. Refer to Section Choosing a model for a detailed overview of the available models and trainers.

The following examples demonstrate the usage of the training package for classical and quantum training scenarios.

See also: