Fixed new coding methods.

parent f7da14ae
/**
* \file bmf_types.h
* \brief basic (scalar) types used in the BMF package.
*/
#ifndef BASE_TYPES_H
#define BASE_TYPES_H
......
......@@ -67,7 +67,7 @@ void initialize_dictionary_coverage(const binary_matrix& E,
counting_sort(scores,N);
for (idx_t i = 0; i < K; i++) {
std::cout << i << ":" << scores[N-i-1].first << " " << scores[N-i-1].second << std::endl;
//std::cout << i << ":" << scores[N-i-1].first << " " << scores[N-i-1].second << std::endl;
E.copy_row_to(scores[N-i-1].second, rowi);
D.set_row(i,rowi);
}
......@@ -124,7 +124,7 @@ void initialize_dictionary_normalized_coverage(const binary_matrix& E,
counting_sort(scores,N);
for (idx_t i = 0; i < K; i++) {
std::cout << i << ":" << scores[N-i-1].first << " " << scores[N-i-1].second << std::endl;
//std::cout << i << ":" << scores[N-i-1].first << " " << scores[N-i-1].second << std::endl;
E.copy_row_to(scores[N-i-1].second, rowi);
D.set_row(i,rowi);
}
......
......@@ -721,20 +721,8 @@ idx_t coefficients_update_missing_data_omp(binary_matrix& E,
}
/**
* Given a current error E, dictionary D and coefficients A, update E and A.
* This algorithm seeks to minimize the Least Hamming Error (LH)
* by repeatedly adding a pair (d_k,a_k) each time to the approximation so
* that the error is minimal w.r.t all other possible atoms d_k' != d_k.
* (note that, given an atom d_k, the choice of a_k is deterministic, so there
* are only k choices at each iteration).
*
* E = AD is an n x m matrix
* D is a p x m matrix, where each row is an atom of dimension m
* A is a n x p matrix, where each row contains the coefficients for representing the corresponding row of X=AD+E
* max_a_weight is the maximum allowed number of ones in any row of A
* max_e_weight is the maximum allowed number of ones in any row of E
*/
//==========================================================================
idx_t coefficients_update_greedy_global(binary_matrix& E,
const binary_matrix& H,
const binary_matrix& D,
......@@ -753,28 +741,30 @@ idx_t coefficients_update_greedy_global(binary_matrix& E,
unsigned char* used = new unsigned char[K];
memset(used,0,K*sizeof(unsigned char));
idx_t unused = K;
idx_t prev_score = M*N+1;
idx_t best_score = E.weight();
while (unused) {
idx_t best_score = prev_score;
std::cout << "unused=" << unused << "prev score=" << prev_score << std::endl;
idx_t prev_score = best_score;
idx_t best_k = K+1;
for (idx_t k = 0; k < K; k++) {
if (used[k]) continue;
idx_t score = 0;
D.copy_row_to(k,Dk);
if (!Dk.weight()) {
continue;
}
idx_t score = 0;
Ak.clear();
for (idx_t i = 0; i < N; i++) {
E.copy_row_to(i,Ei);
idx_t w0 = Ei.weight();
if (!w0) {
continue;
}
add(Dk,Ei,Ei);
idx_t w1 = Ei.weight();
if (w0 > w1) {
Ak.set(i);
score += w1;
} else {
Ak.clear(i);
score += w0;
}
}
......@@ -782,10 +772,10 @@ idx_t coefficients_update_greedy_global(binary_matrix& E,
best_score = score; // new best score
best_k = k; // this is so far the best cand.
Ak.copy_to(Abest); // save its companion A^k
//std::cout << "|Abest|=" << Abest.weight() << "/" << Abest.get_cols() << std::endl;
}
}
if (best_k == K+1) {
std::cout << "No further improvement." << std::endl;
break;
}
// we've got our winner
......@@ -793,9 +783,9 @@ idx_t coefficients_update_greedy_global(binary_matrix& E,
A.set_col(best_k,Abest);
// update error matrix
D.copy_col_to(best_k, Dk);
std::cout << "|Dk|=" << Dk.weight() << " |Ak|="<< Abest.weight() << "|E|=" << E.weight() << " best_k=" << best_k << " score=" << best_score << " unused=" << unused << std::endl;
mul(Abest,true,Dk,false,E);
std::cout << "|E|=" << E.weight() << " best_k=" << best_k << " score=" << best_score << " unused=" << unused << std::endl;
// std::cout << "|Dk|=" << Dk.weight() << " |Ak|="<< Abest.weight() << " prev_score=" << prev_score << " best_k=" << best_k << " best_score=" << best_score << " unused=" << unused;
// std::cout << "|E|=" << E.weight() << std::endl;
used[best_k] = 1;
unused--;
}
......
/**
* \file bmf_update_coefficients.h
* \brief Interface to functions whose role is to update the coefficients of a BMF representatio.
*/
#ifndef ENCODE_SAMPLES_H
#define ENCODE_SAMPLES_H
#include "binmat.h"
/**
* Given a current error E, dictionary D and coefficients A, update coefficients and error,
* \brief Given a current error E, dictionary D and coefficients A, update coefficients and error,
* so that the total weight of the error E is reduced. The algorithm updates one row of A at a time.
*
* E = AD is an n x m matrix
* D is a p x m matrix, where each row is an atom of dimension m
* A is a n x p matrix, where each row contains the coefficients for representing the corresponding row of X=AD+E
* max_a_weight is the maximum allowed number of ones in any row of A
* max_e_weight is the maximum allowed number of ones in any row of E
* \param E = AD is an n x m matrix
* \param H = n x m mask matrix where a one indicates that a given sample is missing
* \param D is a p x m matrix, where each row is an atom of dimension m
* \param A is a n x p matrix, where each row contains the coefficients for representing the corresponding row of X=AD+E
* \param max_a_weight is the maximum allowed number of ones in any row of A
* \param max_e_weight is the maximum allowed number of ones in any row of E
*/
idx_t coefficients_update_bmp(binary_matrix& E,
const binary_matrix& H,
......@@ -28,14 +33,15 @@ idx_t coefficients_update_bmp_omp(binary_matrix& E,
const idx_t max_e_weight);
/**
* Given a current error E, dictionary D and coefficients A, update coefficients and error,
* \brief Given a current error E, dictionary D and coefficients A, update coefficients and error,
* so that the total weight of the error E is reduced. The algorithm updates one row of A at a time.
*
* E = AD is an n x m matrix
* D is a p x m matrix, where each row is an atom of dimension m
* A is a n x p matrix, where each row contains the coefficients for representing the corresponding row of X=AD+E
* max_a_weight is the maximum allowed number of ones in any row of A
* max_e_weight is the maximum allowed number of ones in any row of E
* \param E = AD is an n x m matrix
* \param H = n x m mask matrix where a one indicates that a given sample is missing
* \param D is a p x m matrix, where each row is an atom of dimension m
* \param A is a n x p matrix, where each row contains the coefficients for representing the corresponding row of X=AD+E
* \param max_a_weight is the maximum allowed number of ones in any row of A
* \param max_e_weight is the maximum allowed number of ones in any row of E
*/
idx_t coefficients_update_basic(binary_matrix& E,
const binary_matrix& H,
......@@ -45,14 +51,15 @@ idx_t coefficients_update_basic(binary_matrix& E,
const idx_t max_e_weight);
/**
* Given a current error E, dictionary D and coefficients A, update coefficients and error,
* \brief Given a current error E, dictionary D and coefficients A, update coefficients and error,
* so that the total weight of the error E is reduced. The algorithm updates one row of A at a time.
*
* E = AD is an n x m matrix
* D is a p x m matrix, where each row is an atom of dimension m
* A is a n x p matrix, where each row contains the coefficients for representing the corresponding row of X=AD+E
* max_a_weight is the maximum allowed number of ones in any row of A
* max_e_weight is the maximum allowed number of ones in any row of E
* \param E = AD is an n x m matrix
* \param H = n x m mask matrix where a one indicates that a given sample is missing
* \param D is a p x m matrix, where each row is an atom of dimension m
* \param A is a n x p matrix, where each row contains the coefficients for representing the corresponding row of X=AD+E
* \param max_a_weight is the maximum allowed number of ones in any row of A
* \param max_e_weight is the maximum allowed number of ones in any row of E
*/
idx_t coefficients_update_omp(binary_matrix& E,
const binary_matrix& H,
......@@ -62,7 +69,7 @@ idx_t coefficients_update_omp(binary_matrix& E,
const idx_t max_e_weight);
/**
* Given a current error E, dictionary D and coefficients A, update coefficients and error,
* \brief Given a current error E, dictionary D and coefficients A, update coefficients and error,
* so that the total weight of the error E is reduced.
*
* Instead of working one sample at a time, this algorithm operates on the whole signal,
......@@ -70,11 +77,13 @@ idx_t coefficients_update_omp(binary_matrix& E,
* E XOR A^k D_k
* This implies an exhaustive, greedy search on D, but for each D_k, the corresponding A^k
* has a closed form, so the algorithm requires O(k^2) iterations.
* E = AD is an n x m matrix
* D is a p x m matrix, where each row is an atom of dimension m
* A is a n x p matrix, where each row contains the coefficients for representing the corresponding row of X=AD+E
* max_a_weight is the maximum allowed number of ones in any row of A
* max_e_weight is the maximum allowed number of ones in any row of E
*
* \param E = n x m data matrix to be approximated
* \param H = n x m mask matrix where a one indicates that a given sample is missing
* \param D is a p x m matrix, where each row is an atom of dimension m
* \param A is a n x p matrix, where each row contains the coefficients for representing the corresponding row of X=AD+E
* \param max_a_weight is the maximum allowed number of ones in any row of A
* \param max_e_weight is the maximum allowed number of ones in any row of E
*/
idx_t coefficients_update_greedy_global(binary_matrix& E,
const binary_matrix& H,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment