Added prune and reinitialize methods to bmf.cpp. Need to use them now.

parent ed8afd92
......@@ -13,6 +13,116 @@
#define FW 14
//============================================================================
idx_t prune_unused(binary_matrix& D, binary_matrix& A) {
idx_t nused = 0;
const idx_t K = D.get_rows();
const idx_t M = D.get_cols();
const idx_t N = A.get_rows();
char iused[K];
for (size_t k = 0; k < K; k++) {
if (A.col_weight(k) > 0) {
iused[k] = 1;
nused++;
} else {
iused[k] = 0;
}
}
if (get_verbosity() >= 1) {
std::cout << "Removing " << (K-nused) << " unused atoms." << std::endl;
}
//
// initial candidate is best candidate after pruning
//
binary_matrix Dp,Ap;
binary_matrix Dk,Ak;
Dk.allocate(1,M);
Ak.allocate(1,N);
Dp.allocate(nused,M);
Ap.allocate(N,nused);
for (size_t k = 0, r = 0; r < nused; k++) {
if (iused[k]) {
D.copy_row_to(k,Dk);
Dp.set_row(r,Dk);
A.copy_col_to(k,Ak);
Ap.set_col(r,Ak);
r++;
}
}
Ak.destroy();
Dk.destroy();
D.destroy();
A.destroy();
D = Dp;
A = Ap;
return nused;
}
//============================================================================
idx_t reinitialize_unused(const binary_matrix& E, const binary_matrix& H, binary_matrix& D, binary_matrix& A) {
idx_t nused = 0;
const idx_t K = D.get_rows();
const idx_t M = D.get_cols();
const idx_t N = A.get_rows();
char iused[K];
for (size_t k = 0; k < K; k++) {
if (A.col_weight(k) > 0) {
iused[k] = 1;
nused++;
} else {
iused[k] = 0;
}
}
if (nused == K) {
return K;
}
if (get_verbosity() >= 1) {
std::cout << "Removing " << (K-nused) << " unused atoms." << std::endl;
}
binary_matrix Dp,Ap;
binary_matrix Dk,Ak;
Dk.allocate(1,M);
Ak.allocate(1,N);
//
// create K-nused new atoms
//
Dp.allocate(K-nused,M);
Ap.allocate(N,K-nused);
initialize_dictionary(E,H,Dp,Ap);
//
// fill in unused atoms and corresponding coefficients
// with fresh ones from Dp and Ap
//
for (size_t k = 0, r = 0; k < K; k++) {
if (!iused[k]) {
Dp.copy_row_to(r,Dk);
D.set_row(k,Dk);
Ap.copy_col_to(r,Ak);
A.set_col(r,Ak);
r++;
}
}
//
// add new atoms in place of previous ones
//
Ak.destroy();
Dk.destroy();
Dp.destroy();
Ap.destroy();
return nused;
}
//============================================================================
idx_t learn_model_traditional(binary_matrix& X,
const binary_matrix& H,
binary_matrix& E,
......@@ -96,6 +206,7 @@ idx_t learn_model_traditional(binary_matrix& X,
return iter;
}
//============================================================================
idx_t learn_model_alter1(binary_matrix& X,
const binary_matrix& H,
......@@ -168,6 +279,7 @@ idx_t learn_model_alter1(binary_matrix& X,
return iter;
}
//============================================================================
idx_t learn_model_alter2(binary_matrix& X,
const binary_matrix& H,
......@@ -245,6 +357,7 @@ idx_t learn_model_alter2(binary_matrix& X,
return iter;
}
//============================================================================
idx_t learn_model_alter3(binary_matrix& X,
const binary_matrix& H,
......@@ -299,6 +412,8 @@ idx_t learn_model_alter3(binary_matrix& X,
return iter;
}
//============================================================================
#include "bmf_coding.h"
#include "time.h"
......@@ -308,6 +423,8 @@ extern bool force_mosaic;
extern bool force_residual_mosaic;
#include "bmf_util.h"
//============================================================================
idx_t learn_model_mdl_forward_selection(binary_matrix& X,
const binary_matrix& H,
binary_matrix& E,
......@@ -440,6 +557,8 @@ idx_t learn_model_mdl_forward_selection(binary_matrix& X,
return bestL;
}
//============================================================================
#if 0
//
// THIS IMPLEMENTATION IS FASTER BUT IS NOT WORKING WELL
......@@ -583,6 +702,7 @@ idx_t learn_model_mdl_backward_selection(binary_matrix& X,
#else
//
// SLOWER IMPLEMENTATION, BUT SAFE
//
......@@ -601,44 +721,18 @@ idx_t learn_model_mdl_backward_selection(binary_matrix& X,
binary_matrix bestA,candA,innerA;
binary_matrix innerE(N,M);
//
// first stage: get rid of unused atoms
// first get rid of unused atoms
//
prune_unused(D,A);
//
// backward selection
//
idx_t nused = 0;
char iused[K];
for (size_t k = 0; k < K; k++) {
if (A.col_weight(k) > 0) {
iused[k] = 1;
nused++;
} else {
iused[k] = 0;
}
}
if (get_verbosity() >= 1) {
std::cout << "Removing " << (K-nused) << " unused atoms." << std::endl;
}
codelength Lparts = model_codelength(E,D,A);
idx_t bestL = Lparts.X;
idx_t bestK = nused;
idx_t bestK = D.get_cols();
idx_t candL = bestL;
idx_t candK = bestK;
//
// initial candidate is best candidate after pruning
//
bestD.allocate(bestK,M);
bestA.allocate(N,bestK);
candD.allocate(bestK,M);
candA.allocate(N,bestK);
for (size_t k = 0, r = 0; r < bestK; k++) {
if (iused[k]) {
D.copy_row_to(k,Dk);
bestD.set_row(r,Dk);
candD.set_row(r,Dk);
A.copy_col_to(k,Ak);
bestA.set_row(r,Ak);
candA.set_row(r,Ak);
r++;
}
}
idx_t stuck = 0;
idx_t sumStuck = 0;
idx_t allStuck = 0;
......@@ -771,10 +865,10 @@ idx_t learn_model_mdl_backward_selection(binary_matrix& X,
}
return bestL;
}
#endif
//============================================================================
idx_t learn_model_mdl_full_search(binary_matrix& X,
const binary_matrix& H,
binary_matrix& E,
......@@ -830,3 +924,5 @@ idx_t learn_model_mdl_full_search(binary_matrix& X,
std::cout << "bestL=" << bestL << " bestk=" << bestk << std::endl;
return bestL;
}
//============================================================================
......@@ -25,6 +25,8 @@ idx_t W = 16;
idx_t stride = 0;
idx_t K = 512;
idx_t maxK = 512;
idx_t batch_size = 0;
bool image_mode = false;
bool force_mosaic = true;
bool force_residual_mosaic = true;
......@@ -94,6 +96,9 @@ void parse_args(int argc, char **argv) {
case 'm':
force_mosaic = (atoi(val) > 0);
break;
case 'B':
batch_size = (atoi(val) > 0);
break;
case 'M':
force_residual_mosaic = (atoi(val) > 0);
break;
......
#include "preview.h"
#include "bmf_util.h"
static char prefix[1024];
void set_prefix(const char* p) {
}
idx_t preview_none( const binary_matrix& X,
const binary_matrix& H,
const binary_matrix& E,
......@@ -20,7 +26,7 @@ idx_t preview_mosaic( const binary_matrix& X,
const idx_t inner_iter,
const idx_t outer_iter) {
char tmpfname[128];
snprintf(tmpfname,127,"%04lu_%04lu_iter.pbm",outer_iter,inner_iter);
snprintf(tmpfname,127,"%s%04lu_%04lu_iter.pbm",prefix,outer_iter,inner_iter);
render_mosaic(D,tmpfname);
return 0;
}
\ No newline at end of file
......@@ -8,6 +8,8 @@
#include "base_types.h"
#include "pbm.h"
void set_prefix(const char* prefix);
idx_t preview_none( const binary_matrix& X,
const binary_matrix& H,
const binary_matrix& E,
......
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