MOD Dictionary Learning

This module implements the MOD (Method of Optimal Directions) dictionary learning algorithm, along with related functions.

MOD.py - Method of Optimal Directions (MOD) algorithm for dictionary learning.

This module implements the MOD algorithm, which is used for learning a dictionary for sparse coding. It includes utility functions to measure the distance between dictionaries and to learn a dictionary that can sparsely represent a set of signals using the OMP algorithm.

Example usage:

>>> from MOD import MOD, I_findDistanceBetweenDictionaries
>>> data = np.random.randn(100, 200)  # Random signals
>>> params = {'K': 50, 'num_iterations': 10, 'initialization_method': 'DataElements', 'L': 5}
>>> dictionary, coefficients = MOD(data, params)
>>> # Distance between original and updated dictionary
>>> ratio, total_distance = I_findDistanceBetweenDictionaries(dictionary, new_dictionary)
CompSensePack.dictionaries.MOD.I_findDistanceBetweenDictionaries(original, new)[source]

Calculates the distance between two dictionaries.

This function compares two dictionaries by calculating the number of columns that have a very low error between the original and the new dictionary.

Parameters

originalnumpy.ndarray

The original dictionary.

newnumpy.ndarray

The new dictionary.

Returns

ratiofloat

The ratio of dictionary elements with a small error (i.e., those that satisfy the condition errorOfElement < 0.01).

totalDistancesfloat

The sum of all error values between the original and new dictionary elements.

Notes

  • The function modifies the new dictionary to ensure that each column starts with a positive value (consistent with the original dictionary).

Example

>>> original_dict = np.random.randn(100, 50)
>>> new_dict = np.random.randn(100, 50)
>>> ratio, total_distance = I_findDistanceBetweenDictionaries(original_dict, new_dict)
CompSensePack.dictionaries.MOD.MOD(data, parameters)[source]

Method of Optimal Directions (MOD) algorithm for dictionary learning.

The MOD algorithm is a method for learning a dictionary for sparse representation of signals. It iteratively updates the dictionary to best represent the input data with sparse coefficients using the Orthogonal Matching Pursuit (OMP) algorithm.

Parameters

datanumpy.ndarray

An (n x N) matrix containing N signals, each of dimension n.

parametersdict
A dictionary containing the parameters for the MOD algorithm:
  • Kint

    The number of dictionary elements (columns) to train.

  • num_iterationsint

    The number of iterations to perform for dictionary learning.

  • initialization_methodstr

    Method to initialize the dictionary. Options are: * ‘DataElements’ - Initializes the dictionary using the first K data signals. * ‘GivenMatrix’ - Initializes the dictionary using a provided matrix

    (requires ‘initial_dictionary’ key).

  • initial_dictionarynumpy.ndarray, optional

    The initial dictionary matrix to use if ‘initialization_method’ is set to ‘GivenMatrix’. It should be of size (n x K).

  • Lint

    The number of non-zero coefficients to use in OMP for sparse representation of each signal.

Returns

dictionarynumpy.ndarray

The trained dictionary of size (n x K), where each column is a dictionary element.

coef_matrixnumpy.ndarray

The coefficient matrix of size (K x N), representing the sparse representation of the input data using the trained dictionary.

Notes

  • The dictionary is initialized either from the data signals or from a provided matrix.

  • The OMP algorithm is used to solve the sparse coding step at each iteration.

  • The algorithm iteratively refines the dictionary by solving a least squares problem.

  • The columns of the dictionary are normalized to unit norm and positive first elements.

Example

>>> data = np.random.randn(100, 200)
>>> params = {'K': 50, 'num_iterations': 10, 'initialization_method': 'DataElements', 'L': 5}
>>> dictionary, coefficients = MOD(data, params)