.TH "fmkstutorial" 3 "Tue Sep 9 2014" "Version 1.0.10" "MLPACK" \" -*- nroff -*- .ad l .nh .SH NAME fmkstutorial \- Fast max-kernel search tutorial (fastmks) .SH "Introduction" .PP The FastMKS algorithm (fast exact max-kernel search) is a recent algorithm proposed in the following paper: .PP .PP .nf @inproceedings{curtin2013fast, title={Fast Exact Max-Kernel Search}, author={Curtin, Ryan R\&. and Ram, Parikshit and Gray, Alexander G\&.}, booktitle={Proceedings of the 2013 SIAM International Conference on Data Mining (SDM '13)}, year={2013}, pages={1--9} } .fi .PP .PP Given a set of query points $Q$ and a set of reference points $R$, the FastMKS algorithm is a fast dual-tree (or single-tree) algorithm which finds .PP \[ \arg\max_{p_r \in R} K(p_q, p_r) \].PP for all points $p_q \in Q$ and for some Mercer kernel $K(\cdot, \cdot)$\&. A Mercer kernel is a kernel that is positive semidefinite; these are the classes of kernels that can be used with the kernel trick\&. In short, the positive semidefiniteness of a Mercer kernel means that any kernel matrix (or Gram matrix) created on a dataset must be positive semidefinite\&. .PP The FastMKS algorithm builds trees on the datasets $Q$ and $R$ in such a way that explicit representation of the points in the kernel space is unnecessary, by using cover trees (\fBmlpack::tree::CoverTree\fP)\&. This allows the algorithm to be run, for instance, on string kernels, where there is no sensible explicit representation\&. The \fBmlpack\fP implementation allows any type of tree that does not require an explicit representation to be used\&. For more details, see the paper\&. .PP At the time of this writing there is no other fast algorithm for exact max-kernel search\&. Also, \fBmlpack\fP implements dual-tree FastMKS, while the paper referenced above only explains single-tree search\&. .PP \fBmlpack\fP provides: .PP .IP "\(bu" 2 a \fBsimple command-line executable\fP to run FastMKS .IP "\(bu" 2 a \fBC++ interface\fP to run FastMKS .PP .SH "Table of Contents" .PP A list of all the sections this tutorial contains\&. .PP .IP "\(bu" 2 \fBIntroduction\fP .IP "\(bu" 2 \fBTable of Contents\fP .IP "\(bu" 2 \fBCommand-line FastMKS (fastmks)\fP .IP " \(bu" 4 \fBFastMKS with a linear kernel on one dataset\fP .IP " \(bu" 4 \fBFastMKS on a reference and query dataset\fP .IP " \(bu" 4 \fBFastMKS with a different kernel\fP .IP " \(bu" 4 \fBUsing single-tree search or naive search\fP .IP " \(bu" 4 \fBParamters for alternate kernels\fP .PP .IP "\(bu" 2 \fBThe 'FastMKS' class\fP .IP " \(bu" 4 \fBFastMKS on one dataset\fP .IP " \(bu" 4 \fBFastMKS with a query and reference dataset\fP .IP " \(bu" 4 \fBFastMKS with an initialized kernel\fP .IP " \(bu" 4 \fBFastMKS with an already-created tree\fP .PP .IP "\(bu" 2 \fBWriting a custom kernel for FastMKS\fP .IP "\(bu" 2 \fBUsing other tree types for FastMKS\fP .IP "\(bu" 2 \fBRunning FastMKS on objects\fP .IP "\(bu" 2 \fBFurther documentation\fP .PP .SH "Command-line FastMKS (fastmks)" .PP \fBmlpack\fP provides a command-line program, \fCfastmks\fP, which is used to perform FastMKS on a given query and reference dataset\&. It supports numerous different types of kernels: .PP .IP "\(bu" 2 \fBlinear kernel\fP .IP "\(bu" 2 \fBpolynomial kernel\fP .IP "\(bu" 2 \fBcosine distance\fP .IP "\(bu" 2 \fBGaussian kernel\fP .IP "\(bu" 2 \fBEpanechnikov kernel\fP .IP "\(bu" 2 \fBtriangular kernel\fP .IP "\(bu" 2 \fBhyperbolic tangent kernel\fP .IP "\(bu" 2 \fBLaplacian kernel\fP .PP .PP Note that when a shift-invariant kernel is used, the results will be the same as nearest neighbor search, so \fBallknn\fP may be a better option\&. A shift-invariant kernel is a kernel that depends only on the distance between the two input points\&. The \fBGaussian kernel\fP, \fBEpanechnikov kernel\fP, \fBtriangular kernel\fP, and \fBLaplacian kernel\fP are instances of shift-invariant kernels\&. The paper contains more details on this situation\&. The \fCfastmks\fP executable still provides these kernels as options, though\&. .PP The following examples detail usage of the \fCfastmks\fP program\&. Note that you can get documentation on all the possible parameters by typing: .PP .PP .nf $ fastmks --help .fi .PP .SS "FastMKS with a linear kernel on one dataset" If only one dataset is specified (with \fC-r\fP or \fC--reference_file\fP), the reference dataset is taken to be both the query and reference datasets\&. The example below finds the 4 maximum kernels of each point in dataset\&.csv, using the default linear kernel\&. .PP .PP .nf $ fastmks -r dataset\&.csv -k 4 -v -p products\&.csv -i indices\&.csv .fi .PP .PP When the operation completes, the values of the kernels are saved in products\&.csv and the indices of the points which give the maximum kernels are saved in indices\&.csv\&. .PP .PP .nf $ head indices\&.csv 762,910,863,890 762,910,426,568 910,762,863,426 762,910,863,426 863,910,614,762 762,863,910,614 762,910,488,568 762,910,863,426 910,762,863,426 863,762,910,614 .fi .PP .PP .PP .nf $ head products\&.csv 1\&.6221652894e+00,1\&.5998743443e+00,1\&.5898890769e+00,1\&.5406789753e+00 1\&.3387953449e+00,1\&.3317349486e+00,1\&.2966613184e+00,1\&.2774493620e+00 1\&.6386110476e+00,1\&.6332029753e+00,1\&.5952629124e+00,1\&.5887195330e+00 1\&.0917545803e+00,1\&.0820878726e+00,1\&.0668992636e+00,1\&.0419838050e+00 1\&.2272441028e+00,1\&.2169643942e+00,1\&.2104597963e+00,1\&.2067780154e+00 1\&.5720962456e+00,1\&.5618504956e+00,1\&.5609069923e+00,1\&.5235605095e+00 1\&.3655478674e+00,1\&.3548593212e+00,1\&.3311547298e+00,1\&.3250728881e+00 2\&.0119149744e+00,2\&.0043668067e+00,1\&.9847289214e+00,1\&.9298280046e+00 1\&.1586923205e+00,1\&.1494586097e+00,1\&.1274872962e+00,1\&.1248172766e+00 4\&.4789820372e-01,4\&.4618539778e-01,4\&.4200024852e-01,4\&.3989721792e-01 .fi .PP .PP We can see in this example that for point 0, the point with maximum kernel value is point 762, with a kernel value of 1\&.622165\&. For point 3, the point with third largest kernel value is point 863, with a kernel value of 1\&.0669\&. .SS "FastMKS on a reference and query dataset" The query points may be different than the reference points\&. To specify a different query set, the \fC-q\fP (or \fC--query_file\fP) option is used, as in the example below\&. .PP .PP .nf $ fastmks -q query_set\&.csv -r reference_set\&.csv -k 5 -i indices\&.csv -p products\&.csv .fi .PP .SS "FastMKS with a different kernel" The \fCfastmks\fP program offers more than just the linear kernel\&. Valid options are \fC'linear'\fP, \fC'polynomial'\fP, \fC'cosine'\fP, \fC'gaussian'\fP, \fC'epanechnikov'\fP, \fC'triangular'\fP, \fC'laplacian'\fP, and \fC'hyptan'\fP (the hyperbolic tangent kernel)\&. Note that the hyperbolic tangent kernel is provably not a Mercer kernel but is positive semidefinite on most datasets and is commonly used as a kernel\&. Note also that the Gaussian kernel and other shift-invariant kernels give the same results as nearest neighbor search (see \fBNeighborSearch tutorial (k-nearest-neighbors)\fP)\&. .PP The kernel to use is specified with the \fC-K\fP (or \fC--kernel\fP) option\&. The example below uses the cosine similarity as a kernel\&. .PP .PP .nf $ fastmks -r dataset\&.csv -k 5 -K cosine -i indices\&.csv -p products\&.csv -v .fi .PP .SS "Using single-tree search or naive search" In some cases, it may be useful to not use the dual-tree FastMKS algorithm\&. Instead you can specify the \fC--single\fP option, indicating that a tree should be built only on the reference set, and then the queries should be processed in a linear scan (instead of in a tree)\&. Alternately, the \fC-N\fP (or \fC--naive\fP) option makes the program not build trees at all and instead use brute-force search to find the solutions\&. .PP The example below uses single-tree search on two datasets\&. .PP .PP .nf $ fastmks -q query_set\&.csv -r reference_set\&.csv --single -k 5 -p products\&.csv \ > -i indices\&.csv .fi .PP .PP The example below uses naive search on one dataset\&. .PP .PP .nf $ fastmks -r reference_set\&.csv -k 5 -N -p products\&.csv -i indices\&.csv .fi .PP .SS "Paramters for alternate kernels" Many of the alternate kernel choices have parameters which can be chosen; these are detailed in this section\&. .PP .IP "\(bu" 2 \fB\fC-w\fP \fP(\fC--bandwidth\fP): this sets the bandwidth of the kernel, and is applicable to the \fC'gaussian'\fP, \fC'epanechnikov'\fP, and \fC'triangular'\fP kernels\&. This is the 'spread' of the kernel\&. .IP "\(bu" 2 \fB\fC-d\fP \fP(\fC--degree\fP): this sets the degree of the polynomial kernel (the power to which the result is raised)\&. It is only applicable to the \fC'polynomial'\fP kernel\&. .IP "\(bu" 2 \fB\fC-o\fP \fP(\fC--offset\fP): this sets the offset of the kernel, for the \fC'polynomial'\fP and \fC'hyptan'\fP kernel\&. See \fBthe polynomial kernel documentation\fP and \fBthe hyperbolic tangent kernel documentation\fP for more information\&. .IP "\(bu" 2 \fB\fC-s\fP \fP(\fC--scale\fP): this sets the scale of the kernel, and is only applicable to the \fC'hyptan'\fP kernel\&. See \fBthe hyperbolic tangent kernel documentation\fP for more information\&. .PP .SH "The 'FastMKS' class" .PP The \fCFastMKS<>\fP class offers a simple API for use within C++ applications, and allows further flexibility in kernel choice and tree type choice\&. However, \fCFastMKS<>\fP has no default template parameter for the kernel type -- that must be manually specified\&. Choices that \fBmlpack\fP provides include: .PP .IP "\(bu" 2 \fBmlpack::kernel::LinearKernel\fP .IP "\(bu" 2 \fBmlpack::kernel::PolynomialKernel\fP .IP "\(bu" 2 \fBmlpack::kernel::CosineDistance\fP .IP "\(bu" 2 \fBmlpack::kernel::GaussianKernel\fP .IP "\(bu" 2 \fBmlpack::kernel::EpanechnikovKernel\fP .IP "\(bu" 2 \fBmlpack::kernel::TriangularKernel\fP .IP "\(bu" 2 \fBmlpack::kernel::HyperbolicTangentKernel\fP .IP "\(bu" 2 \fBmlpack::kernel::LaplacianKernel\fP .IP "\(bu" 2 \fBmlpack::kernel::PSpectrumStringKernel\fP .PP .PP The following examples use kernels from that list\&. Writing your own kernel is detailed in \fBthe next section\fP\&. Remember that when you are using the C++ interface, the data matrices must be column-major\&. See \fBMatrices in MLPACK\fP for more information\&. .SS "FastMKS on one dataset" Given only a reference dataset, the following code will run FastMKS with k set to 5\&. .PP .PP .nf #include #include using namespace mlpack::fastmks; // The reference dataset, which is column-major\&. extern arma::mat data; // This will initialize the FastMKS object with the linear kernel with default // options: K(x, y) = x^T y\&. The tree is built in the constructor\&. FastMKS f(data); // The results will be stored in these matrices\&. arma::Mat indices; arma::mat products; // Run FastMKS\&. f\&.Search(5, indices, products); .fi .PP .SS "FastMKS with a query and reference dataset" In this setting we have both a query and reference dataset\&. We search for 10 maximum kernels\&. .PP .PP .nf #include #include using namespace mlpack::fastmks; using namespace mlpack::kernel; // The reference and query datasets, which are column-major\&. extern arma::mat referenceData; extern arma::mat queryData; // This will initialize the FastMKS object with the triangular kernel with // default options (bandwidth of 1)\&. The trees are built in the constructor\&. FastMKS f(queryData, referenceData); // The results will be stored in these matrices\&. arma::Mat indices; arma::mat products; // Run FastMKS\&. f\&.Search(10, indices, products); .fi .PP .SS "FastMKS with an initialized kernel" Often, kernels have parameters which need to be specified\&. \fCFastMKS<>\fP has constructors which take initialized kernels\&. Note that temporary kernels cannot be passed as an argument\&. The example below initializes a \fCPolynomialKernel\fP object and then runs FastMKS with a query and reference dataset\&. .PP .PP .nf #include #include using namespace mlpack::fastmks; using namespace mlpack::kernel; // The reference and query datasets, which are column-major\&. extern arma::mat referenceData; extern arma::mat queryData; // Initialize the polynomial kernel with degree of 3 and offset of 2\&.5\&. PolynomialKernel pk(3\&.0, 2\&.5); // Create the FastMKS object with the initialized kernel\&. FastMKS f(referenceData, queryData, pk); // The results will be stored in these matrices\&. arma::Mat indices; arma::mat products; // Run FastMKS\&. f\&.Search(10, indices, products); .fi .PP .PP The syntax for running FastMKS with one dataset and an initialized kernel is very similar: .PP .PP .nf FastMKS f(referenceData, pk); .fi .PP .SS "FastMKS with an already-created tree" By default, \fCFastMKS<>\fP uses the cover tree datastructure (see \fBmlpack::tree::CoverTree\fP)\&. Sometimes, it is useful to modify the parameters of the cover tree\&. In this scenario, a tree must be built outside of the constructor, and then passed to the appropriate \fCFastMKS<>\fP constructor\&. An example on just a reference dataset is shown below, where the base of the cover tree is modified\&. .PP We also use an instantiated kernel, but because we are building our own tree, we must use \fBIPMetric\fP so that our tree is built on the metric induced by our kernel function\&. .PP .PP .nf #include #include // The reference dataset, which is column-major\&. extern arma::mat data; // Initialize the polynomial kernel with a degree of 4 and offset of 2\&.0\&. PolynomialKernel pk(4\&.0, 2\&.0); // Create the metric induced by this kernel (because a kernel is not a metric // and we can't build a tree on a kernel alone)\&. IPMetric metric(pk); // Now build a tree on the reference dataset using the instantiated metric and // the custom base of 1\&.5 (default is 1\&.3)\&. We have to be sure to use the right // type here -- FastMKS needs the FastMKSStat object as the tree's // StatisticType\&. typedef tree::CoverTree, tree::FirstPointIsRoot, FastMKSStat> TreeType; // Convenience typedef\&. TreeType* tree = new TreeType(data, metric, 1\&.5); // Now initialize FastMKS with that statistic\&. We don't need to specify the // TreeType template parameter since we are still using the default\&. We don't // need to pass the kernel because that is contained in the tree\&. FastMKS f(data, tree); // The results will be stored in these matrices\&. arma::Mat indices; arma::mat products; // Run FastMKS\&. f\&.Search(10, indices, products); .fi .PP .PP The syntax is similar for the case where different query and reference datasets are given; but trees for both need to be built in the manner specified above\&. Be sure to build both trees using the same metric (or at least a metric with the exact same parameters)\&. .PP .PP .nf FastMKS f(referenceData, referenceTree, queryData, queryTree); .fi .PP .SH "Writing a custom kernel for FastMKS" .PP While \fBmlpack\fP provides some number of kernels in the \fBmlpack::kernel\fP namespace, it is easy to create a custom kernel\&. To satisfy the KernelType policy, a class must implement the following methods: .PP .PP .nf // Empty constructor is required\&. KernelType(); // Evaluate the kernel between two points\&. template double Evaluate(const VecType& a, const VecType& b); .fi .PP .PP The template parameter \fCVecType\fP is helpful (but not necessary) so that the kernel can be used with both sparse and dense matrices (\fCarma::sp_mat\fP and \fCarma::mat\fP)\&. .SH "Using other tree types for FastMKS" .PP The use of the cover tree (see \fBCoverTree\fP) is not necessary for FastMKS, although it is the default tree type\&. A different type of tree can be specified with the TreeType template parameter\&. However, the tree type is required to have \fBFastMKSStat\fP as the StatisticType, and for FastMKS to work, the tree must be built only on kernel evaluations (or distance evaluations in the kernel space via \fBIPMetric::Evaluate()\fP)\&. .PP Below is an example where a custom tree class, \fCCustomTree\fP, is used as the tree type for FastMKS\&. In this example FastMKS is only run on one dataset\&. .PP .PP .nf #include #include "custom_tree\&.hpp" using namespace mlpack::fastmks; using namespace mlpack::tree; // The dataset that FastMKS will be run on\&. extern arma::mat data; // The custom tree type\&. We'll assume that the first template parameter is the // statistic type\&. typedef CustomTree TreeType; // The FastMKS constructor will create the tree\&. FastMKS f(data); // These will hold the results\&. arma::Mat indices; arma::mat products; // Run FastMKS\&. f\&.Search(5, indices, products); .fi .PP .SH "Running FastMKS on objects" .PP FastMKS has a lot of utility on objects which are not representable in some sort of metric space\&. These objects might be strings, graphs, models, or other objects\&. For these types of objects, questions based on distance don't really make sense\&. One good example is with strings\&. The question 'how far is 'dog' from 'Taki Inoue'?' simply doesn't make sense\&. We can't have a centroid of the terms 'Fritz', 'E28', and 'popsicle'\&. .PP However, what we can do is define some sort of kernel on these objects\&. These kernels generally correspond to some similarity measure, with one example being the p-spectrum string kernel (see \fBmlpack::kernel::PSpectrumStringKernel\fP)\&. Using that, we can say 'how similar is 'dog' to 'Taki Inoue'?' and get an actual numerical result by evaluating K('dog', 'Taki Inoue') (where K is our p-spectrum string kernel)\&. .PP The only requirement on these kernels is that they are positive definite kernels (or Mercer kernels)\&. For more information on those details, refer to the FastMKS paper\&. .PP Remember that FastMKS is a tree-based method\&. But trees like the binary space tree require centroids -- and as we said earlier, centroids often don't make sense with these types of objects\&. Therefore, we need a type of tree which is built \fBexclusively\fP on points in the dataset -- those are points which we can evaluate our kernel function on\&. The cover tree is one example of a type of tree satisfying this condition; its construction will only call the kernel function on two points that are in the dataset\&. .PP But, we have one more problem\&. The \fCCoverTree\fP class is built on \fCarma::mat\fP objects (dense matrices)\&. Our objects, however, are not necessarily representable in a column of a matrix\&. To use the example we have been using, strings cannot be represented easily in a matrix because they may all have different lengths\&. .PP The way to work around this problem is to create a 'fake' data matrix which simply holds indices to objects\&. A good example of how to do this is detailed in the documentation for the \fBPSpectrumStringKernel\fP\&. .PP In short, the trick is to make each data matrix one-dimensional and containing linear indices: .PP .PP .nf arma::mat data = "0 1 2 3 4 5 6 7 8"; .fi .PP .PP Then, when \fCEvaluate()\fP is called on the kernel function, the parameters will be two one-dimensional vectors that simply contain indices to objects\&. The example below details the process a little better: .PP .PP .nf // This function evaluates the kernel on two Objects (in this example, its // implementation is not important; the only important thing is that the // function exists)\&. double ObjectKernel::Evaluate(const Object& a, const Object& b) const; template double ObjectKernel::Evaluate(const VecType& a, const VecType& b) const { // Extract the indices from the vectors\&. const size_t indexA = size_t(a[0]); const size_t indexB = size_t(b[0]); // Assume that 'objects' is an array (or std::vector or other container) // holding Objects\&. const Object& objectA = objects[indexA]; const Object& objectB = objects[indexB]; // Now call the function that does the actual evaluation on the objects and // return its result\&. return Evaluate(objectA, objectB); } .fi .PP .PP As written earlier, the documentation for \fBPSpectrumStringKernel\fP is a good place to consult for further reference on this\&. That kernel uses two dimensional indices; one dimension represents the index of the string, and the other represents whether it is referring to the query set or the reference set\&. If your kernel is meant to work on separate query and reference sets, that strategy should be considered\&. .SH "Further documentation" .PP For further documentation on the FastMKS class, consult the \fBcomplete API documentation\fP\&.