added the example for memory_manager

This commit is contained in:
vasiloglou
2008-04-07 23:07:13 +00:00
parent 2cc9c75179
commit 321a06060d
9 changed files with 132 additions and 17 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
librule(name="l_bfgs",
headers=["l_bfgs.h", "l_bfgs_impl.h"],
headers=["l_bfgs.h", "l_bfgs_impl.h", "optimization_utils.h"],
tests=["test_l_bfgs.cc"],
deplibs=["fastlib:fastlib", "mlpack/allknn:allknn",
"contrib/nvasil/allkfn:allkfn", "contrib/nvasil/mvu:mvu"])
+13 -4
View File
@@ -88,11 +88,16 @@ void LBfgs<OptimizedFunction>::ComputeLocalOptimumBFGS() {
s_bfgs_[0].ptr(), y_bfgs_[0].ptr());
double old_feasibility_error = feasibility_error;
for(index_t i=0; i<mem_bfgs_; i++) {
// ComputeBFGS_(&step_, gradient_, i);
// if (step_==0) {
ComputeBFGS_(&step_, gradient_, i);
if (step_==0) {
NOTIFY("LBFGS failed to find a direction, continuing with gradient descent\n");
ComputeWolfeStep_(&step_, gradient_);
// }
}
// if (success=SUCCESS_FAIL) {
// NOTIFY("LBFGS failed to find a direction, continuing with gradient descent\n");
// ComputeWolfeStep_(&step_, gradient_);
// UpdateBFGS_();
// }
optimized_function_->ComputeGradient(coordinates_, &gradient_);
UpdateBFGS_();
previous_gradient_.CopyValues(gradient_);
@@ -163,6 +168,10 @@ void LBfgs<OptimizedFunction>::GetResults(Matrix *result) {
result->Copy(coordinates_);
}
template<typename OptimizedFunction>
void LBfgs<OptimizedFunction>::set_coordinates(Matrix &coordinates) {
coordinates_.CopyValues(coordinates);
}
template<typename OptimizedFunction>
Matrix *LBfgs<OptimizedFunction>::coordinates() {
return &coordinates_;
@@ -336,7 +345,7 @@ success_t LBfgs<OptimizedFunction>::UpdateBFGS_(index_t index_bfgs) {
temp_y_bfgs.ptr());
double y_norm=la::Dot(temp_y_bfgs.n_elements(),
temp_y_bfgs.ptr(), temp_y_bfgs.ptr());
if (temp_ro<1e-10*y_norm) {
if (temp_ro<1e-70*y_norm) {
fx_timer_stop(module_, "update_bfgs");
NONFATAL("Rejecting s, y they don't satisfy curvature condition");
return SUCCESS_FAIL;
@@ -33,6 +33,7 @@ class OptUtils {
la::AddTo(dimension, mean.ptr(), data->GetColumnPtr(i));
}
}
static void NonNegativeProjection(Matrix *data) {
double *ptr=data->ptr();
for(index_t i=0; i<(index_t)data->n_elements(); i++) {
@@ -41,6 +42,49 @@ class OptUtils {
}
}
}
static success_t SVDTransform(Matrix &input_mat, Matrix *output_mat,
index_t components_to_keep) {
Matrix temp;
temp.Copy(input_mat);
RemoveMean(&temp);
Vector s;
Matrix U, VT;
success_t success=la::SVDInit(temp, &s, &U, &VT);
if (success==SUCCESS_PASS) {
NOTIFY("PCA successful !! Printing requested %i eigenvalues...",
components_to_keep);
for(index_t i=0; i<components_to_keep; i++) {
printf("%lg ", s[i]);
}
printf("\n");
}
output_mat->Init(components_to_keep, input_mat.n_cols());
Matrix temp_reconstructed;
Matrix temp_S;
temp_S.Init(input_mat.n_rows(), input_mat.n_rows());
temp_S.SetAll(0.0);
for(index_t i=0; i<components_to_keep; i++) {
temp_S.set(i, i, s[i]);
}
Matrix temp_U;
la::MulInit(U, temp_S, &temp_U);
la::MulInit(temp_U, VT, &temp_reconstructed);
for(index_t i=0; i<output_mat->n_cols(); i++) {
memcpy(output_mat->GetColumnPtr(i),
temp_reconstructed.GetColumnPtr(i), components_to_keep*sizeof(double));
}
double error=0;
for(index_t i=0; i<temp_reconstructed.n_rows(); i++) {
for(index_t j=0; j<temp_reconstructed.n_cols(); j++) {
error+=math::Sqr(temp_reconstructed.get(i,j)-temp.get(i,j));
error+=math::Sqr(input_mat.get(i,j)-temp.get(i,j));
}
}
NOTIFY("Reconstruction error : %lg", error);
return success;
}
static void SparseProjection(Matrix *data, double sparse_factor) {
DEBUG_ASSERT(sparse_factor<=1);
DEBUG_ASSERT(sparse_factor>=0);
+17 -6
View File
@@ -37,14 +37,25 @@ int main(int argc, char *argv[]){
l_bfgs_node=fx_submodule(NULL, "opts/l_bfgs", "l_bfgs");
optfun_node=fx_submodule(NULL, "opts/optfun", "optfun");
bool pca_preprocess=fx_param_bool(NULL, "opts/pca", false);
bool pca_preprocess=fx_param_bool(NULL, "opts/pca_pre", false);
index_t pca_dimension=fx_param_int(NULL, "opts/pca_dim", 5);
bool pca_init=fx_param_bool(NULL, "opts/pca_init", false);
Matrix *initial_data;
if (pca_preprocess==true) {
NOTIFY("Preprocessing with pca");
initial_data = new Matrix();
Matrix temp;
OptUtils::SVDTransform(data_mat, &temp, pca_dimension);
data_mat.Destruct();
data_mat.Own(&temp);
}
if (pca_init==true) {
NOTIFY("Preprocessing with pca");
initial_data = new Matrix();
index_t new_dimension=fx_param_int(l_bfgs_node, "new_dimension", 2);
OptUtils::SVDTransform(data_mat, initial_data, new_dimension);
}
}
//we need to insert the number of points
char buffer[128];
@@ -58,7 +69,7 @@ int main(int argc, char *argv[]){
opt_function.Init(optfun_node, data_mat);
LBfgs<MaxVariance> engine;
engine.Init(&opt_function, l_bfgs_node);
if (pca_preprocess==true) {
if (pca_init==true) {
engine.set_coordinates(*initial_data);
}
engine.ComputeLocalOptimumBFGS();
@@ -75,7 +86,7 @@ int main(int argc, char *argv[]){
opt_function.Init(optfun_node, data_mat);
LBfgs<MaxVarianceInequalityOnFurthest> engine;
engine.Init(&opt_function, l_bfgs_node);
if (pca_preprocess==true) {
if (pca_init==true) {
engine.set_coordinates(*initial_data);
}
engine.ComputeLocalOptimumBFGS();
@@ -90,10 +101,10 @@ int main(int argc, char *argv[]){
if (optimized_function == "mvfu"){
MaxFurthestNeighbors opt_function;
opt_function.Init(optfun_node, data_mat);
opt_function.set_lagrange_mult(0.0);
//opt_function.set_lagrange_mult(0.0);
LBfgs<MaxFurthestNeighbors> engine;
engine.Init(&opt_function, l_bfgs_node);
if (pca_preprocess==true) {
if (pca_init==true) {
engine.set_coordinates(*initial_data);
}
engine.ComputeLocalOptimumBFGS();
@@ -0,0 +1,45 @@
/*
* =====================================================================================
*
* Filename: memory_manager_test.cc
*
* Description:
*
* Version: 1.0
* Created: 04/07/2008 05:51:29 PM EDT
* Revision: none
* Compiler: gcc
*
* Author: Nikolaos Vasiloglou (NV), nvasil@ieee.org
* Company: Georgia Tech Fastlab-ESP Lab
*
* =====================================================================================
*/
#include "memory_manager.h"
class MemoryManagerTest {
void Init(){
mmapmm::MemoryManager<false>::allocator_ = new mmapmm::MemoryManager<false>();
mmapmm::MemoryManager<false>::allocator_->set_capacity(65536*1000);
mmapmm::MemoryManager<false>::allocator_->Init();
}
void Destruct() {
mmapmm::MemoryManager<false>::allocator_->Destruct();
delete mmapmm::MemoryManager<false>::allocator_;
}
void Test1() {
double *ptr=mmap::MemoryManager<false>::allocator_->malloc<double>(100000);
}
void TestAll() {
Test1();
}
};
int main(int argc, char* argv[]) {
MemoryManagerTest test;
test.TestAll();
}
+5
View File
@@ -212,6 +212,11 @@ class SparseMatrix {
* Set Values
*/
void set(index_t r, index_t c, double v);
/**
* Returns the transpose of the matrix.
* if it is symmetric it just returns a copy of the same matrix
*/
void Transpose(SparseMatrix *transpose);
/**
* scales the matrix with a scalar
*/
+1 -1
View File
@@ -447,7 +447,7 @@ void SparseMatrix::Eig(SparseMatrix &pencil_matrix,
"it will fail\n");
}
index_t block_size=2*num_of_eigvalues;
index_t block_size=std::min(2*num_of_eigvalues, 10);
Teuchos::RCP<Epetra_MultiVector> ivec =
Teuchos::rcp(new Epetra_MultiVector(*map_, block_size));
// Fill it with random numbers
+2 -1
View File
@@ -197,9 +197,10 @@ void KernelPCA::ComputeLLE(index_t num_of_eigenvalues,
kernel_matrix_.SetDiagonal(1.0);
NONFATAL("Computing eigen values...\n");
SparseMatrix kernel_matrix1;
kernel_matrix_.ToFile("i_w.txt");
kernel_matrix_.EndLoading();
Sparsem::MultiplyT(kernel_matrix_, &kernel_matrix1);
kernel_matrix1.ToFile("lle_mat.txt");
kernel_matrix1.ToFile("i_w_i_w.txt");
kernel_matrix1.EndLoading();
kernel_matrix1.Eig(num_of_eigenvalues,
"SM",
@@ -41,7 +41,7 @@ class KernelPCATest {
NOTIFY("Estimated bandwidth %lg ...\n", bandwidth);
kernel_.set(bandwidth);
engine_->LoadAffinityMatrix();
engine_->ComputeGeneralKernelPCA(kernel_, 5,
engine_->ComputeGeneralKernelPCA(kernel_, 15,
&eigen_vectors,
&eigen_values);
@@ -89,9 +89,9 @@ class KernelPCATest {
NOTIFY("Test ComputeSpectralRegression passed...\n");
}
void TestAll() {
TestGeneralKernelPCA();
//TestLLE();
TestSpectralRegression();
//TestGeneralKernelPCA();
TestLLE();
//TestSpectralRegression();
}
private:
KernelPCA *engine_;