Package: aghasher
aghasherAn implementation of the Anchor Graph Hashing algorithm (AGH-1), presented inHashing with Graphs(Liu et al. 2011).Dependenciesaghashersupports Python 2.7 and Python 3, with numpy and scipy. These should be linked with a BLAS implementation
(e.g., OpenBLAS, ATLAS, Intel MKL). Without being linked to BLAS, numpy/scipy will use a fallback that causes
PyAnchorGraphHasher to run over 50x slower.Installationaghasheris available on PyPI, the Python Package Index.$pipinstallaghasherHow To UseTo use aghasher, first import theaghashermodule.import aghasherTraining a ModelAn AnchorGraphHasher is constructed using thetrainmethod, which returns an AnchorGraphHasher and the hash bit
embedding for the training data.agh, H_train = aghasher.AnchorGraphHasher.train(X, anchors, num_bits, nn_anchors, sigma)AnchorGraphHasher.train takes 5 arguments:XAnn-by-dnumpy.ndarray with training data. The rows correspond tonobservations, and the columns
correspond toddimensions.anchorsAnm-by-dnumpy.ndarray with anchors.mis the total number of anchors. Rows correspond to anchors,
and columns correspond to dimensions. The dimensionality of the anchors much match the dimensionality of the training
data.num_bits(optional; defaults to 12) Number of hash bits for the embedding.nn_anchors(optional; defaults to 2) Number of nearest anchors that are used for approximating the neighborhood
structure.sigma(optional; defaults toNone) sigma for the Gaussian radial basis function that is used to determine
similarity between points. When sigma is specified asNone, the code will automatically set a value, depending on
the training data and anchors.Hashing Data with an AnchorGraphHasher ModelWith an AnchorGraphHasher object, which has variable nameaghin the preceding and following examples, hashing
out-of-sample data is done with the object'shashmethod.agh.hash(X)The hash method takes one argument:XAnn-by-dnumpy.ndarray with data. The rows correspond tonobservations, and the columns correspond toddimensions. The dimensionality of the data much match the dimensionality of the training data used to train the
AnchorGraphHasher.Since Python does not have a native bit vector data structure, the hash method returns ann-by-rnumpy.ndarray, wherenis the number of observations indata, andris the number of hash bits specified when the model was trained.
The elements of the returned array are boolean values that correspond to bits.Testing an AnchorGraphHasher ModelTesting is performed with the AnchorGraphHasher.test method.precision = AnchorGraphHasher.test(H_train, H_test, y_train, y_test, radius)AnchorGraphHasher.test takes 5 arguments:H_trainAnn-by-rnumpy.ndarray with the hash bit embedding corresponding to the training data. The rows
correspond to thenobservations, and the columns correspond to therhash bits.H_testAnm-by-rnumpy.ndarray with the hash bit embedding corresponding to the testing data. The rows
correspond to themobservations, and the columns correspond to therhash bits.y_trainAnn-by-1numpy.ndarray with the ground truth labels for the training data.y_testAnm-by-1numpy.ndarray with the ground truth labels for the testing data.radius(optional; defaults to 2) Hamming radius to use for calculating precision.TestsTests are intests/.# Run tests$python3-munittestdiscovertests-vDifferences from the Matlab Reference ImplementationThe code is structured differently than the Matlab reference implementation.The Matlab code implements an additional hashing method, hierarchical hashing (referred to as 2-AGH), an extension of
1-AGH that is not implemented here.There is one functional difference relative to the Matlab code. Ifsigmais specified (as opposed to being
auto-estimated), then for the same value ofsigma, the Matlab and Python code will produce different results. They
will produce the same results when the Matlabsigmais sqrt(2) times bigger than the manually specifiedsigmain the
Python code. This is because in the Gaussian RBF kernel, the Python code uses a 2 in the denominator of the exponent,
and the Matlab code does not. A 2 was included in the denominator of the Python code, as that is the canonical way to
use an RBF kernel.Licenseaghasherhas anMIT License.SeeLICENSE.ReferencesLiu, Wei, Jun Wang, Sanjiv Kumar, and Shih-Fu Chang. 2011. “Hashing with Graphs.” In Proceedings of the 28th
International Conference on Machine Learning (ICML-11), edited by Lise Getoor and Tobias Scheffer, 1–8. ICML ’11. New
York, NY, USA: ACM.