cblearn.embedding.FORTE#

class cblearn.embedding.FORTE(n_components=2, verbose=False, random_state=None, max_iter=2000, batch_size=50000, device='auto')[source]#

Fast Ordinal Triplet Embedding (FORTE).

FORTE [1] minimizes a kernel version of the triplet hinge soft objective as a smooth relaxation of the triplet error.

This estimator supports multiple implementations which can be selected by the backend parameter.

The torch backend uses the ADAM optimizer and backpropagation [2]. It can executed on CPU, but also CUDA GPUs. We optimize using BFSGS and Strong-Wolfe line search.

Note

The torch backend requires the pytorch python package (see Install Extra Requirements).

embedding_#

Final embedding, shape (n_objects, n_components)

stress_#

Final value of the SOE stress corresponding to the embedding.

n_iter_#

Final number of optimization steps.

Examples:

>>> from cblearn import datasets
>>> np.random.seed(42)
>>> true_embedding = np.random.rand(15, 2)
>>> triplets = datasets.make_random_triplets(true_embedding, result_format='list-order', size=1000)
>>> triplets.shape, np.unique(triplets).shape
((1000, 3), (15,))
>>> estimator = FORTE(n_components=2)
>>> embedding = estimator.fit_transform(triplets)
>>> embedding.shape
(15, 2)
>>> estimator.score(triplets) > 0.6
True

References

__init__(n_components=2, verbose=False, random_state=None, max_iter=2000, batch_size=50000, device='auto')[source]#

Initialize the estimator.

Parameters:
  • n_components – The dimension of the embedding.

  • verbose – boolean, default=False Enable verbose output.

  • random_state (None | int | RandomState) – The seed of the pseudo random number generator used to initialize the optimization.

  • max_iter – Maximum number of optimization iterations.

  • batch_size – Batch size of stochastic optimization. Only used with torch backend, else ignored.

  • device (str) – The device on which pytorch computes. {“auto”, “cpu”, “cuda”} “auto” chooses cuda (GPU) if available, but falls back on cpu if not. Only used with the torch backend, else ignored.

Methods

__init__([n_components, verbose, ...])

Initialize the estimator.

fit(X[, y, init, n_objects])

Computes the embedding.

fit_transform(X[, y])

Fit to data, then transform it.

get_metadata_routing()

Get metadata routing of this object.

get_params([deep])

Get parameters for this estimator.

predict(X[, result_format])

score(X[, y])

Triplet score on the estimated embedding.

set_fit_request(*[, init, n_objects])

Request metadata passed to the fit method.

set_output(*[, transform])

Set output container.

set_params(**params)

Set the parameters of this estimator.

set_predict_request(*[, result_format])

Request metadata passed to the predict method.

transform(X)

Transform the input data into the learned embedding.

fit(X, y=None, init=None, n_objects=None)[source]#

Computes the embedding.

Parameters:
  • X (ndarray | COO | spmatrix) – The training input samples, shape (n_samples, 3)

  • y (ndarray | None) – Ignored

  • init (ndarray | None) – Initial embedding for optimization

Returns:

self.

Return type:

FORTE

fit_transform(X, y=None, **fit_params)#

Fit to data, then transform it.

Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Input samples.

  • y (array-like of shape (n_samples,) or (n_samples, n_outputs), default=None) – Target values (None for unsupervised transformations).

  • **fit_params (dict) – Additional fit parameters.

Returns:

X_new – Transformed array.

Return type:

ndarray array of shape (n_samples, n_features_new)

get_metadata_routing()#

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:

routing – A MetadataRequest encapsulating routing information.

Return type:

MetadataRequest

get_params(deep=True)#

Get parameters for this estimator.

Parameters:

deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

params – Parameter names mapped to their values.

Return type:

dict

score(X, y=None)#

Triplet score on the estimated embedding.

Parameters:

X (ndarray | COO | spmatrix) – Triplet answers

Returns.

Fraction of correct triplets.

set_fit_request(*, init='$UNCHANGED$', n_objects='$UNCHANGED$')#

Request metadata passed to the fit method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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:
  • init (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for init parameter in fit.

  • n_objects (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for n_objects parameter in fit.

Returns:

self – The updated object.

Return type:

object

set_output(*, transform=None)#

Set output container.

See sphx_glr_auto_examples_miscellaneous_plot_set_output.py for an example on how to use the API.

Parameters:

transform ({"default", "pandas", "polars"}, default=None) –

Configure output of transform and fit_transform.

  • ”default”: Default output format of a transformer

  • ”pandas”: DataFrame output

  • ”polars”: Polars output

  • None: Transform configuration is unchanged

Added in version 1.4: “polars” option was added.

Returns:

self – Estimator instance.

Return type:

estimator instance

set_params(**params)#

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:

**params (dict) – Estimator parameters.

Returns:

self – Estimator instance.

Return type:

estimator instance

set_predict_request(*, result_format='$UNCHANGED$')#

Request metadata passed to the predict method.

Note that this method is only relevant if enable_metadata_routing=True (see sklearn.set_config()). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • 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:

result_format (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for result_format parameter in predict.

Returns:

self – The updated object.

Return type:

object

transform(X)#

Transform the input data into the learned embedding.

The input data can be none or an array with all or a subset of the triplets provided by .fit method. Actually, the input data is not used in this method, but is required for compatibility with the scikit-learn API.

Parameters:

X (ndarray | COO | spmatrix | None) – Triplet answers, identical to the input in the .fit method or None.

Returns:

The learned embedding.

Warns:

If X is not the same instance as in the .fit method.