FileDocCategorySizeDatePackage
StochasticLinearRankerWithPrior.javaAPI DocAndroid 5.1 API8968Thu Mar 12 22:22:48 GMT 2015android.bordeaux.services

StochasticLinearRankerWithPrior

public class StochasticLinearRankerWithPrior extends android.bordeaux.learning.StochasticLinearRanker

Fields Summary
private final String
TAG
private final float
EPSILON
private final String
USE_PRIOR
private final String
SET_ALPHA
private final String
USE_AUTO_ALPHA
private final String
SET_FORGET_RATE
private final String
SET_MIN_TRAIN_PAIR
private final String
SET_USER_PERF
private final String
SET_PRIOR_PERF
private final String
SET_NUM_TRAIN_PAIR
private final String
SET_AUTO_ALPHA
private HashMap
mPriorWeights
private float
mAlpha
private float
mAutoAlpha
private float
mForgetRate
private float
mUserRankerPerf
private float
mPriorRankerPerf
private int
mMinReqTrainingPair
private int
mNumTrainPair
private boolean
mUsePrior
private boolean
mUseAutoAlpha
Constructors Summary
Methods Summary
public android.bordeaux.services.StochasticLinearRankerWithPrior$ModelgetModel()

        Model m = new Model();
        m.uModel = super.getUModel();
        m.priorWeights.putAll(mPriorWeights);
        m.priorParameters.put(SET_ALPHA, String.valueOf(mAlpha));
        m.priorParameters.put(SET_AUTO_ALPHA, String.valueOf(mAutoAlpha));
        m.priorParameters.put(SET_FORGET_RATE, String.valueOf(mForgetRate));
        m.priorParameters.put(SET_MIN_TRAIN_PAIR, String.valueOf(mMinReqTrainingPair));
        m.priorParameters.put(SET_USER_PERF, String.valueOf(mUserRankerPerf));
        m.priorParameters.put(SET_PRIOR_PERF, String.valueOf(mPriorRankerPerf));
        m.priorParameters.put(SET_NUM_TRAIN_PAIR, String.valueOf(mNumTrainPair));
        m.priorParameters.put(USE_AUTO_ALPHA, String.valueOf(mUseAutoAlpha));
        m.priorParameters.put(USE_PRIOR, String.valueOf(mUsePrior));
        return m;
    
public booleanloadModel(android.bordeaux.services.StochasticLinearRankerWithPrior$Model m)

        mPriorWeights.clear();
        mPriorWeights.putAll(m.priorWeights);
        for (Map.Entry<String, String> e : m.priorParameters.entrySet()) {
            boolean res = setModelParameter(e.getKey(), e.getValue());
            if (!res) return false;
        }
        return super.loadModel(m.uModel);
    
public voidprint(android.bordeaux.services.StochasticLinearRankerWithPrior$Model m)

        super.print(m.uModel);
        String Spw = "";
        for (Map.Entry<String, Float> e : m.priorWeights.entrySet())
            Spw = Spw + "<" + e.getKey() + "," + e.getValue() + "> ";
        Log.i(TAG, "Prior model is " + Spw);
        String Spp = "";
        for (Map.Entry<String, String> e : m.priorParameters.entrySet())
            Spp = Spp + "<" + e.getKey() + "," + e.getValue() + "> ";
        Log.i(TAG, "Prior parameters are " + Spp);
    
public floatpriorScoreSample(java.lang.String[] keys, float[] values)

        float score = 0;
        for (int i=0; i< keys.length; i++){
            if (mPriorWeights.get(keys[i]) != null )
                score = score + mPriorWeights.get(keys[i]) * values[i];
        }
        return score;
    
public voidresetRanker()

    

    
      
        super.resetRanker();
        mPriorWeights.clear();
        mAlpha = 0;
        mAutoAlpha = 0;
        mForgetRate = 0;
        mMinReqTrainingPair = 0;
        mUserRankerPerf = 0;
        mPriorRankerPerf = 0;
        mNumTrainPair = 0;
        mUsePrior = false;
        mUseAutoAlpha = false;
    
public floatscoreSample(java.lang.String[] keys, float[] values)

        if (!mUsePrior){
            return super.scoreSample(keys, values);
        } else {
            if (mUseAutoAlpha) {
                if (mNumTrainPair > mMinReqTrainingPair)
                    return (1 - mAutoAlpha) * super.scoreSample(keys,values) +
                            mAutoAlpha * priorScoreSample(keys,values);
                else
                    return priorScoreSample(keys,values);
            } else
                return (1 - mAlpha) * super.scoreSample(keys,values) +
                        mAlpha * priorScoreSample(keys,values);
        }
    
public booleansetModelParameter(java.lang.String key, java.lang.String value)

        if (key.equals(USE_AUTO_ALPHA)){
            mUseAutoAlpha = Boolean.parseBoolean(value);
        } else if (key.equals(USE_PRIOR)){
            mUsePrior = Boolean.parseBoolean(value);
        } else if (key.equals(SET_ALPHA)){
            mAlpha = Float.valueOf(value.trim()).floatValue();
        }else if (key.equals(SET_AUTO_ALPHA)){
            mAutoAlpha = Float.valueOf(value.trim()).floatValue();
        }else if (key.equals(SET_FORGET_RATE)){
            mForgetRate = Float.valueOf(value.trim()).floatValue();
        }else if (key.equals(SET_MIN_TRAIN_PAIR)){
            mMinReqTrainingPair = (int) Float.valueOf(value.trim()).floatValue();
        }else if (key.equals(SET_USER_PERF)){
            mUserRankerPerf = Float.valueOf(value.trim()).floatValue();
        }else if (key.equals(SET_PRIOR_PERF)){
            mPriorRankerPerf = Float.valueOf(value.trim()).floatValue();
        }else if (key.equals(SET_NUM_TRAIN_PAIR)){
            mNumTrainPair = (int) Float.valueOf(value.trim()).floatValue();
        }else
            return super.setModelParameter(key, value);
        return true;
    
public booleansetModelPriorWeights(java.util.HashMap pw)

        mPriorWeights.clear();
        mPriorWeights.putAll(pw);
        return true;
    
voidupdateAutoAlpha(java.lang.String[] keys_positive, float[] values_positive, java.lang.String[] keys_negative, float[] values_negative)

        float positiveUserScore = super.scoreSample(keys_positive, values_positive);
        float negativeUserScore = super.scoreSample(keys_negative, values_negative);
        float positivePriorScore = priorScoreSample(keys_positive, values_positive);
        float negativePriorScore = priorScoreSample(keys_negative, values_negative);
        float userDecision = 0;
        float priorDecision = 0;
        if (positiveUserScore > negativeUserScore)
            userDecision = 1;
        if (positivePriorScore > negativePriorScore)
            priorDecision = 1;
        mUserRankerPerf = (1 - mForgetRate) * mUserRankerPerf + userDecision;
        mPriorRankerPerf = (1 - mForgetRate) * mPriorRankerPerf + priorDecision;
        mAutoAlpha = (mPriorRankerPerf + EPSILON) / (mUserRankerPerf + mPriorRankerPerf + EPSILON);
    
public booleanupdateClassifier(java.lang.String[] keys_positive, float[] values_positive, java.lang.String[] keys_negative, float[] values_negative)

        if (mUsePrior && mUseAutoAlpha && (mNumTrainPair > mMinReqTrainingPair))
            updateAutoAlpha(keys_positive, values_positive, keys_negative, values_negative);
        mNumTrainPair ++;
        return super.updateClassifier(keys_positive, values_positive,
                                      keys_negative, values_negative);