qailab.torch.qmodel#
Module with QModel
Summary#
Classes:
Dummy HO |
|
Quantum model class |
Reference#
- class qailab.torch.qmodel.HybridOptimizer(model, lr_classical=0.01, lr_quantum=0.01, optimizer_quantum='SGD', optimizer_classical='Adam', betas=(0.9, 0.999), spsa_resamplings=1, spsa_gamma_decay=0.101, spsa_alpha_decay=0.602)[source]#
Bases:
object
Dummy HO
- class qailab.torch.qmodel.QModel(module: Module, loss: Callable, optimizer_type: type[Optimizer] | type[HybridOptimizer] | str = 'adamw', learning_rate: float | Literal['auto'] = 'auto', quantum_learning_rate: float | Literal['auto'] = 'auto', batch_size: int = 1, epochs: int = 1, validation_fraction: float = 0.2, shuffle: bool = True, device: Literal['cpu', 'cuda', 'mps'] = 'cpu', metric: Literal['accuracy', 'mse'] = 'accuracy')[source]#
Bases:
BaseEstimator
Quantum model class
Parameters#
- module: nn.Module
pytorch Module representing the quantum or classical neural network.
- loss: Callable
pytorch loss function to be used during training.
- optimizer_type: type[Optimizer] | str, default = “adamw”
pytorch Optimizer class to be used during training.
- learning_rate: float | Literal[‘auto’], default = “auto”
learning rate used by the optimizer, “auto” sets it to optimizer’s default one.
- quantum_learning_rate: float | Literal[‘auto’], default = ‘auto’
learning rate for quantum layers used by the HybridOptimizer, “auto” sets it to optimizer’s default one.
- batch_size: int, default = 1
number of training examples in batch.
- epochs: int, default = 1
number of epochs to train the model.
- validation_fraction: float, default = 0.2
share of the training dataset to be used for validation.
- shuffle: bool, default = True
whether to shuffle data every epoch.
- device: {“cpu”,”cuda”,”mps”}, default=”cpu”
the device neural network will be trained on.
Attributes#
- optimizer: Optimizer
pytorch optimizer object used during training
- loss_history: dict[str,list]
history of loss values from the last fit call. dict contains keys ‘training’ and ‘validation’
- module: Module#
- loss: Callable#
- optimizer_type: type[Optimizer] | type[HybridOptimizer]#
- learning_rate: float | Literal['auto']#
- quantum_learning_rate: float | Literal['auto']#
- optimizer: Optimizer#
- batch_size: int#
- epochs: int#
- validation_fraction: float#
- shuffle: bool#
- device: Literal['cpu', 'cuda', 'mps'] = 'cpu'#
- set_fit_request(*, x: bool | None | str = '$UNCHANGED$') QModel #
Request metadata passed to the
fit
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed tofit
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it tofit
.None
: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str
: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED
) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.Parameters#
- xstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
x
parameter infit
.
Returns#
- selfobject
The updated object.
- set_predict_request(*, x: bool | None | str = '$UNCHANGED$') QModel #
Request metadata passed to the
predict
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed topredict
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it topredict
.None
: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str
: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED
) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.Parameters#
- xstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
x
parameter inpredict
.
Returns#
- selfobject
The updated object.
- fit(x: Tensor | ndarray | DataFrame, y: Tensor | ndarray | DataFrame | Series) QModel [source]#
scikit-learn like fit method trains the neural network based on training set (x,y).
Parameters#
- x: Tensor | np.ndarray | pd.DataFrame
The training input samples of shape (n_samples, n_features).
- y: Tensor | np.array | pd.DataFrame | pd.Series
The training target values of shape (n_samples,) or (n_samples, n_outputs)
Returns#
- self: QModel
trained NN model
- fit_predict(x: Tensor | ndarray | DataFrame, y: Tensor | ndarray | DataFrame | Series) Tensor [source]#
scikit-learn like fit_predict method trains the neural network based on training set (x,y) and predicts values for training examples x combines fit and predict methods into one.
Parameters#
- x: Tensor | np.ndarray | pd.DataFrame
The training input samples of shape (n_samples, n_features).
- y: Tensor | np.array | pd.DataFrame | pd.Series
The training target values of shape (n_samples,) or (n_samples, n_outputs).
Returns#
- y_pred: Tensor
The predicted values for the training examples x.
- predict(x: Tensor | ndarray | DataFrame) Tensor [source]#
scikit-learn like predict method predicts values for examples input examples x.
Parameters#
- x: Tensor | np.ndarray | pd.DataFrame
The input samples of shape (n_samples, n_features).
Returns#
- y_pred: Tensor | np.ndarray | pd.DataFrame
The predicted values for examples x.