falkon.center_selection

CenterSelector

class falkon.center_selection.CenterSelector(random_gen)

Create the center selector with a random number generator

Parameters:

random_gen – A numpy random number generator object or a random seed.

abstract select(X, Y) Tensor | SparseTensor | Tuple[Tensor | SparseTensor, Tensor]

Abstract method for selecting M centers from the data.

Parameters:
  • X – The full input dataset (or a representation of it)

  • Y – The full input labels (this may be None)

Returns:

  • X_centers – If Y is None this is the only output: M centers selected from the data.

  • Y_centers – If Y is not empty, a set of label centers shall be returned as well.

abstract select_indices(X, Y) Tuple[Tensor | SparseTensor, Tensor] | Tuple[Tensor | SparseTensor, Tensor, Tensor]

Abstract method for selecting M centers from the data.

Parameters:
  • X – The full input dataset (or a representation of it)

  • Y – The full input labels (this may be None)

Returns:

  • X_centers – If Y is None this is the only output: M centers selected from the data.

  • Y_centers – If Y is not empty, a set of label centers shall be returned as well.

  • indices – The indices in X associated with the chosen centers. Subclasses may not implement this method, in which case a NotImplementedError will be raised.

UniformSelector

class falkon.center_selection.UniformSelector(random_gen, num_centers: int)

Bases: CenterSelector

Center selector which chooses from the full dataset uniformly at random (without replacement)

Parameters:
  • random_gen – A numpy random number generator object or a random seed.

  • num_centers – The number of centers which should be selected by this class.

select(X: Tensor | SparseTensor, Y: Tensor | None) Tensor | SparseTensor | Tuple[Tensor | SparseTensor, Tensor]

Select M observations from 2D tensor X, preserving device and memory order.

The selection strategy is uniformly at random. To control the randomness, pass an appropriate numpy random generator to this class’s constructor.

Parameters:
  • X – N x D tensor containing the whole input dataset. If N is lower than the number of centers this class is programmed to pick, a warning will be raised and only N centers will be returned.

  • Y – Optional N x T tensor containing the input targets. If Y is provided, the same observations selected for X will also be selected from Y. Certain models (such as falkon.models.LogisticFalkon) require centers to be extracted from both predictors and targets, while others (such as falkon.models.Falkon) only require the centers from the predictors.

Returns:

  • X_M – The randomly selected centers. They will be in a new, memory-contiguous tensor. All characteristics of the input tensor will be preserved.

  • (X_M, Y_M) – If Y was different than None then the entries of Y corresponding to the selected centers of X will also be returned.

select_indices(X: Tensor | SparseTensor, Y: Tensor | None) Tuple[Tensor | SparseTensor, Tensor] | Tuple[Tensor | SparseTensor, Tensor, Tensor]

Select M observations from 2D tensor X, preserving device and memory order.

The selection strategy is uniformly at random. To control the randomness, pass an appropriate numpy random generator to this class’s constructor.

This method behaves the same as select() but additionally returns a LongTensor containing the indices of the chosen points.

Parameters:
  • X – N x D tensor containing the whole input dataset. If N is lower than the number of centers this class is programmed to pick, a warning will be raised and only N centers will be returned.

  • Y – Optional N x T tensor containing the input targets. If Y is provided, the same observations selected for X will also be selected from Y. Certain models (such as falkon.models.LogisticFalkon) require centers to be extracted from both predictors and targets, while others (such as falkon.models.Falkon) only require the centers from the predictors.

Returns:

  • (X_M, indices) – The randomly selected centers and the corresponding indices. The centers will be stored in a new, memory-contiguous tensor and all characteristics of the input tensor will be preserved.

  • (X_M, Y_M, indices) – If parameter`Y` is not None then the entries of Y corresponding to the selected centers of X will also be returned.

FixedSelector

class falkon.center_selection.FixedSelector(centers: Tensor | SparseTensor, y_centers: Tensor | None = None, idx_centers: Tensor | None = None)

Bases: CenterSelector

Center selector which always picks the same centers.

The fixed centers are specified at class initialization time.

Parameters:
  • centers – Tensor of data-centers to be used.

  • y_centers – Optional tensor of label-centers to be used. If this is None, calling select() with a non-empty Y argument will throw an exception

  • idx_centers – Optional tensor containing the indices which correspond to the given centers. This tensor is used in the select_indices() method.

select(X: Tensor | SparseTensor, Y: Tensor | None) Tensor | SparseTensor | Tuple[Tensor | SparseTensor, Tensor]

Returns the fixed centers with which this instance was created

Parameters:
  • X – This parameter is ignored. The centers returned are the ones passed in the class’s constructor.

  • Y – Optional N x T tensor containing the input targets. The value of the parameter is ignored, but if it is not None, this method will return a tuple of X-centers and Y-centers.

Returns:

  • X_M – The fixed centers as given in the class constructor

  • (X_M, Y_M) – The X-centers and Y-centers as given in the class constructor. This tuple is only returned if Y is not None.

Raises:

RuntimeError – If parameter Y is not None but the y_centers tensor passed to the class constructor is None.

select_indices(X: Tensor | SparseTensor, Y: Tensor | None) Tuple[Tensor | SparseTensor, Tensor] | Tuple[Tensor | SparseTensor, Tensor, Tensor]

Returns the fixed centers, and their indices with which this instance was created

Parameters:
  • X – This parameter is ignored. The centers returned are the ones passed in the class’s constructor.

  • Y – Optional N x T tensor containing the input targets. The value of the parameter is ignored, but if it is not None, this method will return a tuple of X-centers Y-centers, and indices.

Returns:

  • (X_M, indices) – The fixed centers and the indices as given in the class constructor

  • (X_M, Y_M, indices) – The X-centers, Y-centers and indices as given in the class constructor. This tuple is only returned if Y is not None.

Raises:
  • RuntimeError – If the indices passed to the class constructor are None.

  • RuntimeError – If parameter Y is not None but the y_centers tensor passed to the class constructor is None.