Skip to main content

reppi

GitHub Docs

A Python library for representation learning — sparse coding and dictionary learning algorithms implemented close to their original formulations.

Installation

pip install reppi

Algorithms

Algorithm Class Reference
Orthogonal Matching Pursuit OMP Elad et al., 2008
K-SVD KSVD Aharon et al., 2006
Label Consistent K-SVD LCKSVD Jiang et al., 2011
Frozen Dictionary Learning IncrementalFrozenDictionary Carroll et al., 2017

Convention

reppi follows the column-major convention common in the sparse representation literature: signals are columns, so data matrices are shaped (n_features, n_samples). This matches the MATLAB toolboxes the implementations are based on. If your data is in sklearn's (n_samples, n_features) layout, transpose before passing it in.

Quick Start

Installaion

pip install reppi

Sparse Coding with OMP

from reppi import OMP
import numpy as np

# D: (n_features, n_atoms), unit-norm columns
# X: (n_features, n_samples)
omp = OMP(n_nonzero_coefs=10)
Gamma = omp.encode(X, D)  # (n_atoms, n_samples)

Dictionary Learning with K-SVD

from reppi import KSVD

ksvd = KSVD(
    n_components=128,     # number of atoms
    n_nonzero_coefs=10,   # sparsity level T
    n_iter=20,
    verbose=True,
)
ksvd.fit(X_train)         # X_train: (n_features, n_samples)

D = ksvd.D_               # learned dictionary (n_features, n_components)
Gamma = ksvd.transform(X) # sparse codes (n_components, n_samples)

Discriminative Dictionary Learning with LC-KSVD

LC-KSVD jointly learns a dictionary and a linear classifier from labelled data. Labels are passed as a one-hot matrix H of shape (n_classes, n_samples).

LC-KSVD1 — reconstruction + label-consistency:

from reppi import LCKSVD

model = LCKSVD(
    n_components=570,
    n_nonzero_coefs=30,
    alpha=4.0,            # weight for label-consistency term
    variant="lcksvd1",
    n_iter=50,
    n_iter_init=20,       # K-SVD iterations for initialisation
    verbose=True,
)
model.fit(X_train, H_train)
Gamma = model.transform(X_test)  # (n_components, n_samples)

LC-KSVD2 — reconstruction + label-consistency + classification error:

model = LCKSVD(
    n_components=570,
    n_nonzero_coefs=30,
    alpha=4.0,
    beta=2.0,             # weight for classifier term
    variant="lcksvd2",
    n_iter=50,
    n_iter_init=20,
    verbose=True,
)
model.fit(X_train, H_train)

predictions = model.predict(X_test)        # integer class indices
accuracy    = model.score(X_test, H_test)  # float in [0, 1]

Frozen Dictionary Learning

reppi also supports incremental frozen dictionary learning for scenarios where a base dictionary is learned first and then extended over time with new class-specific residual dictionaries.

The learned dictionary grows as:

D = [ D_n | D_a_1 | D_a_2 | ... | D_a_k ]

Where:

D_n is the frozen base/background dictionary D_a_i is the residual dictionary learned for class i

Previously learned atoms remain frozen when new classes are added.

Single Frozen Residual Step

Use FrozenDictionaryLearner for a single residual-learning stage:

from reppi.dictionary.frozen import FrozenDictionaryLearner
from reppi import LCKSVD

frozen = FrozenDictionaryLearner(
    D_frozen=D_base,
    learner_class=LCKSVD,
    learner_kwargs=dict(
        n_components=32,
        n_nonzero_coefs=10,
        variant="lcksvd2",
    ),
    n_nonzero_coefs=10,
)

frozen.fit(X_class, H_class)

D_combined = frozen.D_combined_
Gamma = frozen.transform(X_test)
predictions = frozen.predict(X_test)

Incremental Frozen Dictionary Pipeline

Use IncrementalFrozenDictionary for full sequential learning:

from reppi.dictionary.frozen import IncrementalFrozenDictionary
from reppi import LCKSVD

inc = IncrementalFrozenDictionary(
    base_learner_class=LCKSVD,
    base_learner_kwargs=dict(
        n_components=128,
        n_nonzero_coefs=10,
        variant="lcksvd2",
    ),
    residual_learner_class=LCKSVD,
    residual_learner_kwargs=dict(
        n_components=32,
        n_nonzero_coefs=10,
        variant="lcksvd2",
    ),
    n_nonzero_coefs=10,
)

References

  • M. Aharon, M. Elad, A. Bruckstein. "The K-SVD: An Algorithm for Designing Overcomplete Dictionaries for Sparse Representation". IEEE Trans. Signal Processing, 54(11), 2006.
  • M. Elad, R. Rubinstein, M. Zibulevsky. "Efficient Implementation of the K-SVD Algorithm using Batch Orthogonal Matching Pursuit". Technion Technical Report, 2008.
  • Z. Jiang, Z. Lin, L. Davis. "Learning A Discriminative Dictionary for Sparse Coding via Label Consistent K-SVD". CVPR, 2011.
  • B. T. Carroll, B. M. Whitaker, W. Daley, D. V. Anderson. "Outlier Learning via Augmented Frozen Dictionaries". IEEE/ACM Transactions on Audio, Speech, and Language Processing, 2017.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

reppi-0.1.14.tar.gz (30.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

reppi-0.1.14-py3-none-any.whl (38.4 kB view details)

Uploaded Python 3

File details

Details for the file reppi-0.1.14.tar.gz.

File metadata

  • Download URL: reppi-0.1.14.tar.gz
  • Upload date:
  • Size: 30.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for reppi-0.1.14.tar.gz
Algorithm Hash digest
SHA256 5b89f2edaac370f0f21384727b9a4fd9eaba8637ffafaf23f4f8938895e8b3e5
MD5 3ad5406019c9190ee664e4d9eeca52be
BLAKE2b-256 a366abc2cf988ab380897f8881aee376fc60f9373573b10cf65225b82a330576

See more details on using hashes here.

Provenance

The following attestation bundles were made for reppi-0.1.14.tar.gz:

Publisher: publish.yml on ckekula/reppi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file reppi-0.1.14-py3-none-any.whl.

File metadata

  • Download URL: reppi-0.1.14-py3-none-any.whl
  • Upload date:
  • Size: 38.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for reppi-0.1.14-py3-none-any.whl
Algorithm Hash digest
SHA256 20778ff0e744ea4b0d9010100d03a11b1734ff02f211368545183c942f8a235f
MD5 ed69f2230bd1a467a13da60282901b96
BLAKE2b-256 b559b836e1e20cddddb90cd911e168fb160c9d0a158a5d524f6e11e478e8f4bf

See more details on using hashes here.

Provenance

The following attestation bundles were made for reppi-0.1.14-py3-none-any.whl:

Publisher: publish.yml on ckekula/reppi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

1.0.0

2 files

0.1.59

2 files

0.1.58

2 files

0.1.56

2 files

0.1.55

2 files

0.1.53

2 files

0.1.52

2 files

0.1.51

2 files

0.1.50

2 files

0.1.45

2 files

0.1.43

2 files

0.1.42

2 files

0.1.41

2 files

0.1.40

2 files

0.1.39

2 files

0.1.38

2 files

0.1.37

2 files

0.1.36

2 files

0.1.35

2 files

0.1.34

2 files

0.1.33

2 files

0.1.32

2 files

0.1.31

2 files

0.1.29

2 files

0.1.28

2 files

0.1.27

2 files

0.1.26

2 files

0.1.24

2 files

0.1.23

2 files

0.1.21

2 files

0.1.20

2 files

0.1.18

2 files

0.1.16

2 files

This release

0.1.14 This release

2 files

0.1.13

2 files

0.1.11

2 files

0.1.10

2 files

0.1.9

2 files

0.1.7

2 files

0.1.6

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page