[mlpack-svn] r10319 - in mlpack/trunk/src/mlpack: core/io core/kernels core/tree core/utilities methods/emst methods/naive_bayes methods/neighbor_search methods/neighbor_search/sort_policies methods/nnsvm methods/svm tests
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Thu Nov 17 14:53:38 EST 2011
Author: mamidon
Date: 2011-11-17 14:53:37 -0500 (Thu, 17 Nov 2011)
New Revision: 10319
Removed:
mlpack/trunk/src/mlpack/core/tree/binary_space_tree_crtp.hpp
mlpack/trunk/src/mlpack/core/tree/binary_space_tree_impl_crtp.hpp
Modified:
mlpack/trunk/src/mlpack/core/io/cli.cpp
mlpack/trunk/src/mlpack/core/kernels/lmetric.cpp
mlpack/trunk/src/mlpack/core/kernels/lmetric.hpp
mlpack/trunk/src/mlpack/core/tree/CMakeLists.txt
mlpack/trunk/src/mlpack/core/tree/hrectbound.hpp
mlpack/trunk/src/mlpack/core/tree/hrectbound_impl.hpp
mlpack/trunk/src/mlpack/core/utilities/timers.cpp
mlpack/trunk/src/mlpack/core/utilities/timers.hpp
mlpack/trunk/src/mlpack/methods/emst/dtb.hpp
mlpack/trunk/src/mlpack/methods/naive_bayes/nbc_main.cc
mlpack/trunk/src/mlpack/methods/neighbor_search/allkfn_main.cc
mlpack/trunk/src/mlpack/methods/neighbor_search/neighbor_search.h
mlpack/trunk/src/mlpack/methods/neighbor_search/neighbor_search_impl.h
mlpack/trunk/src/mlpack/methods/neighbor_search/sort_policies/furthest_neighbor_sort.hpp
mlpack/trunk/src/mlpack/methods/neighbor_search/sort_policies/furthest_neighbor_sort_impl.hpp
mlpack/trunk/src/mlpack/methods/neighbor_search/sort_policies/nearest_neighbor_sort.hpp
mlpack/trunk/src/mlpack/methods/neighbor_search/sort_policies/nearest_neighbor_sort_impl.hpp
mlpack/trunk/src/mlpack/methods/neighbor_search/typedef.h
mlpack/trunk/src/mlpack/methods/nnsvm/nnsvm_impl.hpp
mlpack/trunk/src/mlpack/methods/nnsvm/nnsvm_main.cpp
mlpack/trunk/src/mlpack/methods/svm/svm_impl.h
mlpack/trunk/src/mlpack/tests/allkfn_test.cpp
Log:
Reverted changes, as it is apparent diff did not work. Will work on that, too.
Modified: mlpack/trunk/src/mlpack/core/io/cli.cpp
===================================================================
--- mlpack/trunk/src/mlpack/core/io/cli.cpp 2011-11-17 19:30:24 UTC (rev 10318)
+++ mlpack/trunk/src/mlpack/core/io/cli.cpp 2011-11-17 19:53:37 UTC (rev 10319)
@@ -1,4 +1,4 @@
-#include "cli.hpp"
+#include "cli.hpp"
#include "log.hpp"
#include "../utilities/timers.hpp"
@@ -59,10 +59,10 @@
did_parse(false), doc(&empty_program_doc) {
return;
}
-
+
CLI::~CLI() {
// Terminate the program timer.
- Timers::Stop("total_time");
+ Timers::StopTimer("total_time");
// Did the user ask for verbose output? If so we need to print everything.
// But only if the user did not ask for help or info.
@@ -75,7 +75,7 @@
std::map<std::string, timeval>::iterator iter;
for(iter = times.begin(); iter != times.end(); iter++) {
Log::Info << iter->first << " -- ";
- Timers::Print(iter->first.c_str());
+ Timers::PrintTimer(iter->first.c_str());
}
}
@@ -297,7 +297,7 @@
DefaultMessages();
RequiredOptions();
- Timers::Start("total_time");
+ Timers::StartTimer("total_time");
}
/*
@@ -322,7 +322,7 @@
DefaultMessages();
RequiredOptions();
- Timers::Start("total_time");
+ Timers::StartTimer("total_time");
}
/*
@@ -424,7 +424,7 @@
std::string str = *iter;
if (!vmap.count(str))
{// If a required option isn't there...
- Timers::Stop("total_time"); //Execution stop here, pretty much.
+ Timers::StopTimer("total_time"); //Execution stop here, pretty much.
Log::Fatal << "Required option --" << str.c_str() << " is undefined."
<< std::endl;
}
Modified: mlpack/trunk/src/mlpack/core/kernels/lmetric.cpp
===================================================================
--- mlpack/trunk/src/mlpack/core/kernels/lmetric.cpp 2011-11-17 19:30:24 UTC (rev 10318)
+++ mlpack/trunk/src/mlpack/core/kernels/lmetric.cpp 2011-11-17 19:53:37 UTC (rev 10319)
@@ -11,9 +11,7 @@
// L1-metric specializations; the root doesn't matter.
template<>
-template<typename elem_type>
-double LMetric<1, true>::Evaluate(const arma::Col<elem_type>& a,
- const arma::Col<elem_type>& b) {
+double LMetric<1, true>::Evaluate(const arma::vec& a, const arma::vec& b) {
double sum = 0;
for (size_t i = 0; i < a.n_elem; i++)
sum += fabs(a[i] - b[i]);
@@ -22,9 +20,7 @@
}
template<>
-template<typename elem_type>
-double LMetric<1, false>::Evaluate(const arma::Col<elem_type>& a,
- const arma::Col<elem_type>& b) {
+double LMetric<1, false>::Evaluate(const arma::vec& a, const arma::vec& b) {
double sum = 0;
for (size_t i = 0; i < a.n_elem; i++)
sum += fabs(a[i] - b[i]);
@@ -34,9 +30,7 @@
// L2-metric specializations.
template<>
-template<typename elem_type>
-double LMetric<2, true>::Evaluate(const arma::Col<elem_type>& a,
- const arma::Col<elem_type>& b) {
+double LMetric<2, true>::Evaluate(const arma::vec& a, const arma::vec& b) {
double sum = 0;
for (size_t i = 0; i < a.n_elem; i++)
sum += pow(a[i] - b[i], 2.0); // fabs() not necessary when squaring.
@@ -45,9 +39,7 @@
}
template<>
-template<typename elem_type>
-double LMetric<2, false>::Evaluate(const arma::Col<elem_type>& a,
- const arma::Col<elem_type>& b) {
+double LMetric<2, false>::Evaluate(const arma::vec& a, const arma::vec& b) {
double sum = 0;
for (size_t i = 0; i < a.n_elem; i++)
sum += pow(a[i] - b[i], 2.0);
@@ -57,9 +49,7 @@
// L3-metric specialization (not very likely to be used, but just in case).
template<>
-template<typename elem_type>
-double LMetric<3, true>::Evaluate(const arma::Col<elem_type>& a,
- const arma::Col<elem_type>& b) {
+double LMetric<3, true>::Evaluate(const arma::vec& a, const arma::vec& b) {
double sum = 0;
for (size_t i = 0; i < a.n_elem; i++)
sum += pow(fabs(a[i] - b[i]), 3.0);
@@ -68,9 +58,7 @@
}
template<>
-template<typename elem_type>
-double LMetric<3, false>::Evaluate(const arma::Col<elem_type>& a,
- const arma::Col<elem_type>& b) {
+double LMetric<3, false>::Evaluate(const arma::vec& a, const arma::vec& b) {
double sum = 0;
for (size_t i = 0; i < a.n_elem; i++)
sum += pow(fabs(a[i] - b[i]), 3.0);
@@ -79,4 +67,4 @@
}
}; // namespace kernel
-}; // namespace mlpack
+}; // namespace mlpack
Modified: mlpack/trunk/src/mlpack/core/kernels/lmetric.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/kernels/lmetric.hpp 2011-11-17 19:30:24 UTC (rev 10318)
+++ mlpack/trunk/src/mlpack/core/kernels/lmetric.hpp 2011-11-17 19:53:37 UTC (rev 10319)
@@ -62,9 +62,7 @@
/**
* Computes the distance between two points.
*/
- template<typename elem_type>
- static double Evaluate(const arma::Col<elem_type>& a,
- const arma::Col<elem_type>& b);
+ static double Evaluate(const arma::vec& a, const arma::vec& b);
};
// Doxygen will not include this specialization.
@@ -74,9 +72,8 @@
// the unspecialized implementation of the one function is given below.
// Unspecialized implementation. This should almost never be used...
template<int t_pow, bool t_take_root>
-template<typename elem_type>
-double LMetric<t_pow, t_take_root>::Evaluate(const arma::Col<elem_type>& a,
- const arma::Col<elem_type>& b) {
+double LMetric<t_pow, t_take_root>::Evaluate(const arma::vec& a,
+ const arma::vec& b) {
double sum = 0;
for (size_t i = 0; i < a.n_elem; i++)
sum += pow(fabs(a[i] - b[i]), t_pow);
Modified: mlpack/trunk/src/mlpack/core/tree/CMakeLists.txt
===================================================================
--- mlpack/trunk/src/mlpack/core/tree/CMakeLists.txt 2011-11-17 19:30:24 UTC (rev 10318)
+++ mlpack/trunk/src/mlpack/core/tree/CMakeLists.txt 2011-11-17 19:53:37 UTC (rev 10319)
@@ -5,8 +5,6 @@
set(SOURCES
binary_space_tree.hpp
binary_space_tree_impl.hpp
- binary_space_tree_crtp.hpp
- binary_space_tree_impl_crtp.hpp
bounds.hpp
dballbound.hpp
dballbound_impl.hpp
Deleted: mlpack/trunk/src/mlpack/core/tree/binary_space_tree_crtp.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/tree/binary_space_tree_crtp.hpp 2011-11-17 19:30:24 UTC (rev 10318)
+++ mlpack/trunk/src/mlpack/core/tree/binary_space_tree_crtp.hpp 2011-11-17 19:53:37 UTC (rev 10319)
@@ -1,294 +0,0 @@
-/**
- * @file spacetree.h
- *
- * Definition of generalized binary space partitioning tree (BinarySpaceTree).
- */
-#ifndef __MLPACK_CORE_TREE_BINARY_SPACE_TREE_HPP
-#define __MLPACK_CORE_TREE_BINARY_SPACE_TREE_HPP
-
-#include <mlpack/core.h>
-
-#include "statistic.hpp"
-
-namespace mlpack {
-namespace tree /** Trees and tree-building procedures. */ {
-
-PARAM_MODULE("tree", "Parameters for the binary space partitioning tree.");
-PARAM_INT("leaf_size", "Leaf size used during tree construction.", "tree", 20);
-
-/**
- * A binary space partitioning tree, such as a KD-tree or a ball tree. Once the
- * bound and type of dataset is defined, the tree will construct itself. Call
- * the constructor with the dataset to build the tree on, and the entire tree
- * will be built.
- *
- * This particular tree does not allow growth, so you cannot add or delete nodes
- * from it. If you need to add or delete a node, the better procedure is to
- * rebuild the tree entirely.
- *
- * This tree does take one parameter, which is the leaf size to be used. You
- * can set this at runtime with --tree/leaf_size [leaf_size]. You can also set
- * it in your program using CLI:
- *
- * @code
- * CLI::GetParam<int>("tree/leaf_size") = target_leaf_size;
- * @endcode
- *
- * @param leaf_size Maximum number of points allowed in each leaf.
- *
- * @tparam TBound The bound used for each node. The valid types of bounds and
- * the necessary skeleton interface for this class can be found in bounds/.
- * @tparam TDataset The type of dataset (forced to be arma::mat for now).
- * @tparam TStatistic Extra data contained in the node. See statistic.h for
- * the necessary skeleton interface.
- */
-template<typename T1,
- typename Bound,
- typename Statistic = EmptyStatistic>
-class BinarySpaceTree {
- private:
- //! The left child node.
- BinarySpaceTree *left_;
- //! The right child node.
- BinarySpaceTree *right_;
- //! The index of the first point in the dataset contained in this node (and
- //! its children).
- size_t begin_;
- //! The number of points of the dataset contained in this node (and its
- //! children).
- size_t count_;
- //! The bound object for this node.
- Bound bound_;
- //! Any extra data contained in the node.
- Statistic stat_;
-
- public:
- /**
- * Construct this as the root node of a binary space tree using the given
- * dataset. This will modify the ordering of the points in the dataset!
- *
- * @param data Dataset to create tree from. This will be modified!
- */
- BinarySpaceTree(arma::Base<typename T1::elem_type, T1>& data);
-
- /**
- * Construct this as the root node of a binary space tree using the given
- * dataset. This will modify the ordering of points in the dataset! A
- * mapping of the old point indices to the new point indices is filled.
- *
- * @param data Dataset to create tree from. This will be modified!
- * @param old_from_new Vector which will be filled with the old positions for
- * each new point.
- * @param new_from_old Vector which will be filled with the new positions for
- * each old point.
- */
- BinarySpaceTree(arma::Base<typename T1::elem_type, T1>& data, std::vector<size_t>& old_from_new);
-
- /**
- * Construct this as the root node of a binary space tree using the given
- * dataset. This will modify the ordering of points in the dataset! A
- * mapping of the old point indices to the new point indices is filled, as
- * well as a mapping of the new point indices to the old point indices.
- *
- * @param data Dataset to create tree from. This will be modified!
- * @param old_from_new Vector which will be filled with the old positions for
- * each new point.
- * @param new_from_old Vector which will be filled with the new positions for
- * each old point.
- */
- BinarySpaceTree(arma::Base<typename T1::elem_type, T1>& data,
- std::vector<size_t>& old_from_new,
- std::vector<size_t>& new_from_old);
-
- /**
- * Construct this node on a subset of the given matrix, starting at column
- * begin_in and using count_in points. The ordering of that subset of points
- * will be modified! This is used for recursive tree-building by the other
- * constructors which don't specify point indices.
- *
- * @param data Dataset to create tree from. This will be modified!
- * @param begin_in Index of point to start tree construction with.
- * @param count_in Number of points to use to construct tree.
- */
- BinarySpaceTree(arma::Base<typename T1::elem_type, T1>& data,
- size_t begin_in,
- size_t count_in);
-
- /**
- * Construct this node on a subset of the given matrix, starting at column
- * begin_in and using count_in points. The ordering of that subset of points
- * will be modified! This is used for recursive tree-building by the other
- * constructors which don't specify point indices.
- *
- * A mapping of the old point indices to the new point indices is filled, but
- * it is expected that the vector is already allocated with size greater than
- * or equal to (begin_in + count_in), and if that is not true, invalid memory
- * reads (and writes) will occur.
- *
- * @param data Dataset to create tree from. This will be modified!
- * @param begin_in Index of point to start tree construction with.
- * @param count_in Number of points to use to construct tree.
- * @param old_from_new Vector which will be filled with the old positions for
- * each new point.
- */
- BinarySpaceTree(arma::Base<typename T1::elem_type, T1>& data,
- size_t begin_in,
- size_t count_in,
- std::vector<size_t>& old_from_new);
-
- /**
- * Construct this node on a subset of the given matrix, starting at column
- * begin_in and using count_in points. The ordering of that subset of points
- * will be modified! This is used for recursive tree-building by the other
- * constructors which don't specify point indices.
- *
- * A mapping of the old point indices to the new point indices is filled, as
- * well as a mapping of the new point indices to the old point indices. It is
- * expected that the vector is already allocated with size greater than or
- * equal to (begin_in + count_in), and if that is not true, invalid memory
- * reads (and writes) will occur.
- *
- * @param data Dataset to create tree from. This will be modified!
- * @param begin_in Index of point to start tree construction with.
- * @param count_in Number of points to use to construct tree.
- * @param old_from_new Vector which will be filled with the old positions for
- * each new point.
- * @param new_from_old Vector which will be filled with the new positions for
- * each old point.
- */
- BinarySpaceTree(arma::Base<typename T1::elem_type, T1>& data,
- size_t begin_in,
- size_t count_in,
- std::vector<size_t>& old_from_new,
- std::vector<size_t>& new_from_old);
-
- /**
- * Create an empty tree node.
- */
- BinarySpaceTree();
-
- /**
- * Deletes this node, deallocating the memory for the children and calling
- * their destructors in turn. This will invalidate any pointers or references
- * to any nodes which are children of this one.
- */
- ~BinarySpaceTree();
-
- /**
- * Find a node in this tree by its begin and count (const).
- *
- * Every node is uniquely identified by these two numbers.
- * This is useful for communicating position over the network,
- * when pointers would be invalid.
- *
- * @param begin_q The begin() of the node to find.
- * @param count_q The count() of the node to find.
- * @return The found node, or NULL if not found.
- */
- const BinarySpaceTree* FindByBeginCount(size_t begin_q,
- size_t count_q) const;
-
- /**
- * Find a node in this tree by its begin and count.
- *
- * Every node is uniquely identified by these two numbers.
- * This is useful for communicating position over the network,
- * when pointers would be invalid.
- *
- * @param begin_q The begin() of the node to find.
- * @param count_q The count() of the node to find.
- * @return The found node, or NULL if not found.
- */
- BinarySpaceTree* FindByBeginCount(size_t begin_q, size_t count_q);
-
- //! Return the bound object for this node.
- const Bound& bound() const;
- //! Return the bound object for this node.
- Bound& bound();
-
- //! Return the statistic object for this node.
- const Statistic& stat() const;
- //! Return the statistic object for this node.
- Statistic& stat();
-
- //! Return whether or not this node is a leaf (true if it has no children).
- bool is_leaf() const;
-
- /**
- * Gets the left child of this node.
- */
- BinarySpaceTree *left() const;
-
- /**
- * Gets the right child of this node.
- */
- BinarySpaceTree *right() const;
-
- /**
- * Gets the index of the beginning point of this subset.
- */
- size_t begin() const;
-
- /**
- * Gets the index one beyond the last index in the subset.
- */
- size_t end() const;
-
- /**
- * Gets the number of points in this subset.
- */
- size_t count() const;
-
- void Print() const;
-
- private:
- /**
- * Splits the current node, assigning its left and right children recursively.
- *
- * @param data Dataset which we are using.
- */
- void SplitNode(arma::Base<typename T1::elem_type, T1>& data);
-
- /**
- * Splits the current node, assigning its left and right children recursively.
- * Also returns a list of the changed indices.
- *
- * @param data Dataset which we are using.
- * @param old_from_new Vector holding permuted indices.
- */
- void SplitNode(arma::Base<typename T1::elem_type, T1>& data,
- std::vector<size_t>& old_from_new);
-
- /**
- * Find the index to split on for this node, given that we are splitting in
- * the given split dimension on the specified split value.
- *
- * @param data Dataset which we are using.
- * @param split_dim Dimension of dataset to split on.
- * @param split_val Value to split on, in the given split dimension.
- */
- size_t GetSplitIndex(arma::Base<typename T1::elem_type, T1>& data,
- int split_dim, double split_val);
-
- /**
- * Find the index to split on for this node, given that we are splitting in
- * the given split dimension on the specified split value. Also returns a
- * list of the changed indices.
- *
- * @param data Dataset which we are using.
- * @param split_dim Dimension of dataset to split on.
- * @param split_val Value to split on, in the given split dimension.
- * @param old_from_new Vector holding permuted indices.
- */
- size_t GetSplitIndex(arma::Base<typename T1::elem_type, T1>& data,
- int split_dim, double split_val, std::vector<size_t>& old_from_new);
-
-};
-
-}; // namespace tree
-}; // namespace mlpack
-
-// Include implementation.
-#include "binary_space_tree_impl_crtp.hpp"
-
-#endif
Deleted: mlpack/trunk/src/mlpack/core/tree/binary_space_tree_impl_crtp.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/tree/binary_space_tree_impl_crtp.hpp 2011-11-17 19:30:24 UTC (rev 10318)
+++ mlpack/trunk/src/mlpack/core/tree/binary_space_tree_impl_crtp.hpp 2011-11-17 19:53:37 UTC (rev 10319)
@@ -1,481 +0,0 @@
-/**
- * @file binary_space_tree_impl.hpp
- *
- * Implementation of generalized space partitioning tree.
- */
-#ifndef __MLPACK_CORE_TREE_BINARY_SPACE_TREE_IMPL_HPP
-#define __MLPACK_CORE_TREE_BINARY_SPACE_TREE_IMPL_HPP
-
-// In case it wasn't included already for some reason.
-#include "binary_space_tree_crtp.hpp"
-
-#include <mlpack/core/io/cli.hpp>
-#include <mlpack/core/io/log.hpp>
-
-namespace mlpack {
-namespace tree {
-
-// Each of these overloads is kept as a separate function to keep the overhead
-// from the two std::vectors out, if possible.
-template<typename T1, typename Bound, typename Statistic>
-BinarySpaceTree<T1, Bound, Statistic>::BinarySpaceTree(
- arma::Base<typename T1::elem_type, T1>& data) : left_(NULL), right_(NULL), begin_(0), /* This root node starts at index 0, */
- count_(data.get_ref().n_cols), /* and spans all of the dataset. */
- bound_(data.get_ref().n_rows),
- stat_() {
- // Do the actual splitting of this node.
- SplitNode(data);
-}
-
-template<typename T1, typename Bound, typename Statistic>
-BinarySpaceTree<T1, Bound, Statistic>::BinarySpaceTree(
- arma::Base<typename T1::elem_type, T1>& data,
- std::vector<size_t>& old_from_new) :
- left_(NULL),
- right_(NULL),
- begin_(0),
- count_(data.get_ref().n_cols),
- bound_(data.get_ref().n_rows),
- stat_() {
- // Initialize old_from_new correctly.
- old_from_new.resize(data.get_ref().n_cols);
- for (size_t i = 0; i < data.get_ref().n_cols; i++)
- old_from_new[i] = i; // Fill with unharmed indices.
-
- // Now do the actual splitting.
- SplitNode(data, old_from_new);
-}
-
-template<typename T1, typename Bound, typename Statistic>
-BinarySpaceTree<T1, Bound, Statistic>::BinarySpaceTree(
- arma::Base<typename T1::elem_type, T1>& data,
- std::vector<size_t>& old_from_new,
- std::vector<size_t>& new_from_old) :
- left_(NULL),
- right_(NULL),
- begin_(0),
- count_(data.get_ref().n_cols),
- bound_(data.get_ref().n_rows),
- stat_() {
- // Initialize the old_from_new vector correctly.
- old_from_new.resize(data.get_ref().n_cols);
- for (size_t i = 0; i < data.get_ref().n_cols; i++)
- old_from_new[i] = i; // Fill with unharmed indices.
-
- // Now do the actual splitting.
- SplitNode(data, old_from_new);
-
- // Map the new_from_old indices correctly.
- new_from_old.resize(data.get_ref().n_cols);
- for (size_t i = 0; i < data.get_ref().n_cols; i++)
- new_from_old[old_from_new[i]] = i;
-}
-
-template<typename T1, typename Bound, typename Statistic>
-BinarySpaceTree<T1, Bound, Statistic>::BinarySpaceTree(
- arma::Base<typename T1::elem_type, T1>& data,
- size_t begin_in,
- size_t count_in) :
- left_(NULL),
- right_(NULL),
- begin_(begin_in),
- count_(count_in),
- bound_(data.get_ref().n_rows),
- stat_() {
- // Perform the actual splitting.
- SplitNode(data);
-}
-
-template<typename T1, typename Bound, typename Statistic>
-BinarySpaceTree<T1, Bound, Statistic>::BinarySpaceTree(
- arma::Base<typename T1::elem_type, T1>& data,
- size_t begin_in,
- size_t count_in,
- std::vector<size_t>& old_from_new) :
- left_(NULL),
- right_(NULL),
- begin_(begin_in),
- count_(count_in),
- bound_(data.get_ref().n_rows),
- stat_() {
- // Hopefully the vector is initialized correctly! We can't check that
- // entirely but we can do a minor sanity check.
- assert(old_from_new.size() == data.get_ref().n_cols);
-
- // Perform the actual splitting.
- SplitNode(data, old_from_new);
-}
-
-template<typename T1, typename Bound, typename Statistic>
-BinarySpaceTree<T1, Bound, Statistic>::BinarySpaceTree(
- arma::Base<typename T1::elem_type, T1>& data,
- size_t begin_in,
- size_t count_in,
- std::vector<size_t>& old_from_new,
- std::vector<size_t>& new_from_old) :
- left_(NULL),
- right_(NULL),
- begin_(begin_in),
- count_(count_in),
- bound_(data.get_ref().n_rows),
- stat_() {
- // Hopefully the vector is initialized correctly! We can't check that
- // entirely but we can do a minor sanity check.
- assert(old_from_new.size() == data.get_ref().n_cols);
-
- // Perform the actual splitting.
- SplitNode(data, old_from_new);
-
- // Map the new_from_old indices correctly.
- new_from_old.resize(data.get_reg().n_cols);
- for (size_t i = 0; i < data.get_ref().n_cols; i++)
- new_from_old[old_from_new[i]] = i;
-}
-
-template<typename T1, typename Bound, typename Statistic>
-BinarySpaceTree<T1, Bound, Statistic>::BinarySpaceTree() :
- left_(NULL),
- right_(NULL),
- begin_(0),
- count_(0),
- bound_(),
- stat_() {
- // Nothing to do.
-}
-
-/**
- * Deletes this node, deallocating the memory for the children and calling their
- * destructors in turn. This will invalidate any pointers or references to any
- * nodes which are children of this one.
- */
-template<typename T1, typename Bound, typename Statistic>
-BinarySpaceTree<T1, Bound, Statistic>::~BinarySpaceTree() {
- if (left_)
- delete left_;
- if (right_)
- delete right_;
-}
-
-/**
- * Find a node in this tree by its begin and count.
- *
- * Every node is uniquely identified by these two numbers.
- * This is useful for communicating position over the network,
- * when pointers would be invalid.
- *
- * @param begin_q the begin() of the node to find
- * @param count_q the count() of the node to find
- * @return the found node, or NULL
- */
-template<typename T1, typename Bound, typename Statistic>
-const BinarySpaceTree<T1, Bound, Statistic>*
-BinarySpaceTree<T1, Bound, Statistic>::FindByBeginCount(size_t begin_q,
- size_t count_q) const {
-
- mlpack::Log::Assert(begin_q >= begin_);
- mlpack::Log::Assert(count_q <= count_);
- if (begin_ == begin_q && count_ == count_q)
- return this;
- else if (is_leaf())
- return NULL;
- else if (begin_q < right_->begin_)
- return left_->FindByBeginCount(begin_q, count_q);
- else
- return right_->FindByBeginCount(begin_q, count_q);
-}
-
-/**
- * Find a node in this tree by its begin and count (const).
- *
- * Every node is uniquely identified by these two numbers.
- * This is useful for communicating position over the network,
- * when pointers would be invalid.
- *
- * @param begin_q the begin() of the node to find
- * @param count_q the count() of the node to find
- * @return the found node, or NULL
- */
-template<typename T1, typename Bound, typename Statistic>
-BinarySpaceTree<T1, Bound, Statistic>*
-BinarySpaceTree<T1, Bound, Statistic>::FindByBeginCount(size_t begin_q,
- size_t count_q) {
-
- mlpack::Log::Assert(begin_q >= begin_);
- mlpack::Log::Assert(count_q <= count_);
- if (begin_ == begin_q && count_ == count_q)
- return this;
- else if (is_leaf())
- return NULL;
- else if (begin_q < right_->begin_)
- return left_->FindByBeginCount(begin_q, count_q);
- else
- return right_->FindByBeginCount(begin_q, count_q);
-}
-
-template<typename T1, typename Bound, typename Statistic>
-const Bound& BinarySpaceTree<T1, Bound, Statistic>::bound() const {
- return bound_;
-}
-
-template<typename T1, typename Bound, typename Statistic>
-Bound& BinarySpaceTree<T1, Bound, Statistic>::bound() {
- return bound_;
-}
-
-template<typename T1, typename Bound, typename Statistic>
-const Statistic& BinarySpaceTree<T1, Bound, Statistic>::stat() const {
- return stat_;
-}
-
-template<typename T1, typename Bound, typename Statistic>
-Statistic& BinarySpaceTree<T1, Bound, Statistic>::stat() {
- return stat_;
-}
-
-template<typename T1, typename Bound, typename Statistic>
-bool BinarySpaceTree<T1, Bound, Statistic>::is_leaf() const {
- return !left_;
-}
-
-/**
- * Gets the left branch of the tree.
- */
-template<typename T1, typename Bound, typename Statistic>
-BinarySpaceTree<T1, Bound, Statistic>*
-BinarySpaceTree<T1, Bound, Statistic>::left() const {
- // TODO: Const correctness
- return left_;
-}
-
-/**
- * Gets the right branch.
- */
-template<typename T1, typename Bound, typename Statistic>
-BinarySpaceTree<T1, Bound, Statistic>*
-BinarySpaceTree<T1, Bound, Statistic>::right() const {
- // TODO: Const correctness
- return right_;
-}
-
-/**
- * Gets the index of the begin point of this subset.
- */
-template<typename T1, typename Bound, typename Statistic>
-size_t BinarySpaceTree<T1, Bound, Statistic>::begin() const {
- return begin_;
-}
-
-/**
- * Gets the index one beyond the last index in the series.
- */
-template<typename T1, typename Bound, typename Statistic>
-size_t BinarySpaceTree<T1, Bound, Statistic>::end() const {
- return begin_ + count_;
-}
-
-/**
- * Gets the number of points in this subset.
- */
-template<typename T1, typename Bound, typename Statistic>
-size_t BinarySpaceTree<T1, Bound, Statistic>::count() const {
- return count_;
-}
-
-template<typename T1, typename Bound, typename Statistic>
-void BinarySpaceTree<T1, Bound, Statistic>::Print() const {
- printf("node: %d to %d: %d points total\n",
- begin_, begin_ + count_ - 1, count_);
- if (!is_leaf()) {
- left_->Print();
- right_->Print();
- }
-}
-
-template<typename T1, typename Bound, typename Statistic>
-void BinarySpaceTree<T1, Bound, Statistic>::SplitNode(
- arma::Base<typename T1::elem_type, T1>& data) {
- // This should be a single function for Bound.
- // We need to expand the bounds of this node properly.
- for (size_t i = begin_; i < (begin_ + count_); i++)
- bound_ |= data.get_ref().unsafe_col(i);
-
- // Now, check if we need to split at all.
- if (count_ <= (size_t) CLI::GetParam<int>("tree/leaf_size"))
- return; // We can't split this.
-
- // Figure out which dimension to split on.
- size_t split_dim = data.get_ref().n_rows; // Indicate invalid by max_dim + 1.
- double max_width = -1;
-
- // Find the split dimension.
- for (size_t d = 0; d < data.get_ref().n_rows; d++) {
- double width = bound_[d].width();
-
- if (width > max_width) {
- max_width = width;
- split_dim = d;
- }
- }
-
- // Split in the middle of that dimension.
- double split_val = bound_[split_dim].mid();
-
- if (max_width == 0) // All these points are the same. We can't split.
- return;
-
- // Perform the actual splitting. This will order the dataset such that points
- // with value in dimension split_dim less than or equal to split_val are on
- // the left of split_col, and points with value in dimension split_dim greater
- // than split_val are on the right side of split_col.
- size_t split_col = GetSplitIndex(data, split_dim, split_val);
-
- // Now that we know the split column, we will recursively split the children
- // by calling their constructors (which perform this splitting process).
- left_ = new BinarySpaceTree<T1, Bound, Statistic>(data, begin_,
- split_col - begin_);
- right_ = new BinarySpaceTree<T1, Bound, Statistic>(data, split_col,
- begin_ + count_ - split_col);
-}
-
-template<typename T1, typename Bound, typename Statistic>
-void BinarySpaceTree<T1, Bound, Statistic>::SplitNode(
- arma::Base<typename T1::elem_type, T1>& data,
- std::vector<size_t>& old_from_new) {
- // This should be a single function for Bound.
- // We need to expand the bounds of this node properly.
- for (size_t i = begin_; i < (begin_ + count_); i++)
- bound_ |= data.get_ref().unsafe_col(i);
-
- // First, check if we need to split at all.
- if (count_ <= (size_t) CLI::GetParam<int>("tree/leaf_size"))
- return; // We can't split this.
-
- // Figure out which dimension to split on.
- size_t split_dim = data.get_ref().n_rows; // Indicate invalid by max_dim + 1.
- double max_width = -1;
-
- // Find the split dimension.
- for (size_t d = 0; d < data.get_ref().n_rows; d++) {
- double width = bound_[d].width();
-
- if (width > max_width) {
- max_width = width;
- split_dim = d;
- }
- }
-
- // Split in the middle of that dimension.
- double split_val = bound_[split_dim].mid();
-
- if (max_width == 0) // All these points are the same. We can't split.
- return;
-
- // Perform the actual splitting. This will order the dataset such that points
- // with value in dimension split_dim less than or equal to split_val are on
- // the left of split_col, and points with value in dimension split_dim greater
- // than split_val are on the right side of split_col.
- size_t split_col = GetSplitIndex(data, split_dim, split_val, old_from_new);
-
- // Now that we know the split column, we will recursively split the children
- // by calling their constructors (which perform this splitting process).
- left_ = new BinarySpaceTree<T1, Bound, Statistic>(data, begin_,
- split_col - begin_, old_from_new);
- right_ = new BinarySpaceTree<T1, Bound, Statistic>(data, split_col,
- begin_ + count_ - split_col, old_from_new);
-}
-
-template<typename T1, typename Bound, typename Statistic>
-size_t BinarySpaceTree<T1, Bound, Statistic>::GetSplitIndex(
- arma::Base<typename T1::elem_type, T1>& data,
- int split_dim,
- double split_val) {
- // This method modifies the input dataset. We loop both from the left and
- // right sides of the points contained in this node. The points less than
- // split_val should be on the left side of the matrix, and the points greater
- // than split_val should be on the right side of the matrix.
- size_t left = begin_;
- size_t right = begin_ + count_ - 1;
-
- // First half-iteration of the loop is out here because the termination
- // condition is in the middle.
- while ((data.get_ref()(split_dim, left) < split_val) && (left <= right))
- left++;
- while ((data.get_ref()(split_dim, right) >= split_val) && (left <= right))
- right--;
-
- while(left <= right) {
- // Swap columns.
- data.get_ref().swap_cols(left, right);
-
- // See how many points on the left are correct. When they are correct,
- // increase the left counter accordingly. When we encounter one that isn't
- // correct, stop. We will switch it later.
- while ((data.get_ref()(split_dim, left) < split_val) && (left <= right))
- left++;
-
- // Now see how many points on the right are correct. When they are correct,
- // decrease the right counter accordingly. When we encounter one that isn't
- // correct, stop. We will switch it with the wrong point we found in the
- // previous loop.
- while ((data.get_ref()(split_dim, right) >= split_val) && (left <= right))
- right--;
- }
-
- assert(left == right + 1);
-
- return left;
-}
-
-template<typename T1, typename Bound, typename Statistic>
-size_t BinarySpaceTree<T1, Bound, Statistic>::GetSplitIndex(
- arma::Base<typename T1::elem_type, T1>& data,
- int split_dim,
- double split_val,
- std::vector<size_t>& old_from_new) {
- // This method modifies the input dataset. We loop both from the left and
- // right sides of the points contained in this node. The points less than
- // split_val should be on the left side of the matrix, and the points greater
- // than split_val should be on the right side of the matrix.
- size_t left = begin_;
- size_t right = begin_ + count_ -1;
-
- // First half-iteration of the loop is out here because the termination
- // condition is in the middle.
- while ((data.get_ref()(split_dim, left) < split_val) && (left <= right))
- left++;
- while ((data.get_ref()(split_dim, right) >= split_val) && (left <= right))
- right--;
-
- while(left <= right) {
- // Swap columns.
- T1 ref = data.get_ref();
- ref.swap_cols(left, right);
-
- // Update the indices for what we changed.
- size_t t = old_from_new[left];
- old_from_new[left] = old_from_new[right];
- old_from_new[right] = t;
-
- // See how many points on the left are correct. When they are correct,
- // increase the left counter accordingly. When we encounter one that isn't
- // correct, stop. We will switch it later.
- while ((data.get_ref()(split_dim, left) < split_val) && (left <= right))
- left++;
-
- // Now see how many points on the right are correct. When they are correct,
- // decrease the right counter accordingly. When we encounter one that isn't
- // correct, stop. We will switch it with the wrong point we found in the
- // previous loop.
- while ((data.get_ref()(split_dim, right) >= split_val) && (left <= right))
- right--;
- }
-
- assert(left == right + 1);
-
- return left;
-}
-
-}; // namespace tree
-}; // namespace mlpack
-
-#endif
Modified: mlpack/trunk/src/mlpack/core/tree/hrectbound.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/tree/hrectbound.hpp 2011-11-17 19:30:24 UTC (rev 10318)
+++ mlpack/trunk/src/mlpack/core/tree/hrectbound.hpp 2011-11-17 19:53:37 UTC (rev 10319)
@@ -73,8 +73,7 @@
/**
* Calculates minimum bound-to-point squared distance.
*/
- template<typename elem_type>
- double MinDistance(const arma::Col<elem_type>& point) const;
+ double MinDistance(const arma::vec& point) const;
/**
* Calculates minimum bound-to-bound squared distance.
@@ -86,8 +85,7 @@
/**
* Calculates maximum bound-to-point squared distance.
*/
- template<typename elem_type>
- double MaxDistance(const arma::Col<elem_type>& point) const;
+ double MaxDistance(const arma::vec& point) const;
/**
* Computes maximum distance.
@@ -107,8 +105,7 @@
/**
* Expands this region to include a new point.
*/
- template<typename elem_type>
- HRectBound& operator|=(const arma::Col<elem_type>& vector);
+ HRectBound& operator|=(const arma::vec& vector);
/**
* Expands this region to encompass another bound.
Modified: mlpack/trunk/src/mlpack/core/tree/hrectbound_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/tree/hrectbound_impl.hpp 2011-11-17 19:30:24 UTC (rev 10318)
+++ mlpack/trunk/src/mlpack/core/tree/hrectbound_impl.hpp 2011-11-17 19:53:37 UTC (rev 10319)
@@ -121,8 +121,7 @@
* Calculates minimum bound-to-point squared distance.
*/
template<int t_pow>
-template<typename elem_type>
-double HRectBound<t_pow>::MinDistance(const arma::Col<elem_type>& point) const {
+double HRectBound<t_pow>::MinDistance(const arma::vec& point) const {
assert(point.n_elem == dim_);
double sum = 0;
@@ -182,8 +181,7 @@
* Calculates maximum bound-to-point squared distance.
*/
template<int t_pow>
-template<typename elem_type>
-double HRectBound<t_pow>::MaxDistance(const arma::Col<elem_type>& point) const {
+double HRectBound<t_pow>::MaxDistance(const arma::vec& point) const {
double sum = 0;
assert(point.n_elem == dim_);
@@ -289,8 +287,7 @@
* Expands this region to include a new point.
*/
template<int t_pow>
-template<typename elem_type>
-HRectBound<t_pow>& HRectBound<t_pow>::operator|=(const arma::Col<elem_type>& vector) {
+HRectBound<t_pow>& HRectBound<t_pow>::operator|=(const arma::vec& vector) {
Log::Assert(vector.n_elem == dim_);
for (size_t i = 0; i < dim_; i++) {
Modified: mlpack/trunk/src/mlpack/core/utilities/timers.cpp
===================================================================
--- mlpack/trunk/src/mlpack/core/utilities/timers.cpp 2011-11-17 19:30:24 UTC (rev 10318)
+++ mlpack/trunk/src/mlpack/core/utilities/timers.cpp 2011-11-17 19:53:37 UTC (rev 10319)
@@ -13,12 +13,12 @@
return timers;
}
-timeval Timers::Get(const char* timerName) {
+timeval Timers::GetTimer(const char* timerName) {
std::string name(timerName);
return timers[name];
}
-void Timers::Print(const char* timerName) {
+void Timers::PrintTimer(const char* timerName) {
std::string name=timerName;
timeval&t=timers[name];
Log::Info<<t.tv_sec<<"."<<std::setw(6)<<std::setfill('0')
@@ -63,7 +63,7 @@
Log::Info << std::endl;
}
-void Timers::Start(const char* timerName) {
+void Timers::StartTimer(const char* timerName) {
//Don't want to actually document the timer
std::string name(timerName);
timeval tmp;
@@ -79,7 +79,7 @@
timers[name] = tmp;
}
-void Timers::Stop(const char* timerName) {
+void Timers::StopTimer(const char* timerName) {
std::string name(timerName);
timeval delta, b, a = timers[name];
@@ -94,7 +94,7 @@
}
#ifdef _WIN32
-void Timers::FileTimeToTimeVal(timeval* tv) {
+void Timers::FileTimeToTimeVal(timeval* tv) {
FILETIME ftime;
uint64_t ptime = 0;
//Acquire the file time
Modified: mlpack/trunk/src/mlpack/core/utilities/timers.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/utilities/timers.hpp 2011-11-17 19:30:24 UTC (rev 10318)
+++ mlpack/trunk/src/mlpack/core/utilities/timers.hpp 2011-11-17 19:53:37 UTC (rev 10319)
@@ -31,7 +31,7 @@
*
* @param timerName The name of the timer in question.
*/
- static timeval Get(const char* timerName);
+ static timeval GetTimer(const char* timerName);
/*
* Prints the specified timer. If it took longer than a minute to complete
@@ -39,7 +39,7 @@
*
* @param timerName The name of the timer in question.
*/
- static void Print(const char* timerName);
+ static void PrintTimer(const char* timerName);
/*
* Initializes a timer, available like a normal value specified on
@@ -47,7 +47,7 @@
*
* @param timerName The name of the timer in question.
*/
- static void Start(const char* timerName);
+ static void StartTimer(const char* timerName);
/*
* Halts the timer, and replaces it's value with
@@ -55,7 +55,7 @@
*
* @param timerName The name of the timer in question.
*/
- static void Stop(const char* timerName);
+ static void StopTimer(const char* timerName);
private:
static std::map<std::string, timeval> timers;
Modified: mlpack/trunk/src/mlpack/methods/emst/dtb.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/emst/dtb.hpp 2011-11-17 19:30:24 UTC (rev 10318)
+++ mlpack/trunk/src/mlpack/methods/emst/dtb.hpp 2011-11-17 19:53:37 UTC (rev 10319)
@@ -575,11 +575,11 @@
CLI::GetParam<int>("tree/leaf_size") =
CLI::GetParam<int>("emst/leaf_size");
- Timers::Start("emst/tree_building");
+ Timers::StartTimer("emst/tree_building");
tree_ = new DTBTree(data_points_, old_from_new_permutation_);
- Timers::Stop("emst/tree_building");
+ Timers::StopTimer("emst/tree_building");
}
else {
@@ -615,7 +615,7 @@
*/
void ComputeMST(arma::mat& results) {
- Timers::Start("emst/MST_computation");
+ Timers::StartTimer("emst/MST_computation");
while (number_of_edges_ < (number_of_points_ - 1)) {
@@ -639,7 +639,7 @@
}
- Timers::Stop("emst/MST_computation");
+ Timers::StopTimer("emst/MST_computation");
EmitResults_(results);
Modified: mlpack/trunk/src/mlpack/methods/naive_bayes/nbc_main.cc
===================================================================
--- mlpack/trunk/src/mlpack/methods/naive_bayes/nbc_main.cc 2011-11-17 19:30:24 UTC (rev 10318)
+++ mlpack/trunk/src/mlpack/methods/naive_bayes/nbc_main.cc 2011-11-17 19:53:37 UTC (rev 10319)
@@ -87,25 +87,25 @@
////// SIMPLE NAIVE BAYES CLASSIFICATCLIN ASSUMING THE DATA TO BE UNIFORMLY DISTRIBUTED //////
////// Timing the training of the Naive Bayes Classifier //////
- Timers::Start("nbc/training");
+ Timers::StartTimer("nbc/training");
////// Create and train the classifier
SimpleNaiveBayesClassifier nbc = SimpleNaiveBayesClassifier(training_data);
////// Stop training timer //////
- Timers::Stop("nbc/training");
+ Timers::StopTimer("nbc/training");
////// Timing the testing of the Naive Bayes Classifier //////
////// The variable that contains the result of the classification
arma::vec results;
- Timers::Start("nbc/testing");
+ Timers::StartTimer("nbc/testing");
////// Calling the function that classifies the test data
nbc.Classify(testing_data, results);
////// Stop testing timer //////
- Timers::Stop("nbc/testing");
+ Timers::StopTimer("nbc/testing");
////// OUTPUT RESULTS //////
std::string output_filename = CLI::GetParam<std::string>("nbc/output");
Modified: mlpack/trunk/src/mlpack/methods/neighbor_search/allkfn_main.cc
===================================================================
--- mlpack/trunk/src/mlpack/methods/neighbor_search/allkfn_main.cc 2011-11-17 19:30:24 UTC (rev 10318)
+++ mlpack/trunk/src/mlpack/methods/neighbor_search/allkfn_main.cc 2011-11-17 19:53:37 UTC (rev 10319)
@@ -45,7 +45,7 @@
string reference_file = CLI::GetParam<string>("reference_file");
string output_file = CLI::GetParam<string>("output_file");
- arma::vec reference_data;
+ arma::mat reference_data;
arma::Mat<size_t> neighbors;
arma::mat distances;
@@ -74,7 +74,7 @@
if (CLI::GetParam<string>("query_file") != "") {
string query_file = CLI::GetParam<string>("query_file");
- arma::vec query_data;
+ arma::mat query_data;
if (!data::Load(query_file.c_str(), query_data))
Log::Fatal << "Query file " << query_file << " not found" << endl;
Modified: mlpack/trunk/src/mlpack/methods/neighbor_search/neighbor_search.h
===================================================================
--- mlpack/trunk/src/mlpack/methods/neighbor_search/neighbor_search.h 2011-11-17 19:30:24 UTC (rev 10318)
+++ mlpack/trunk/src/mlpack/methods/neighbor_search/neighbor_search.h 2011-11-17 19:53:37 UTC (rev 10319)
@@ -9,7 +9,7 @@
#include <mlpack/core.h>
#include <mlpack/core/tree/bounds.hpp>
-#include <mlpack/core/tree/binary_space_tree_crtp.hpp>
+#include <mlpack/core/tree/binary_space_tree.hpp>
#include <vector>
#include <string>
@@ -54,8 +54,7 @@
* @tparam Kernel The kernel function; see kernel::ExampleKernel.
* @tparam SortPolicy The sort policy for distances; see NearestNeighborSort.
*/
-template<typename T1 = arma::mat,
- typename Kernel = mlpack::kernel::SquaredEuclideanDistance,
+template<typename Kernel = mlpack::kernel::SquaredEuclideanDistance,
typename SortPolicy = NearestNeighborSort>
class NeighborSearch {
@@ -80,13 +79,13 @@
* Simple typedef for the trees, which use a bound and a QueryStat (to store
* distances for each node). The bound should be configurable...
*/
- typedef tree::BinarySpaceTree<T1, bound::HRectBound<2>, QueryStat> TreeType;
+ typedef tree::BinarySpaceTree<bound::HRectBound<2>, QueryStat> TreeType;
private:
//! Reference dataset.
- arma::Base<typename T1::elem_type, T1> references_;
+ arma::mat references_;
//! Query dataset (may not be given).
- arma::Base<typename T1::elem_type, T1> queries_;
+ arma::mat queries_;
//! Instantiation of kernel.
Kernel kernel_;
@@ -135,8 +134,7 @@
* process! Defaults to false.
* @param kernel An optional instance of the Kernel class.
*/
- NeighborSearch(arma::Base<typename T1::elem_type, T1>& queries_in,
- arma::Base<typename T1::elem_type, T1>&references_in,
+ NeighborSearch(arma::mat& queries_in, arma::mat& references_in,
bool alias_matrix = false, Kernel kernel = Kernel());
/**
@@ -153,8 +151,8 @@
* process! Defaults to false.
* @param kernel An optional instance of the Kernel class.
*/
- NeighborSearch(arma::Base<typename T1::elem_type, T1>& references_in,
- bool alias_matrix = false, Kernel kernel = Kernel());
+ NeighborSearch(arma::mat& references_in, bool alias_matrix = false,
+ Kernel kernel = Kernel());
/**
* Delete the NeighborSearch object. The tree is the only member we are
@@ -216,8 +214,7 @@
* @param reference_node Reference node.
* @param best_dist_so_far Best distance to a node so far -- used for pruning.
*/
- void ComputeSingleNeighborsRecursion_(size_t point_id,
- arma::Col<typename T1::elem_type>& point,
+ void ComputeSingleNeighborsRecursion_(size_t point_id, arma::vec& point,
TreeType* reference_node,
double& best_dist_so_far);
Modified: mlpack/trunk/src/mlpack/methods/neighbor_search/neighbor_search_impl.h
===================================================================
--- mlpack/trunk/src/mlpack/methods/neighbor_search/neighbor_search_impl.h 2011-11-17 19:30:24 UTC (rev 10318)
+++ mlpack/trunk/src/mlpack/methods/neighbor_search/neighbor_search_impl.h 2011-11-17 19:53:37 UTC (rev 10319)
@@ -1,4 +1,4 @@
- /**
+/**
* @file neighbor_search.cc
*
* Implementation of AllkNN class to perform all-nearest-neighbors on two
@@ -13,13 +13,15 @@
// We call an advanced constructor of arma::mat which allows us to alias a
// matrix (if the user has asked for that).
-template<typename T1, typename Kernel, typename SortPolicy>
-NeighborSearch<T1, Kernel, SortPolicy>::NeighborSearch(arma::Base<typename T1::elem_type, T1>& queries_in,
- arma::Base<typename T1::elem_type, T1>& references_in,
+template<typename Kernel, typename SortPolicy>
+NeighborSearch<Kernel, SortPolicy>::NeighborSearch(arma::mat& queries_in,
+ arma::mat& references_in,
bool alias_matrix,
Kernel kernel) :
- references_(references_in), //Need to figure out how to push alias
- queries_(queries_in),
+ references_(references_in.memptr(), references_in.n_rows,
+ references_in.n_cols, !alias_matrix),
+ queries_(queries_in.memptr(), queries_in.n_rows, queries_in.n_cols,
+ !alias_matrix),
kernel_(kernel),
naive_(CLI::GetParam<bool>("neighbor_search/naive_mode")),
dual_mode_(!(naive_ || CLI::GetParam<bool>("neighbor_search/single_mode"))),
@@ -31,20 +33,20 @@
// Get the leaf size; naive ensures that the entire tree is one node
if (naive_)
CLI::GetParam<int>("tree/leaf_size") =
- std::max(queries_.get_ref().n_cols, references_.get_ref().n_cols);
+ std::max(queries_.n_cols, references_.n_cols);
// K-nearest neighbors initialization
knns_ = CLI::GetParam<int>("neighbor_search/k");
// Initialize the list of nearest neighbor candidates
- neighbor_indices_.set_size(knns_, queries_.get_ref().n_cols);
+ neighbor_indices_.set_size(knns_, queries_.n_cols);
// Initialize the vector of upper bounds for each point.
- neighbor_distances_.set_size(knns_, queries_.get_ref().n_cols);
+ neighbor_distances_.set_size(knns_, queries_.n_cols);
neighbor_distances_.fill(SortPolicy::WorstDistance());
// We'll time tree building
- Timers::Start("neighbor_search/tree_building");
+ Timers::StartTimer("neighbor_search/tree_building");
// This call makes each tree from a matrix, leaf size, and two arrays
// that record the permutation of the data points
@@ -52,16 +54,20 @@
reference_tree_ = new TreeType(references_, old_from_new_references_);
// Stop the timer we started above
- Timers::Stop("neighbor_search/tree_building");
+ Timers::StopTimer("neighbor_search/tree_building");
}
// We call an advanced constructor of arma::mat which allows us to alias a
// matrix (if the user has asked for that).
-template<typename T1, typename Kernel, typename SortPolicy>
-NeighborSearch<T1, Kernel, SortPolicy>::NeighborSearch(arma::Base<typename T1::elem_type, T1>& references_in,
+template<typename Kernel, typename SortPolicy>
+NeighborSearch<Kernel, SortPolicy>::NeighborSearch(arma::mat& references_in,
bool alias_matrix,
Kernel kernel) :
- references_(references_in), queries_(references_), kernel_(kernel),
+ references_(references_in.memptr(), references_in.n_rows,
+ references_in.n_cols, !alias_matrix),
+ queries_(references_.memptr(), references_.n_rows, references_.n_cols,
+ false),
+ kernel_(kernel),
naive_(CLI::GetParam<bool>("neighbor_search/naive_mode")),
dual_mode_(!(naive_ || CLI::GetParam<bool>("neighbor_search/single_mode"))),
number_of_prunes_(0) {
@@ -69,20 +75,20 @@
// Get the leaf size from the module
if (naive_)
CLI::GetParam<int>("tree/leaf_size") =
- std::max(queries_.get_ref().n_cols, references_.get_ref().n_cols);
+ std::max(queries_.n_cols, references_.n_cols);
// K-nearest neighbors initialization
knns_ = CLI::GetParam<int>("neighbor_search/k");
// Initialize the list of nearest neighbor candidates
- neighbor_indices_.set_size(knns_, references_.get_ref().n_cols);
+ neighbor_indices_.set_size(knns_, references_.n_cols);
// Initialize the vector of upper bounds for each point.
- neighbor_distances_.set_size(knns_, references_.get_ref().n_cols);
+ neighbor_distances_.set_size(knns_, references_.n_cols);
neighbor_distances_.fill(SortPolicy::WorstDistance());
// We'll time tree building
- Timers::Start("neighbor_search/tree_building");
+ Timers::StartTimer("neighbor_search/tree_building");
// This call makes each tree from a matrix, leaf size, and two arrays
// that record the permutation of the data points
@@ -91,15 +97,15 @@
reference_tree_ = new TreeType(references_, old_from_new_references_);
// Stop the timer we started above
- Timers::Stop("neighbor_search/tree_building");
+ Timers::StopTimer("neighbor_search/tree_building");
}
/**
* The tree is the only member we are responsible for deleting. The others will
* take care of themselves.
*/
-template<typename T1, typename Kernel, typename SortPolicy>
-NeighborSearch<T1, Kernel, SortPolicy>::~NeighborSearch() {
+template<typename Kernel, typename SortPolicy>
+NeighborSearch<Kernel, SortPolicy>::~NeighborSearch() {
if (reference_tree_ != query_tree_)
delete reference_tree_;
if (query_tree_ != NULL)
@@ -109,8 +115,8 @@
/**
* Performs exhaustive computation between two leaves.
*/
-template<typename T1, typename Kernel, typename SortPolicy>
-void NeighborSearch<T1, Kernel, SortPolicy>::ComputeBaseCase_(
+template<typename Kernel, typename SortPolicy>
+void NeighborSearch<Kernel, SortPolicy>::ComputeBaseCase_(
TreeType* query_node,
TreeType* reference_node) {
// Used to find the query node's new upper bound
@@ -122,11 +128,10 @@
query_index < query_node->end(); query_index++) {
// Get the query point from the matrix
- arma::Col<typename T1::elem_type> query_point =
- queries_.get_ref().unsafe_col(query_index);
+ arma::vec query_point = queries_.unsafe_col(query_index);
- double query_to_node_distance = SortPolicy::BestPointToNodeDistance
- (query_point, reference_node);
+ double query_to_node_distance =
+ SortPolicy::BestPointToNodeDistance(query_point, reference_node);
if (SortPolicy::IsBetter(query_to_node_distance,
neighbor_distances_(knns_ - 1, query_index))) {
@@ -137,8 +142,7 @@
// Confirm that points do not identify themselves as neighbors
// in the monochromatic case
if (reference_node != query_node || reference_index != query_index) {
- arma::Col<typename T1::elem_type> reference_point =
- references_.get_ref().unsafe_col(reference_index);
+ arma::vec reference_point = references_.unsafe_col(reference_index);
double distance = kernel_.Evaluate(query_point, reference_point);
@@ -170,8 +174,8 @@
/**
* The recursive function for dual tree
*/
-template<typename T1, typename Kernel, typename SortPolicy>
-void NeighborSearch<T1, Kernel, SortPolicy>::ComputeDualNeighborsRecursion_(
+template<typename Kernel, typename SortPolicy>
+void NeighborSearch<Kernel, SortPolicy>::ComputeDualNeighborsRecursion_(
TreeType* query_node,
TreeType* reference_node,
double lower_bound) {
@@ -284,10 +288,10 @@
} // ComputeDualNeighborsRecursion_
-template<typename T1, typename Kernel, typename SortPolicy>
-void NeighborSearch<T1, Kernel, SortPolicy>::ComputeSingleNeighborsRecursion_(
+template<typename Kernel, typename SortPolicy>
+void NeighborSearch<Kernel, SortPolicy>::ComputeSingleNeighborsRecursion_(
size_t point_id,
- arma::Col<typename T1::elem_type>& point,
+ arma::vec& point,
TreeType* reference_node,
double& best_dist_so_far) {
@@ -298,11 +302,9 @@
reference_index < reference_node->end(); reference_index++) {
// Confirm that points do not identify themselves as neighbors
// in the monochromatic case
- // SpMat does NOT currently implement memptr
- if (!(references_.get_ref().memptr() == queries_.get_ref().memptr() &&
+ if (!(references_.memptr() == queries_.memptr() &&
reference_index == point_id)) {
- arma::Col<typename T1::elem_type> reference_point =
- references_.get_ref().unsafe_col(reference_index);
+ arma::vec reference_point = references_.unsafe_col(reference_index);
double distance = kernel_.Evaluate(point, reference_point);
@@ -320,10 +322,10 @@
best_dist_so_far = neighbor_distances_(knns_ - 1, point_id);
} else {
// We'll order the computation by distance.
- double left_distance = SortPolicy::BestPointToNodeDistance
- (point, reference_node->left());
- double right_distance = SortPolicy::BestPointToNodeDistance
- (point, reference_node->right());
+ double left_distance = SortPolicy::BestPointToNodeDistance(point,
+ reference_node->left());
+ double right_distance = SortPolicy::BestPointToNodeDistance(point,
+ reference_node->right());
// Recurse in the best direction first.
if (SortPolicy::IsBetter(left_distance, right_distance)) {
@@ -359,12 +361,12 @@
* Computes the best neighbors and stores them in resulting_neighbors and
* distances.
*/
-template<typename T1, typename Kernel, typename SortPolicy>
-void NeighborSearch<T1, Kernel, SortPolicy>::ComputeNeighbors(
+template<typename Kernel, typename SortPolicy>
+void NeighborSearch<Kernel, SortPolicy>::ComputeNeighbors(
arma::Mat<size_t>& resulting_neighbors,
arma::mat& distances) {
- Timers::Start("neighbor_search/computing_neighbors");
+ Timers::StartTimer("neighbor_search/computing_neighbors");
if (naive_) {
// Run the base case computation on all nodes
if (query_tree_)
@@ -383,23 +385,19 @@
reference_tree_));
}
} else {
- size_t chunk = queries_.get_ref().n_cols / 10;
+ size_t chunk = queries_.n_cols / 10;
for(size_t i = 0; i < 10; i++) {
for(size_t j = 0; j < chunk; j++) {
- arma::Col<typename T1::elem_type> point =
- queries_.get_ref().unsafe_col(i * chunk + j);
-
+ arma::vec point = queries_.unsafe_col(i * chunk + j);
double best_dist_so_far = SortPolicy::WorstDistance();
ComputeSingleNeighborsRecursion_(i * chunk + j, point,
reference_tree_, best_dist_so_far);
}
}
- for(size_t i = 0; i < queries_.get_ref().n_cols % 10; i++) {
- size_t ind = (queries_.get_ref().n_cols / 10) * 10 + i;
- arma::Col<typename T1::elem_type> point =
- queries_.get_ref().unsafe_col(ind);
-
+ for(size_t i = 0; i < queries_.n_cols % 10; i++) {
+ size_t ind = (queries_.n_cols / 10) * 10 + i;
+ arma::vec point = queries_.unsafe_col(ind);
double best_dist_so_far = SortPolicy::WorstDistance();
ComputeSingleNeighborsRecursion_(ind, point, reference_tree_,
best_dist_so_far);
@@ -407,7 +405,7 @@
}
}
- Timers::Stop("neighbor_search/computing_neighbors");
+ Timers::StopTimer("neighbor_search/computing_neighbors");
// We need to initialize the results list before filling it
resulting_neighbors.set_size(neighbor_indices_.n_rows,
@@ -442,8 +440,8 @@
* @param neighbor Index of reference point which is being inserted.
* @param distance Distance from query point to reference point.
*/
-template<typename T1, typename Kernel, typename SortPolicy>
-void NeighborSearch<T1, Kernel, SortPolicy>::InsertNeighbor(size_t query_index,
+template<typename Kernel, typename SortPolicy>
+void NeighborSearch<Kernel, SortPolicy>::InsertNeighbor(size_t query_index,
size_t pos,
size_t neighbor,
double distance) {
Modified: mlpack/trunk/src/mlpack/methods/neighbor_search/sort_policies/furthest_neighbor_sort.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/neighbor_search/sort_policies/furthest_neighbor_sort.hpp 2011-11-17 19:30:24 UTC (rev 10318)
+++ mlpack/trunk/src/mlpack/methods/neighbor_search/sort_policies/furthest_neighbor_sort.hpp 2011-11-17 19:53:37 UTC (rev 10319)
@@ -64,8 +64,8 @@
* this is the maximum distance between the tree node and the point using the
* given distance function.
*/
- template<typename elem_type, typename TreeType>
- static double BestPointToNodeDistance(const arma::Col<elem_type>& query_point,
+ template<typename TreeType>
+ static double BestPointToNodeDistance(const arma::vec& query_point,
const TreeType* reference_node);
/**
Modified: mlpack/trunk/src/mlpack/methods/neighbor_search/sort_policies/furthest_neighbor_sort_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/neighbor_search/sort_policies/furthest_neighbor_sort_impl.hpp 2011-11-17 19:30:24 UTC (rev 10318)
+++ mlpack/trunk/src/mlpack/methods/neighbor_search/sort_policies/furthest_neighbor_sort_impl.hpp 2011-11-17 19:53:37 UTC (rev 10319)
@@ -22,9 +22,9 @@
return query_node->bound().MaxDistance(reference_node->bound());
}
-template<typename elem_type, typename TreeType>
+template<typename TreeType>
double FurthestNeighborSort::BestPointToNodeDistance(
- const arma::Col<elem_type>& point,
+ const arma::vec& point,
const TreeType* reference_node) {
// This is not implemented yet for the general case because the trees do not
// accept arbitrary distance metrics.
Modified: mlpack/trunk/src/mlpack/methods/neighbor_search/sort_policies/nearest_neighbor_sort.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/neighbor_search/sort_policies/nearest_neighbor_sort.hpp 2011-11-17 19:30:24 UTC (rev 10318)
+++ mlpack/trunk/src/mlpack/methods/neighbor_search/sort_policies/nearest_neighbor_sort.hpp 2011-11-17 19:53:37 UTC (rev 10319)
@@ -68,8 +68,8 @@
* this is the minimum distance between the tree node and the point using the
* given distance function.
*/
- template<typename elem_type, typename TreeType>
- static double BestPointToNodeDistance(const arma::Col<elem_type>& query_point,
+ template<typename TreeType>
+ static double BestPointToNodeDistance(const arma::vec& query_point,
const TreeType* reference_node);
/**
Modified: mlpack/trunk/src/mlpack/methods/neighbor_search/sort_policies/nearest_neighbor_sort_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/neighbor_search/sort_policies/nearest_neighbor_sort_impl.hpp 2011-11-17 19:30:24 UTC (rev 10318)
+++ mlpack/trunk/src/mlpack/methods/neighbor_search/sort_policies/nearest_neighbor_sort_impl.hpp 2011-11-17 19:53:37 UTC (rev 10319)
@@ -22,9 +22,9 @@
return query_node->bound().MinDistance(reference_node->bound());
}
-template<typename elem_type, typename TreeType>
+template<typename TreeType>
double NearestNeighborSort::BestPointToNodeDistance(
- const arma::Col<elem_type>& point,
+ const arma::vec& point,
const TreeType* reference_node) {
// This is not implemented yet for the general case because the trees do not
// accept arbitrary distance metrics.
Modified: mlpack/trunk/src/mlpack/methods/neighbor_search/typedef.h
===================================================================
--- mlpack/trunk/src/mlpack/methods/neighbor_search/typedef.h 2011-11-17 19:30:24 UTC (rev 10318)
+++ mlpack/trunk/src/mlpack/methods/neighbor_search/typedef.h 2011-11-17 19:53:37 UTC (rev 10319)
@@ -26,7 +26,7 @@
* neighbors. Squared distances are used because they are slightly faster than
* non-squared distances (they have one fewer call to sqrt()).
*/
-typedef NeighborSearch<arma::mat, kernel::SquaredEuclideanDistance, NearestNeighborSort>
+typedef NeighborSearch<kernel::SquaredEuclideanDistance, NearestNeighborSort>
AllkNN;
/**
@@ -35,7 +35,7 @@
* neighbors. Squared distances are used because they are slightly faster than
* non-squared distances (they have one fewer call to sqrt()).
*/
-typedef NeighborSearch<arma::mat, kernel::SquaredEuclideanDistance, FurthestNeighborSort>
+typedef NeighborSearch<kernel::SquaredEuclideanDistance, FurthestNeighborSort>
AllkFN;
}; // namespace neighbor
Modified: mlpack/trunk/src/mlpack/methods/nnsvm/nnsvm_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/nnsvm/nnsvm_impl.hpp 2011-11-17 19:30:24 UTC (rev 10318)
+++ mlpack/trunk/src/mlpack/methods/nnsvm/nnsvm_impl.hpp 2011-11-17 19:53:37 UTC (rev 10319)
@@ -62,9 +62,9 @@
nnsmo.Init(dataset, param_.c_, param_.b_, param_.eps_, param_.max_iter_);
/* 2-classes NNSVM training using NNSMO */
- Timers::Start("nnsvm/nnsvm_train");
+ Timers::StartTimer("nnsvm/nnsvm_train");
nnsmo.Train();
- Timers::Stop("nnsvm/nnsvm_train");
+ Timers::StopTimer("nnsvm/nnsvm_train");
/* Get the trained bi-class model */
nnsmo.GetNNSVM(support_vectors_, model_.sv_coef_, model_.w_);
Modified: mlpack/trunk/src/mlpack/methods/nnsvm/nnsvm_main.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/nnsvm/nnsvm_main.cpp 2011-11-17 19:30:24 UTC (rev 10318)
+++ mlpack/trunk/src/mlpack/methods/nnsvm/nnsvm_main.cpp 2011-11-17 19:53:37 UTC (rev 10319)
@@ -61,7 +61,7 @@
CLI::GetParam<double>("nnsvm/eps"),
CLI::GetParam<int>("nnsvm/max_iter"));
- Timers::Start("nnsvm/nnsvm_train");
+ Timers::StartTimer("nnsvm/nnsvm_train");
Log::Debug << "nnsvm_train_time" << CLI::GetParam<timeval>("nnsvm/nnsvm_train").tv_usec / 1e6 << std::endl;
/* training and testing, thus no need to load model from file */
if (mode=="train_test")
Modified: mlpack/trunk/src/mlpack/methods/svm/svm_impl.h
===================================================================
--- mlpack/trunk/src/mlpack/methods/svm/svm_impl.h 2011-11-17 19:30:24 UTC (rev 10318)
+++ mlpack/trunk/src/mlpack/methods/svm/svm_impl.h 2011-11-17 19:53:37 UTC (rev 10319)
@@ -162,9 +162,9 @@
/* Initialize kernel */
/* 2-classes SVM training using SMO */
- Timers::Start("svm/train_smo");
+ Timers::StartTimer("svm/train_smo");
smo.Train(learner_typeid, &dataset_bi);
- Timers::Stop("svm/train_smo");
+ Timers::StopTimer("svm/train_smo");
/* Get the trained bi-class model */
models_[ct].bias_ = smo.Bias(); /* bias */
Modified: mlpack/trunk/src/mlpack/tests/allkfn_test.cpp
===================================================================
--- mlpack/trunk/src/mlpack/tests/allkfn_test.cpp 2011-11-17 19:30:24 UTC (rev 10318)
+++ mlpack/trunk/src/mlpack/tests/allkfn_test.cpp 2011-11-17 19:53:37 UTC (rev 10319)
@@ -10,11 +10,6 @@
using namespace mlpack;
using namespace mlpack::neighbor;
-#define ELEM double
-#define CONTAINER arma::Mat<ELEM>
-
-typedef NeighborSearch<CONTAINER, kernel::SquaredEuclideanDistance, FurthestNeighborSort> bAllkFN;
-
BOOST_AUTO_TEST_SUITE(AllkFNTest);
/**
@@ -28,7 +23,7 @@
BOOST_AUTO_TEST_CASE(exhaustive_synthetic_test)
{
// Set up our data.
- CONTAINER data(1, 11);
+ arma::mat data(1, 11);
data[0] = 0.05; // Row addressing is unnecessary (they are all 0).
data[1] = 0.35;
data[2] = 0.15;
@@ -46,23 +41,21 @@
CLI::GetParam<int>("neighbor_search/k") = 10;
for (int i = 0; i < 3; i++)
{
- //AllkFN* allkfn;
- bAllkFN* allkfn;
-
- arma::Col<ELEM> data_mutable = data;
+ AllkFN* allkfn;
+ arma::mat data_mutable = data;
switch(i)
{
case 0: // Use the dual-tree method.
- allkfn = new bAllkFN(data_mutable);
+ allkfn = new AllkFN(data_mutable);
break;
case 1: // Use the single-tree method.
CLI::GetParam<bool>("neighbor_search/single_mode") = true;
- allkfn = new bAllkFN(data_mutable);
+ allkfn = new AllkFN(data_mutable);
break;
case 2: // Use the naive method.
CLI::GetParam<bool>("neighbor_search/single_mode") = false;
CLI::GetParam<bool>("neighbor_search/naive_mode") = true;
- allkfn = new bAllkFN(data_mutable);
+ allkfn = new AllkFN(data_mutable);
break;
}
More information about the mlpack-svn
mailing list