adapt.parameter_based.RegularTransferGP
- class adapt.parameter_based.RegularTransferGP(estimator=None, Xt=None, yt=None, lambda_=1.0, copy=True, verbose=1, random_state=None, **params)[source]
Regular Transfer with Gaussian Process
RegularTransferGP is a parameter-based domain adaptation method.
The method is based on the assumption that a good target estimator can be obtained by adapting the parameters of a pre-trained source estimator using a few labeled target data.
The approach consist in fitting the alpha coeficients of a Gaussian Process estimator on target data according to an objective function regularized by the euclidean distance between the source and target alpha:
\[\alpha_T = \underset{\alpha \in \mathbb{R}^n}{\text{argmin}} \, ||K_{TS} \alpha - y_T||^2 + \lambda ||\alpha - \alpha_S||^2\]Where:
\(\alpha_T\) are the target model coeficients.
\(\alpha_S = \underset{\alpha \in \mathbb{R}^n}{\text{argmin}} \, ||K_{SS} \alpha - y_S||^2\) are the source model coeficients.
\(y_S, y_T\) are respectively the source and the target labels.
\(K_{SS}\) is the pariwise kernel distance matrix between source input data.
\(K_{TS}\) is the pariwise kernel distance matrix between target and source input data.
\(n\) is the number of source data in \(X_S\)
\(\lambda\) is a trade-off parameter. The larger \(\lambda\) the closer the target model will be from the source model.
The
estimator
given toRegularTransferGP
should be from classessklearn.gaussian_process.GaussianProcessRegressor
orsklearn.gaussian_process.GaussianProcessClassifier
- Parameters
- estimatorsklearn estimator or tensorflow Model (default=None)
Estimator used to learn the task. If estimator is
None
, aLinearRegression
instance is used as estimator.- Xtnumpy array (default=None)
Target input data.
- ytnumpy array (default=None)
Target output data.
- lambda_float (default=1.0)
Trade-Off parameter. For large
lambda_
, the target model will be similar to the source model.- copyboolean (default=True)
Whether to make a copy of
estimator
or not.- verboseint (default=1)
Verbosity level.
- random_stateint (default=None)
Seed of random generator.
- paramskey, value arguments
Arguments given at the different level of the adapt object. It can be, for instance, compile or fit parameters of the estimator or kernel parameters etc… Accepted parameters can be found by calling the method
_get_legal_params(params)
.
See also
References
- 1
[1] C. Chelba and A. Acero. “Adaptation of maximum entropy classifier: Little data can help a lot”. In EMNLP, 2004.
Examples
>>> from sklearn.gaussian_process import GaussianProcessRegressor >>> from sklearn.gaussian_process.kernels import Matern, WhiteKernel >>> from adapt.utils import make_regression_da >>> from adapt.parameter_based import RegularTransferGP >>> Xs, ys, Xt, yt = make_regression_da() >>> kernel = Matern() + WhiteKernel() >>> src_model = GaussianProcessRegressor(kernel) >>> src_model.fit(Xs, ys) >>> print(src_model.score(Xt, yt)) -2.3409379221035382 >>> tgt_model = RegularTransferGP(src_model, lambda_=1.) >>> tgt_model.fit(Xt[:3], yt[:3]) >>> tgt_model.score(Xt, yt) -0.21947435769240653
- Attributes
- estimator_Same class as estimator
Fitted Estimator.
Methods
__init__
([estimator, Xt, yt, lambda_, copy, ...])fit
([Xt, yt])Fit RegularTransferGP.
fit_estimator
(X, y[, sample_weight, ...])Fit estimator on X, y.
Get metadata routing of this object.
get_params
([deep])Get parameters for this estimator.
predict
(X[, domain])Return estimator predictions after adaptation.
predict_estimator
(X, **predict_params)Return estimator predictions for X.
score
(X, y[, sample_weight, domain])Return the estimator score.
set_fit_request
(*[, domains])Request metadata passed to the
fit
method.set_params
(**params)Set the parameters of this estimator.
set_predict_request
(*[, domain])Request metadata passed to the
predict
method.set_score_request
(*[, domain, sample_weight])Request metadata passed to the
score
method.unsupervised_score
(Xs, Xt)Return unsupervised score.
- __init__(estimator=None, Xt=None, yt=None, lambda_=1.0, copy=True, verbose=1, random_state=None, **params)[source]
- fit(Xt=None, yt=None, **fit_params)[source]
Fit RegularTransferGP.
- Parameters
- Xtnumpy array (default=None)
Target input data.
- ytnumpy array (default=None)
Target output data.
- fit_paramskey, value arguments
Not used. Here for sklearn compatibility.
- Returns
- selfreturns an instance of self
- fit_estimator(X, y, sample_weight=None, random_state=None, warm_start=True, **fit_params)[source]
Fit estimator on X, y.
- Parameters
- Xarray
Input data.
- yarray
Output data.
- sample_weightarray
Importance weighting.
- random_stateint (default=None)
Seed of the random generator
- warm_startbool (default=True)
If True, continue to fit
estimator_
, else, a new estimator is fitted based on a copy ofestimator
. (Be sure to setcopy=True
to usewarm_start=False
)- fit_paramskey, value arguments
Arguments given to the fit method of the estimator and to the compile method for tensorflow estimator.
- Returns
- estimator_fitted estimator
- get_metadata_routing()[source]
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns
- routingMetadataRequest
A
MetadataRequest
encapsulating routing information.
- get_params(deep=True)[source]
Get parameters for this estimator.
- Parameters
- deepbool, default=True
Not used, here for scikit-learn compatibility.
- Returns
- paramsdict
Parameter names mapped to their values.
- predict(X, domain=None, **predict_params)[source]
Return estimator predictions after adaptation.
For feature-based method (object which implements a
transform
method), the input featureX
are first transformed. Then thepredict
method of the fitted estimatorestimator_
is applied on the transformedX
.- Parameters
- Xarray
input data
- domainstr (default=None)
For antisymetric feature-based method, different transformation of the input X are applied for different domains. The domain should then be specified between “src” and “tgt”. If
None
the default transformation is the target one.
- Returns
- y_predarray
prediction of the Adapt Model.
- predict_estimator(X, **predict_params)[source]
Return estimator predictions for X.
- Parameters
- Xarray
input data
- Returns
- y_predarray
prediction of estimator.
- score(X, y, sample_weight=None, domain=None)[source]
Return the estimator score.
If the object has a
transform
method, the estimator is applied on the transformed features X. For antisymetric transformation, a parameter domain can be set to specified between source and target transformation.Call score on sklearn estimator and evaluate on tensorflow Model.
- Parameters
- Xarray
input data
- yarray
output data
- sample_weightarray (default=None)
Sample weights
- domainstr (default=None)
This parameter specifies for antisymetric feature-based method which transformation will be applied between “source” and “target”. If
None
the transformation by default is the target one.
- Returns
- scorefloat
estimator score.
- set_fit_request(*, domains: Union[bool, None, str] = '$UNCHANGED$') adapt.parameter_based._regular.RegularTransferGP [source]
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.New 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
- domainsstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
domains
parameter infit
.
- Returns
- selfobject
The updated object.
- set_params(**params)[source]
Set the parameters of this estimator.
- Parameters
- **paramsdict
Estimator parameters.
- Returns
- selfestimator instance
Estimator instance.
- set_predict_request(*, domain: Union[bool, None, str] = '$UNCHANGED$') adapt.parameter_based._regular.RegularTransferGP [source]
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.New 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
- domainstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
domain
parameter inpredict
.
- Returns
- selfobject
The updated object.
- set_score_request(*, domain: Union[bool, None, str] = '$UNCHANGED$', sample_weight: Union[bool, None, str] = '$UNCHANGED$') adapt.parameter_based._regular.RegularTransferGP [source]
Request metadata passed to the
score
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 toscore
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it toscore
.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.New 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
- domainstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
domain
parameter inscore
.- sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED
Metadata routing for
sample_weight
parameter inscore
.
- Returns
- selfobject
The updated object.