FileDocCategorySizeDatePackage
StochasticLinearRanker.javaAPI DocAndroid 5.1 API6932Thu Mar 12 22:22:48 GMT 2015android.bordeaux.learning

StochasticLinearRanker

public class StochasticLinearRanker extends Object
Stochastic Linear Ranker, learns how to rank a sample. The learned rank score can be used to compare samples. This java class wraps the native StochasticLinearRanker class. To update the ranker, call updateClassifier with two samples, with the first one having higher rank than the second one. To get the rank score of the sample call scoreSample. TODO: adding more interfaces for changing the learning parameters

Fields Summary
String
TAG
public static int
VAR_NUM
private long
mNativeClassifier
Constructors Summary
public StochasticLinearRanker()
Initializing a ranker

    

            
      
        mNativeClassifier = initNativeClassifier();
    
Methods Summary
private native voiddeleteNativeClassifier(long classifierPtr)

protected voidfinalize()

        deleteNativeClassifier(mNativeClassifier);
    
public android.bordeaux.learning.StochasticLinearRanker$ModelgetUModel()
Get the current model and parameters of ranker

        Model slrModel = new Model();
        int len = nativeGetLengthClassifier(mNativeClassifier);
        String[] wKeys = new String[len];
        float[] wValues = new float[len];
        float wNormalizer = 1;
        nativeGetWeightClassifier(wKeys, wValues, wNormalizer, mNativeClassifier);
        slrModel.weightNormalizer = wNormalizer;
        for (int  i=0; i< wKeys.length ; i++)
            slrModel.weights.put(wKeys[i], wValues[i]);

        String[] paramKeys = new String[VAR_NUM];
        String[] paramValues = new String[VAR_NUM];
        nativeGetParameterClassifier(paramKeys, paramValues, mNativeClassifier);
        for (int  i=0; i< paramKeys.length ; i++)
            slrModel.parameters.put(paramKeys[i], paramValues[i]);
        return slrModel;
    
private native longinitNativeClassifier()

public booleanloadModel(android.bordeaux.learning.StochasticLinearRanker$Model model)
load the given model and parameters to the ranker

        String[] wKeys = new String[model.weights.size()];
        float[] wValues = new float[model.weights.size()];
        int i = 0 ;
        for (Map.Entry<String, Float> e : model.weights.entrySet()){
            wKeys[i] = e.getKey();
            wValues[i] = e.getValue();
            i++;
        }
        boolean res = setModelWeights(wKeys, wValues, model.weightNormalizer);
        if (!res)
            return false;

        for (Map.Entry<String, String> e : model.parameters.entrySet()){
            res = setModelParameter(e.getKey(), e.getValue());
            if (!res)
                return false;
        }
        return res;
    
private native intnativeGetLengthClassifier(long classifierPtr)

private native voidnativeGetParameterClassifier(java.lang.String[] keys, java.lang.String[] values, long classifierPtr)

private native voidnativeGetWeightClassifier(java.lang.String[] keys, float[] values, float normalizer, long classifierPtr)

private native floatnativeScoreSample(java.lang.String[] keys, float[] values, long classifierPtr)

private native booleannativeSetParameterClassifier(java.lang.String key, java.lang.String value, long classifierPtr)

private native booleannativeSetWeightClassifier(java.lang.String[] keys, float[] values, float normalizer, long classifierPtr)

private native booleannativeUpdateClassifier(java.lang.String[] keys_positive, float[] values_positive, java.lang.String[] keys_negative, float[] values_negative, long classifierPtr)

public voidprint(android.bordeaux.learning.StochasticLinearRanker$Model model)
Print a model for debugging

        String Sw = "";
        String Sp = "";
        for (Map.Entry<String, Float> e : model.weights.entrySet())
            Sw = Sw + "<" + e.getKey() + "," + e.getValue() + "> ";
        for (Map.Entry<String, String> e : model.parameters.entrySet())
            Sp = Sp + "<" + e.getKey() + "," + e.getValue() + "> ";
        Log.i(TAG, "Weights are " + Sw);
        Log.i(TAG, "Normalizer is " + model.weightNormalizer);
        Log.i(TAG, "Parameters are " + Sp);
    
public voidresetRanker()
Reset the ranker

        deleteNativeClassifier(mNativeClassifier);
        mNativeClassifier = initNativeClassifier();
    
public floatscoreSample(java.lang.String[] keys, float[] values)
Get the rank score of the sample, a sample is a list of key, value pairs.

        return nativeScoreSample(keys, values, mNativeClassifier);
    
public booleansetModelParameter(java.lang.String key, java.lang.String value)

        boolean res = nativeSetParameterClassifier(key, value, mNativeClassifier);
        return res;
    
public booleansetModelWeights(java.lang.String[] keys, float[] values, float normalizer)

        return nativeSetWeightClassifier(keys, values, normalizer, mNativeClassifier);
    
public booleanupdateClassifier(java.lang.String[] keys_positive, float[] values_positive, java.lang.String[] keys_negative, float[] values_negative)
Train the ranker with a pair of samples. A sample, a pair of arrays of keys and values. The first sample should have higher rank than the second one.

        return nativeUpdateClassifier(keys_positive, values_positive,
                                      keys_negative, values_negative,
                                      mNativeClassifier);