mojolearn.MiniBatchDictionaryLearning#

class mojolearn.MiniBatchDictionaryLearning(n_components=None, *, alpha=1, max_iter=1000, fit_algorithm='lars', n_jobs=None, batch_size=256, shuffle=True, dict_init=None, transform_algorithm='omp', transform_n_nonzero_coefs=None, transform_alpha=None, verbose=False, split_sign=False, random_state=None, positive_code=False, positive_dict=False, transform_max_iter=1000, callback=None, tol=0.001, max_no_improvement=10, numeric_mode=None)#

Learn a sparse-coding dictionary from mini-batches.

Follows scikit-learn’s MiniBatchDictionaryLearning. The starting dictionary comes from an exact SVD (scikit-learn uses a randomized one) and the shuffle uses mojolearn’s own generator, so results differ slightly from scikit-learn’s for the same seed.

Parameters:
  • n_components (int or None, default None) – Number of atoms; None uses n_features.

  • alpha (float, default 1) – Sparsity penalty used while fitting.

  • max_iter (int, default 1000) – Maximum passes over the data.

  • fit_algorithm ({“lars”, “cd”}, default “lars”) – Lasso solver name used while fitting.

  • n_jobs (ignored)

  • batch_size (int, default 256) – Rows per mini-batch (capped at n_samples).

  • shuffle (bool, default True) – Shuffle the rows once before fitting.

  • dict_init (array-like of shape (n_components, n_features) or None, default None) – Starting dictionary.

  • transform_algorithm ({“omp”, “lasso_lars”, “lasso_cd”, “lars”, “threshold”}, default “omp”) – Method used by transform; see sparse_encode().

  • transform_n_nonzero_coefs (int or None, default None) – Nonzero coefficients for "omp" and "lars" in transform.

  • transform_alpha (float or None, default None) – Penalty or threshold used by transform; None uses alpha.

  • verbose (ignored)

  • split_sign (bool, default False) – Return the positive and negative parts of the code as separate columns.

  • random_state (int or None, default None) – Seed for the shuffle; None means 0.

  • positive_code (bool, default False) – Force the code to be non-negative.

  • positive_dict (bool, default False) – Force the dictionary to be non-negative.

  • transform_max_iter (int, default 1000) – Maximum iterations of the Lasso methods during fit and transform.

  • callback (None) – Must be None; a callable raises NotImplementedError.

  • tol (float, default 1e-3) – Stop when the dictionary changes less than this between steps.

  • max_no_improvement (int or None, default 10) – Stop after this many mini-batches without improvement of the smoothed cost; None disables this test.

  • numeric_mode ({“identical”, “fast”} or None, default None) – Numeric mode for this estimator. None uses the package default ("identical" unless changed). In identical mode, mojolearn returns the same bits on Apple, NVIDIA and AMD GPUs. "fast" is available on Apple GPUs only.

Variables:
  • components_ (ndarray of shape (n_components, n_features)) – The dictionary atoms.

  • n_iter_ (int) – Passes over the data.

  • n_steps_ (int) – Mini-batches processed.

  • n_features_in_ (int)

Examples

>>> import numpy as np, mojolearn
>>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32)
>>> dl = mojolearn.MiniBatchDictionaryLearning(n_components=5, max_iter=5, random_state=0).fit(X)
>>> code = dl.transform(X)

Methods

fit(X[, y])

Fit the dictionary to X.

fit_transform(X[, y])

Fit the dictionary, then return transform(X).

get_params([deep])

Return this estimator's constructor parameters.

inverse_transform(X)

Rebuild data from a code: code @ components_.

set_params(**params)

Set constructor parameters by keyword.

transform(X)

Encode X as a sparse combination of the dictionary atoms.

fit(X, y=None)#

Fit the dictionary to X.

Parameters:
  • X (array-like of shape (n_samples, n_features))

  • y (ignored)

Returns:

self

fit_transform(X, y=None)#

Fit the dictionary, then return transform(X).

Parameters:
  • X (array-like of shape (n_samples, n_features))

  • y (ignored)

Returns:

ndarray of shape (n_samples, n_components), float32

get_params(deep=True)#

Return this estimator’s constructor parameters.

Parameters:

deep (bool, default True) – Accepted for scikit-learn compatibility; it has no effect.

Returns:

dict – Parameter name to current value.

inverse_transform(X)#

Rebuild data from a code: code @ components_.

Parameters:

X (array-like of shape (n_samples, n_components)) – A code, as returned by transform (with 2 * n_components columns when split_sign=True).

Returns:

ndarray of shape (n_samples, n_features), float32

set_params(**params)#

Set constructor parameters by keyword.

Raises:

ValueError – If a keyword is not a parameter of this estimator.

Returns:

self

transform(X)#

Encode X as a sparse combination of the dictionary atoms.

Uses transform_algorithm and its settings.

Parameters:

X (array-like of shape (n_samples, n_features))

Returns:

ndarray of shape (n_samples, n_components), float32 – Twice as many columns when split_sign=True.