it is running
This commit is contained in:
@@ -115,7 +115,7 @@ class AllkNN {
|
||||
/////////////////////////////// Constructors /////////////////////////////////////////////
|
||||
|
||||
// Add this at the beginning of a class to prevent accidentally calling the copy constructor
|
||||
FORBID_ACCIDENTAL_COPIES(AllNN);
|
||||
FORBID_ACCIDENTAL_COPIES(AllkNN);
|
||||
|
||||
|
||||
public:
|
||||
@@ -171,7 +171,7 @@ class AllkNN {
|
||||
|
||||
// Used to find the query node's new upper bound
|
||||
double query_max_neighbor_distance = -1.0;
|
||||
ArrayList<std::pair<dstance, index_t> > neighbors;
|
||||
ArrayList<std::pair<double, index_t> > neighbors;
|
||||
neighbors.Init(knns_, knns_+leaf_size_);
|
||||
// node->begin() is the index of the first point in the node,
|
||||
// node->end is one past the last index
|
||||
@@ -184,8 +184,8 @@ class AllkNN {
|
||||
|
||||
index_t ind = query_index*knns_;
|
||||
for(index_t i=0; i<knns_; i++) {
|
||||
neighbors[i]=std::make_pair(neighbor_distances[ind+i],
|
||||
neighbor_indices[ind+i]);
|
||||
neighbors[i]=std::make_pair(neighbor_distances_[ind+i],
|
||||
neighbor_indices_[ind+i]);
|
||||
}
|
||||
// We'll do the same for the references
|
||||
for (index_t reference_index = reference_node->begin();
|
||||
@@ -206,13 +206,13 @@ class AllkNN {
|
||||
if (neighbors.size()>knns_) {
|
||||
std::sort(neighbors.begin(), neighbors.end());
|
||||
for(index_t i=0; i<knns_; i++) {
|
||||
neighbor_distances[ind+i] = neighbors[i].first;
|
||||
neighbor_indices[ind+i] = neighbors[i].second;
|
||||
neighbor_distances_[ind+i] = neighbors[i].first;
|
||||
neighbor_indices_[ind+i] = neighbors[i].second;
|
||||
}
|
||||
neighbors.Resize(knns_);
|
||||
// We need to find the upper bound distance for this query node
|
||||
if (neighbor_distances_[ind+knns-1] > query_max_neighbor_distance) {
|
||||
query_max_neighbor_distance = neighbor_distances_[nd+knns_-1];
|
||||
if (neighbor_distances_[ind+knns_-1] > query_max_neighbor_distance) {
|
||||
query_max_neighbor_distance = neighbor_distances_[ind+knns_-1];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -344,7 +344,7 @@ class AllkNN {
|
||||
* local copies of the data.
|
||||
*/
|
||||
void Init(const Matrix& queries_in, const Matrix& references_in,
|
||||
index_t leaf_size_, ndex_t knns) {
|
||||
index_t leaf_size, index_t knns) {
|
||||
|
||||
|
||||
// track the number of prunes
|
||||
@@ -422,14 +422,16 @@ class AllkNN {
|
||||
MinNodeDistSq_(query_tree_, reference_tree_));
|
||||
|
||||
// We need to initialize the results list before filling it
|
||||
results->Init(neighbor_indices_.size());
|
||||
resulting_neighbors->Init(neighbor_indices_.size());
|
||||
// We need to map the indices back from how they have
|
||||
// been permuted
|
||||
for (index_t i = 0; i < neighbor_indices_.size(); i++) {
|
||||
(*results)[old_from_new_queries_[i]] =
|
||||
(*resulting_neighbors)[
|
||||
old_from_new_queries_[i/knns_]*knns_+ i%knns_] =
|
||||
old_from_new_references_[neighbor_indices_[i]];
|
||||
}
|
||||
distances.Copy(neighbor_distances_.ptr(), neighbor_distances.len());
|
||||
distances->Copy(neighbor_distances_.ptr(),
|
||||
neighbor_distances_.length());
|
||||
|
||||
} // ComputeNeighbors
|
||||
|
||||
@@ -444,10 +446,11 @@ class AllkNN {
|
||||
// The same code as above
|
||||
results->Init(neighbor_indices_.size());
|
||||
for (index_t i = 0; i < neighbor_indices_.size(); i++) {
|
||||
(*results)[old_from_new_queries_[i]] =
|
||||
(*results)[old_from_new_queries_[i/knns_]] =
|
||||
old_from_new_references_[neighbor_indices_[i]];
|
||||
}
|
||||
distances.Copy(neighbor_distances_.ptr(), neighbor_distances.len());
|
||||
distances->Copy(neighbor_distances_.ptr(),
|
||||
neighbor_distances_.length());
|
||||
} // ComputeNaive
|
||||
|
||||
}; //class AllNN
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
binrule(name="kptest",
|
||||
headers=["kernel_pca.h", "kernel_pca_impl.h"],
|
||||
headers=["kernel_pca.h", "kernel_pca_impl.h", "allknn.h"],
|
||||
sources=["kernel_pca_test.cc"],
|
||||
cflags=" -fexceptions",
|
||||
deplibs=["sparse:sparse", "fastlib:fastlib", "la:la"]
|
||||
|
||||
@@ -25,17 +25,14 @@
|
||||
#include <unistd.h>
|
||||
#include "fastlib/fastlib.h"
|
||||
#include "la/matrix.h"
|
||||
#include "u/nvasil/tree/binary_kd_tree_mmapmm.h"
|
||||
#include "u/nvasil/dataset/binary_dataset.h"
|
||||
#include "sparse/sparse_matrix.h"
|
||||
#include "allknn.h"
|
||||
|
||||
class KernelPCATest;
|
||||
|
||||
class KernelPCA {
|
||||
public:
|
||||
friend class KernelPCATest;
|
||||
typedef BinaryKdTreeMMAPMMKnnNode_t Tree_t;
|
||||
typedef Tree_t::Precision_t Precision_t;
|
||||
class GaussianKernel {
|
||||
public:
|
||||
void set(double bandwidth) {
|
||||
@@ -50,10 +47,10 @@ class KernelPCA {
|
||||
~KernelPCA() {
|
||||
Destruct();
|
||||
}
|
||||
void Init(std::string data_file,
|
||||
std::string index_file);
|
||||
void Init(std::string data_file, index_t knns,
|
||||
index_t leaf_size);
|
||||
void Destruct();
|
||||
void ComputeNeighborhoods(index_t knn);
|
||||
void ComputeNeighborhoods();
|
||||
void LoadAffinityMatrix();
|
||||
void EstimateBandwidth(double *bandwidth);
|
||||
static void SaveToTextFile(std::string file,
|
||||
@@ -69,8 +66,7 @@ class KernelPCA {
|
||||
Matrix *eigen_vectors,
|
||||
std::vector<double> *eigen_values);
|
||||
void ComputeIsomap(index_t num_of_eigenvalues);
|
||||
void ComputeLLE(index_t knns,
|
||||
index_t num_of_eigenvalues,
|
||||
void ComputeLLE(index_t num_of_eigenvalues,
|
||||
Matrix *eigen_vectors,
|
||||
std::vector<double> *eigen_values);
|
||||
template<typename KERNEL>
|
||||
@@ -79,10 +75,11 @@ class KernelPCA {
|
||||
void ComputeSpectralRegression(std::string label_file);
|
||||
|
||||
private:
|
||||
Tree_t tree_;
|
||||
AllkNN allknn_;
|
||||
index_t knns_;
|
||||
Matrix data_;
|
||||
SparseMatrix kernel_matrix_;
|
||||
SparseMatrix affinity_matrix_;
|
||||
BinaryDataset<Precision_t> data_;
|
||||
index_t dimension_;
|
||||
};
|
||||
|
||||
|
||||
@@ -17,43 +17,34 @@
|
||||
*/
|
||||
|
||||
|
||||
void KernelPCA::Init(std::string data_file,
|
||||
std::string index_file) {
|
||||
if (index_file.empty()) {
|
||||
data_.Init(data_file);
|
||||
} else {
|
||||
data_.Init(data_file, index_file);
|
||||
}
|
||||
dimension_=data_.get_dimension();
|
||||
tree_.Init(&data_);
|
||||
mmapmm::MemoryManager<false>::allocator_ =
|
||||
new mmapmm::MemoryManager<false>();
|
||||
mmapmm::MemoryManager<false>::allocator_->Init();
|
||||
void KernelPCA::Init(std::string data_file, index_t knns,
|
||||
index_t leaf_size) {
|
||||
data::Load(data_file.c_str(), &data_);
|
||||
knns_ = knns;
|
||||
allknn_.Init(data_, data_, leaf_size, knns);
|
||||
}
|
||||
|
||||
void KernelPCA::Destruct() {
|
||||
tree_.Destruct();
|
||||
data_.Destruct();
|
||||
unlink("allnn.txt");
|
||||
if (mmapmm::MemoryManager<false>::allocator_ != NULL) {
|
||||
delete mmapmm::MemoryManager<false>::allocator_;
|
||||
mmapmm::MemoryManager<false>::allocator_=NULL;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void KernelPCA::ComputeNeighborhoods(index_t knns) {
|
||||
void KernelPCA::ComputeNeighborhoods() {
|
||||
NONFATAL("Building tree...\n");
|
||||
fflush(stdout);
|
||||
tree_.set_knns(knns);
|
||||
tree_.BuildDepthFirst();
|
||||
NONFATAL("Memory usage: %llu\n",
|
||||
(unsigned long long)Tree_t::Allocator_t::allocator_->get_usage());
|
||||
NONFATAL("Tree Statistics\n %s\n", tree_.Statistics().c_str());
|
||||
NONFATAL("Computing all nearest neighbors...\n");
|
||||
fflush(stdout);
|
||||
tree_.AllNearestNeighbors(tree_.get_parent(), knns);
|
||||
NONFATAL("Collecting results....\n");
|
||||
tree_.CollectKNearestNeighborWithFwriteText("allnn.txt");
|
||||
ArrayList<index_t> resulting_neighbors;
|
||||
ArrayList<double> distances;
|
||||
allknn_.ComputeNeighbors(&resulting_neighbors,
|
||||
&distances);
|
||||
FILE *fp=fopen("allnn.txt", "w");
|
||||
if (fp==NULL) {
|
||||
FATAL("Unable to open allnn for exporting the results, error %s\n",
|
||||
strerror(errno));
|
||||
}
|
||||
for(index_t i=0; i<resulting_neighbors.size(); i++) {
|
||||
fprintf(fp, "%lli %lli %lg\n", i / knns_, resulting_neighbors[i],
|
||||
distances[i]);
|
||||
}
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
template<typename DISTANCEKERNEL>
|
||||
@@ -64,7 +55,7 @@ void KernelPCA::ComputeGeneralKernelPCA(DISTANCEKERNEL kernel,
|
||||
kernel_matrix_.Copy(affinity_matrix_);
|
||||
kernel_matrix_.ApplyFunction(kernel);
|
||||
Vector temp;
|
||||
temp.Init(kernel_matrix_.get_dimension());
|
||||
temp.Init(kernel_matrix_.dimension());
|
||||
temp.SetAll(1.0);
|
||||
kernel_matrix_.SetDiagonal(temp);
|
||||
kernel_matrix_.EndLoading();
|
||||
@@ -127,48 +118,46 @@ void KernelPCA::EstimateBandwidth(double *bandwidth) {
|
||||
*bandwidth=mean/count;
|
||||
}
|
||||
|
||||
void KernelPCA::ComputeLLE(index_t knns,
|
||||
index_t num_of_eigenvalues,
|
||||
void KernelPCA::ComputeLLE(index_t num_of_eigenvalues,
|
||||
Matrix *eigen_vectors,
|
||||
std::vector<double> *eigen_values);
|
||||
{
|
||||
FILE *fp=fopen("allnn.txt");
|
||||
std::vector<double> *eigen_values) {
|
||||
FILE *fp=fopen("allnn.txt", "r");
|
||||
if unlikely(fp==NULL) {
|
||||
FATAL("Unable to open allnn.txt, error %s\n", strerror(errno));
|
||||
}
|
||||
uint64 p1, p2;
|
||||
double dist;
|
||||
double mean=0;
|
||||
uint64 last_point=numeric_limits<uint64>::max();
|
||||
Vector point;
|
||||
point.Init(dimension_);
|
||||
Matriix neighbors;
|
||||
neighbor_vals.Init(dimension_, knns);
|
||||
Matrix cov(neighbors);
|
||||
Matrix neighbor_vals;
|
||||
neighbor_vals.Init(dimension_, knns_);
|
||||
Matrix cov(neighbor_vals);
|
||||
Vector ones;
|
||||
ones.Init(dimension_);
|
||||
ones.SetAll(1);
|
||||
Vector weights;
|
||||
index_t neighbors[knns];
|
||||
index_t neighbors[knns_];
|
||||
index_t i;
|
||||
kernel_matrix_.Init(data_.get_num_of_points(),
|
||||
data_.get_num_of_point());
|
||||
kernel_matrix_.Init(data_.n_rows(),
|
||||
data_.n_rows(),
|
||||
knns_);
|
||||
while (!feof(fp)) {
|
||||
fscanf(fp, "%llu %llu %lg", &p1, &p2, &dist);
|
||||
i=0;
|
||||
if (p1==last_point) {
|
||||
memcpy(neighbor_vals.GetColumnPtr(i), data_.At(p2),
|
||||
memcpy(neighbor_vals.GetColumnPtr(i), data_.GetColumnPtr(p2),
|
||||
sizeof(double)*dimension_);
|
||||
neighbors[i]=p2;
|
||||
la::SubFrom(dimension_, point.ptr(), neighbor_vals.GetColumnPtr(i));
|
||||
} else {
|
||||
point.Copy(data_.At());
|
||||
point.Copy(data_.GetColumnPtr(p1), dimension_);
|
||||
last_point=p1;
|
||||
i=0;
|
||||
la::MulTransBInit(neighbor_vals, neighbor_vals, &cov);
|
||||
la::SolveInit(cov, ones, &weights);
|
||||
kernel_matrix_.LoadRow(p1, neighbors,weights.ptr());
|
||||
weights.Destruct()
|
||||
kernel_matrix_.LoadRow(p1, knns_, neighbors, weights.ptr());
|
||||
weights.Destruct();
|
||||
}
|
||||
}
|
||||
kernel_matrix_.Negate();
|
||||
|
||||
@@ -25,7 +25,7 @@ class KernelPCATest {
|
||||
public:
|
||||
void Init() {
|
||||
engine_ = new KernelPCA();
|
||||
engine_->Init("test_data_3_1000", "");
|
||||
engine_->Init("test_data_3_1000.csv", 3, 20);
|
||||
}
|
||||
void Destruct() {
|
||||
delete engine_;
|
||||
@@ -34,7 +34,7 @@ class KernelPCATest {
|
||||
Matrix eigen_vectors;
|
||||
std::vector<double> eigen_values;
|
||||
Init();
|
||||
engine_->ComputeNeighborhoods(10);
|
||||
engine_->ComputeNeighborhoods();
|
||||
double bandwidth;
|
||||
engine_->EstimateBandwidth(&bandwidth);
|
||||
NONFATAL("Estimated bandwidth %lg ...\n", bandwidth);
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user