Example usage for com.google.common.primitives Floats isFinite

List of usage examples for com.google.common.primitives Floats isFinite

Introduction

In this page you can find the example usage for com.google.common.primitives Floats isFinite.

Prototype

public static boolean isFinite(float value) 

Source Link

Document

Returns true if value represents a real number.

Usage

From source file:com.cloudera.oryx.common.LangUtils.java

/**
 * Parses a {@code float} from a {@link String} as if by {@link Float#valueOf(String)}, but disallows special
 * values like {@link Float#NaN}, {@link Float#POSITIVE_INFINITY} and {@link Float#NEGATIVE_INFINITY}.
 *
 * @param s {@link String} to parse//from  w  w  w.j  av a  2s .  c  om
 * @return floating-point value in the {@link String}
 * @throws NumberFormatException if input does not parse as a floating-point value
 * @throws IllegalArgumentException if input is infinite or {@link Float#NaN}
 * @see #parseDouble(String)
 */
public static float parseFloat(String s) {
    float value = Float.parseFloat(s);
    Preconditions.checkArgument(Floats.isFinite(value), "Bad value: %s", value);
    return value;
}

From source file:com.cloudera.oryx.als.serving.RecommendIterator.java

@Override
public NumericIDValue next() {
    LongObjectMap.MapEntry<float[]> entry = Yiterator.next();
    long itemID = entry.getKey();

    LongSet theKnownItemIDs = knownItemIDs;
    if (theKnownItemIDs != null) {
        synchronized (theKnownItemIDs) {
            if (theKnownItemIDs.contains(itemID)) {
                return null;
            }/* ww w.ja v a2 s .c  om*/
        }
    }

    Rescorer rescorer = this.rescorer;
    if (rescorer != null && rescorer.isFiltered(idMapping.toString(itemID))) {
        return null;
    }

    float[] itemFeatures = entry.getValue();
    double sum = 0.0;
    int count = 0;
    for (float[] oneUserFeatures : features) {
        sum += SimpleVectorMath.dot(itemFeatures, oneUserFeatures);
        count++;
    }

    if (rescorer != null) {
        sum = rescorer.rescore(idMapping.toString(itemID), sum);
        if (!Doubles.isFinite(sum)) {
            return null;
        }
    }

    float result = (float) (sum / count);
    Preconditions.checkState(Floats.isFinite(result), "Bad recommendation value");
    delegate.set(itemID, result);
    return delegate;
}

From source file:com.cloudera.oryx.als.serving.MostSimilarItemIterator.java

@Override
public NumericIDValue next() {
    LongObjectMap.MapEntry<float[]> entry = Yiterator.next();
    long itemID = entry.getKey();

    for (long l : toItemIDs) {
        if (l == itemID) {
            return null;
        }//from  w  w w .j  a  v a2  s .  c om
    }

    PairRescorer rescorer1 = this.rescorer;
    float[] candidateFeatures = entry.getValue();
    double candidateFeaturesNorm = SimpleVectorMath.norm(candidateFeatures);
    double total = 0.0;

    int length = itemFeatures.length;
    for (int i = 0; i < length; i++) {
        long toItemID = toItemIDs[i];
        if (rescorer1 != null
                && rescorer1.isFiltered(idMapping.toString(itemID), idMapping.toString(toItemID))) {
            return null;
        }
        double similarity = SimpleVectorMath.dot(candidateFeatures, itemFeatures[i])
                / (candidateFeaturesNorm * itemFeatureNorms[i]);
        if (!Doubles.isFinite(similarity)) {
            return null;
        }
        if (rescorer1 != null) {
            similarity = rescorer1.rescore(idMapping.toString(itemID), idMapping.toString(toItemID),
                    similarity);
            if (!Doubles.isFinite(similarity)) {
                return null;
            }
        }
        total += similarity;
    }

    float result = (float) (total / length);
    Preconditions.checkState(Floats.isFinite(result), "Bad similarity value");
    delegate.set(itemID, result);
    return delegate;
}

From source file:com.cloudera.oryx.als.serving.ServerRecommender.java

@Override
public float[] estimatePreferences(String userID, String... itemIDs) throws NotReadyException {

    Generation generation = getCurrentGeneration();
    LongObjectMap<float[]> X = generation.getX();

    float[] userFeatures;
    Lock xLock = generation.getXLock().readLock();
    xLock.lock();//from ww  w  . java2  s .  c  o  m
    try {
        userFeatures = X.get(StringLongMapping.toLong(userID));
    } finally {
        xLock.unlock();
    }
    if (userFeatures == null) {
        return new float[itemIDs.length]; // All 0.0f
    }

    LongObjectMap<float[]> Y = generation.getY();

    Lock yLock = generation.getYLock().readLock();
    yLock.lock();
    try {
        float[] result = new float[itemIDs.length];
        for (int i = 0; i < itemIDs.length; i++) {
            String itemID = itemIDs[i];
            float[] itemFeatures = Y.get(StringLongMapping.toLong(itemID));
            if (itemFeatures != null) {
                float value = (float) SimpleVectorMath.dot(itemFeatures, userFeatures);
                Preconditions.checkState(Floats.isFinite(value), "Bad estimate");
                result[i] = value;
            } // else leave value at 0.0f
        }
        return result;
    } finally {
        yLock.unlock();
    }
}