Java Code Examples for org.nd4j.linalg.ops.transforms.Transforms#allCosineDistances()

The following examples show how to use org.nd4j.linalg.ops.transforms.Transforms#allCosineDistances() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RandomProjectionLSH.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public INDArray search(INDArray query, double maxRange) {
    if (maxRange < 0)
        throw new IllegalArgumentException("ANN search should have a positive maximum search radius");

    INDArray bucketData = bucketData(query);
    INDArray distances = Transforms.allCosineDistances(bucketData, query, -1);
    INDArray[] idxs = Nd4j.sortWithIndices(distances, -1, true);

    INDArray shuffleIndexes = idxs[0];
    INDArray sortedDistances = idxs[1];
    int accepted = 0;
    while (accepted < sortedDistances.length() && sortedDistances.getInt(accepted) <= maxRange) accepted +=1;

    INDArray res = Nd4j.create(new int[] {accepted, inDimension});
    for(int i = 0; i < accepted; i++){
        res.putRow(i, bucketData.getRow(shuffleIndexes.getInt(i)));
    }
    return res;
}
 
Example 2
Source File: RandomProjectionLSH.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public INDArray search(INDArray query, int k) {
    if (k < 1)
        throw new IllegalArgumentException("An ANN search for k neighbors should at least seek one neighbor");

    INDArray bucketData = bucketData(query);
    INDArray distances = Transforms.allCosineDistances(bucketData, query, -1);
    INDArray[] idxs = Nd4j.sortWithIndices(distances, -1, true);

    INDArray shuffleIndexes = idxs[0];
    INDArray sortedDistances = idxs[1];
    val accepted = Math.min(k, sortedDistances.shape()[1]);

    INDArray res = Nd4j.create(accepted, inDimension);
    for(int i = 0; i < accepted; i++){
        res.putRow(i, bucketData.getRow(shuffleIndexes.getInt(i)));
    }
    return res;
}