[mlpack-svn] r15824 - mlpack/trunk/src/mlpack/core/tree/cosine_tree

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Sat Sep 21 23:59:22 EDT 2013


Author: rcurtin
Date: Sat Sep 21 23:59:21 2013
New Revision: 15824

Log:
Remove EuclideanDistance() and CosineSimilarity() functions; instead, use
CosineDistance from kernels/.  Also remove trailing spaces (thanks, vim!).


Modified:
   mlpack/trunk/src/mlpack/core/tree/cosine_tree/cosine_tree_builder.hpp
   mlpack/trunk/src/mlpack/core/tree/cosine_tree/cosine_tree_builder_impl.hpp

Modified: mlpack/trunk/src/mlpack/core/tree/cosine_tree/cosine_tree_builder.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/tree/cosine_tree/cosine_tree_builder.hpp	(original)
+++ mlpack/trunk/src/mlpack/core/tree/cosine_tree/cosine_tree_builder.hpp	Sat Sep 21 23:59:21 2013
@@ -25,7 +25,7 @@
    * @param A Matrix for which probabilities are calculated
    * @param prob Reference to the probability vector
    */
-  void LSSampling(arma::mat A, arma::vec& prob); 
+  void LSSampling(arma::mat A, arma::vec& prob);
 
   /**
    * Calculates the centroid of the matrix
@@ -35,27 +35,12 @@
   arma::rowvec CalculateCentroid(arma::mat A) const;
 
   /**
-   * Calculates the Cosine Similarity between two vector
-   * 
-   * @param A Vector 1
-   * @param B Vector 2
-   */  
-  double CosineSimilarity(arma::vec A, arma::vec B);
-
-  /**
-   * Return the Euclidean Norm of the vectoe
-   * 
-   * @param A Vector for which Euclidean Norm has to be calculated
-   */
-  double EuclideanNorm(arma::vec A);
-  
-  /**
    * Calculates the Pivot for splitting
-   * 
+   *
    * @param prob Probability for a point to act as the pivot
    */
   size_t GetPivot(arma::vec prob);
-  
+
   /**
    * Splits the points into the root node into children nodes
    *
@@ -64,7 +49,7 @@
    * @param ARight Matrix for storing the points in Right Node
    * @param A All points
    */
-  void SplitData(std::vector<double> c, arma::mat& ALeft, 
+  void SplitData(std::vector<double> c, arma::mat& ALeft,
                  arma::mat& Aright, arma::mat A);
 
   /**
@@ -74,31 +59,31 @@
    * @param A All points
    * @param pivot pivot point
    */
-  void CreateCosineSimilarityArray(std::vector<double>& c, 
+  void CreateCosineSimilarityArray(std::vector<double>& c,
                                    arma::mat A, size_t pivot);
-  
+
   /**
    * Calculates Maximum Cosine Similarity
    *
    * @param c Array of Cosine Similarities
    */
   double GetMaxSimilarity(std::vector<double> c);
-   
+
   /**
    * Calculates Maximum Cosine Similarity
    *
    * @param c Array of Cosine Similarities
    */
   double GetMinSimilarity(std::vector<double> c);
- 
+
  public:
   //! Empty Constructor
   CosineTreeBuilder();
   //! Destructor
   ~CosineTreeBuilder();
- 
-  /** 
-   * Creates a new cosine tree node 
+
+  /**
+   * Creates a new cosine tree node
    *
    * @param A Data for constructing the node
    * @param root Reference to the constructed node
@@ -107,7 +92,7 @@
 
   /**
    * Splits a cosine tree node
-   * 
+   *
    * @param root Node to be split
    * @param right reference to the right child
    * @param left reference to the left child

Modified: mlpack/trunk/src/mlpack/core/tree/cosine_tree/cosine_tree_builder_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/tree/cosine_tree/cosine_tree_builder_impl.hpp	(original)
+++ mlpack/trunk/src/mlpack/core/tree/cosine_tree/cosine_tree_builder_impl.hpp	Sat Sep 21 23:59:21 2013
@@ -4,17 +4,18 @@
  *
  * Implementation of cosine tree builder.
  */
-
 #ifndef __MLPACK_CORE_TREE_COSINE_TREE_COSINE_TREE_BUILDER_IMPL_HPP
 #define __MLPACK_CORE_TREE_COSINE_TREE_COSINE_TREE_BUILDER_IMPL_HPP
 
 #include "cosine_tree_builder.hpp"
 
+#include <mlpack/core/kernels/cosine_distance.hpp>
+
 namespace mlpack {
 namespace tree {
 
 // Empty Constructor
-CosineTreeBuilder::CosineTreeBuilder() 
+CosineTreeBuilder::CosineTreeBuilder()
 {
   Log::Info<<"Constructor"<<std::endl;
 }
@@ -32,27 +33,6 @@
     probability(i) = arma::norm(A.row(i),"fro")/normA;
 }
 
-double CosineTreeBuilder::EuclideanNorm(arma::vec A)
-{
-   Log::Info<<"EuclideanNorm"<<std::endl;
-   //Calculating the Euclidean Norm
-   return sqrt(arma::sum(arma::square(A)));
-}
-
-double CosineTreeBuilder::CosineSimilarity(arma::vec A, arma::vec B)
-{
-  Log::Info<<"CosineSimilarity"<<std::endl;
-  //Value of the cosine
-  double value = arma::dot(A,B)/(EuclideanNorm(A)*EuclideanNorm(B));
-  //Stripping to account for floating point error
-  if (value > 1.0)
-    value = 1.0;
-  if (value < -1.0)
-    value = -1.0;
-  //Calculating angle
-  return acos(value);
-}
-
 arma::rowvec CosineTreeBuilder::CalculateCentroid(arma::mat A) const
 {
   Log::Info<<"CalculateCentroid"<<std::endl;
@@ -84,9 +64,9 @@
 {
   Log::Info<<"GetPivot"<<std::endl;
   //Setting firtst value as the pivot
-  double maxPivot=prob(0,0); 
+  double maxPivot=prob(0,0);
   size_t pivot=0;
- 
+
   //Searching for the pivot poitn
   for (size_t i=0;i<prob.n_rows;i++)
   {
@@ -99,7 +79,7 @@
   return pivot;
 }
 
-void CosineTreeBuilder::SplitData(std::vector<double> c, arma::mat& ALeft, 
+void CosineTreeBuilder::SplitData(std::vector<double> c, arma::mat& ALeft,
                                   arma::mat& ARight,arma::mat A)
 {
   Log::Info<<"SplitData"<<std::endl;
@@ -109,7 +89,7 @@
   cMax = GetMinSimilarity(c);
   //Couter for left and right
   size_t lft, rgt;
-  lft = 0; 
+  lft = 0;
   rgt = 0;
   //Splitting on the basis of nearness to the the high or low value
   for(size_t i=0;i<A.n_rows;i++)
@@ -127,19 +107,23 @@
   }
 }
 
-void CosineTreeBuilder::CreateCosineSimilarityArray(std::vector<double>& c, 
+void CosineTreeBuilder::CreateCosineSimilarityArray(std::vector<double>& c,
                                                     arma::mat A, size_t pivot)
 {
   Log::Info<<"CreateCosineSimilarityArray"<<std::endl;
-  for(size_t i=0;i<A.n_rows;i++)
-    c.push_back(CosineSimilarity(A.row(pivot).t(),A.row(i).t()));
+  for (size_t i = 0; i < A.n_rows; i++)
+  {
+    const double similarity =
+        kernel::CosineDistance::Evaluate(A.row(pivot), A.row(i));
+    c.push_back(similarity);
+  }
 }
 double CosineTreeBuilder::GetMinSimilarity(std::vector<double> c)
 {
   Log::Info<<"GetMinSimilarity"<<std::endl;
   double cMin = c[0];
   for(size_t i=1;i<c.size();i++)
-    if(cMin<c[i])  
+    if(cMin<c[i])
       cMin = c[i];
   return cMin;
 }
@@ -148,11 +132,11 @@
   Log::Info<<"GetMaxSimilarity"<<std::endl;
   double cMax = c[0];
   for(size_t i=1;i<c.size();i++)
-    if(cMax<c[i])  
+    if(cMax<c[i])
       cMax = c[i];
   return cMax;
 }
-void CosineTreeBuilder::CTNodeSplit(CosineTree& root, CosineTree& left, 
+void CosineTreeBuilder::CTNodeSplit(CosineTree& root, CosineTree& left,
                                     CosineTree& right)
 {
   //Extracting points from the root



More information about the mlpack-svn mailing list