Search in sources :

Example 1 with Function

use of com.airbnb.aerosolve.core.function.Function in project aerosolve by airbnb.

the class AdditiveModel method generateFeatureIndexer.

/**
   * Generate the feature indexer which maps each feature to a unique integer index
   *
   * @apiNote Subsequent `generateWeightVector` calls will use this index. This index does not
   * automatically update when features are added or removed.
   */
public AdditiveModel generateFeatureIndexer() {
    featureIndexer.clear();
    int count = 0;
    for (Map.Entry<String, Map<String, Function>> family : weights.entrySet()) {
        String familyName = family.getKey();
        Map<String, Integer> featureIndex = new HashMap<>();
        featureIndexer.put(familyName, featureIndex);
        for (Map.Entry<String, Function> feature : family.getValue().entrySet()) {
            featureIndex.put(feature.getKey(), count++);
        }
    }
    // (re)initialize the weight vector to be populated
    weightVector = new Function[count];
    return this;
}
Also used : AbstractFunction(com.airbnb.aerosolve.core.function.AbstractFunction) Function(com.airbnb.aerosolve.core.function.Function) HashMap(java.util.HashMap) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) Map(java.util.Map) SparseLabeledPoint(com.airbnb.aerosolve.core.features.SparseLabeledPoint)

Example 2 with Function

use of com.airbnb.aerosolve.core.function.Function in project aerosolve by airbnb.

the class AdditiveModel method update.

public void update(float gradWithLearningRate, SparseLabeledPoint point, double dropout, Random rand) {
    for (int i = 0; i < point.indices.length; i++) {
        if (dropout <= 0 || rand.nextDouble() > dropout) {
            int index = point.indices[i];
            Function function = weightVector[index];
            float value = point.values[i];
            function.update(-gradWithLearningRate, value);
        }
    }
    for (int i = 0; i < point.denseIndices.length; i++) {
        if (dropout <= 0 || rand.nextDouble() > dropout) {
            int index = point.denseIndices[i];
            Function function = weightVector[index];
            float[] value = point.denseValues[i];
            function.update(-gradWithLearningRate, value);
        }
    }
}
Also used : AbstractFunction(com.airbnb.aerosolve.core.function.AbstractFunction) Function(com.airbnb.aerosolve.core.function.Function) SparseLabeledPoint(com.airbnb.aerosolve.core.features.SparseLabeledPoint)

Example 3 with Function

use of com.airbnb.aerosolve.core.function.Function in project aerosolve by airbnb.

the class AdditiveModelTest method makeAdditiveModel.

AdditiveModel makeAdditiveModel() {
    AdditiveModel model = new AdditiveModel();
    Map<String, Map<String, Function>> weights = new HashMap<>();
    Map<String, Function> innerSplineFloat = new HashMap<String, Function>();
    Map<String, Function> innerLinearFloat = new HashMap<String, Function>();
    Map<String, Function> innerSplineString = new HashMap<String, Function>();
    Map<String, Function> innerLinearString = new HashMap<String, Function>();
    weights.put("spline_float", innerSplineFloat);
    weights.put("linear_float", innerLinearFloat);
    weights.put("spline_string", innerSplineString);
    weights.put("linear_string", innerLinearString);
    float[] ws = { 5.0f, 10.0f, -20.0f };
    innerSplineFloat.put("aaa", new Spline(1.0f, 3.0f, ws));
    // for string feature, only the first element in weight is meaningful.
    innerSplineString.put("bbb", new Spline(1.0f, 2.0f, ws));
    float[] wl = { 1.0f, 2.0f };
    innerLinearFloat.put("ccc", new Linear(-10.0f, 5.0f, wl));
    innerLinearString.put("ddd", new Linear(1.0f, 1.0f, wl));
    model.setWeights(weights);
    model.setOffset(0.5f);
    model.setSlope(1.5f);
    return model;
}
Also used : Function(com.airbnb.aerosolve.core.function.Function) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) Spline(com.airbnb.aerosolve.core.function.Spline) Linear(com.airbnb.aerosolve.core.function.Linear)

Example 4 with Function

use of com.airbnb.aerosolve.core.function.Function in project aerosolve by airbnb.

the class AdditiveModelTest method testAddFunction.

@Test
public void testAddFunction() {
    AdditiveModel model = makeAdditiveModel();
    // add an existing feature without overwrite
    model.addFunction("spline_float", "aaa", new Spline(2.0f, 10.0f, 5), false);
    // add an existing feature with overwrite
    model.addFunction("linear_float", "ccc", new Linear(3.0f, 5.0f), true);
    // add a new feature
    model.addFunction("spline_float", "new", new Spline(2.0f, 10.0f, 5), false);
    Map<String, Map<String, Function>> weights = model.getWeights();
    for (Map.Entry<String, Map<String, Function>> featureFamily : weights.entrySet()) {
        String familyName = featureFamily.getKey();
        Map<String, Function> features = featureFamily.getValue();
        for (Map.Entry<String, Function> feature : features.entrySet()) {
            String featureName = feature.getKey();
            Function func = feature.getValue();
            if (familyName.equals("spline_float")) {
                Spline spline = (Spline) func;
                if (featureName.equals("aaa")) {
                    assertTrue(spline.getMaxVal() == 3.0f);
                    assertTrue(spline.getMinVal() == 1.0f);
                    assertTrue(spline.getWeights().length == 3);
                } else if (featureName.equals("new")) {
                    assertTrue(spline.getMaxVal() == 10.0f);
                    assertTrue(spline.getMinVal() == 2.0f);
                    assertTrue(spline.getWeights().length == 5);
                }
            } else if (familyName.equals("linear_float") && featureName.equals("ccc")) {
                Linear linear = (Linear) func;
                assertTrue(linear.getWeights().length == 2);
                assertTrue(linear.getWeights()[0] == 0.0f);
                assertTrue(linear.getWeights()[1] == 0.0f);
                assertTrue(linear.getMinVal() == 3.0f);
                assertTrue(linear.getMaxVal() == 5.0f);
            }
        }
    }
}
Also used : Function(com.airbnb.aerosolve.core.function.Function) HashMap(java.util.HashMap) Map(java.util.Map) Spline(com.airbnb.aerosolve.core.function.Spline) Linear(com.airbnb.aerosolve.core.function.Linear) Test(org.junit.Test)

Example 5 with Function

use of com.airbnb.aerosolve.core.function.Function in project aerosolve by airbnb.

the class AdditiveModel method scoreFlatFeatures.

public float scoreFlatFeatures(Map<String, Map<String, Double>> flatFeatures) {
    float sum = 0.0f;
    for (Map.Entry<String, Map<String, Double>> featureFamily : flatFeatures.entrySet()) {
        Map<String, Function> familyWeightMap = weights.get(featureFamily.getKey());
        if (familyWeightMap == null) {
            // not important families/features are removed from model
            log.debug("miss featureFamily {}", featureFamily.getKey());
            continue;
        }
        for (Map.Entry<String, Double> feature : featureFamily.getValue().entrySet()) {
            Function func = familyWeightMap.get(feature.getKey());
            if (func == null)
                continue;
            float val = feature.getValue().floatValue();
            sum += func.evaluate(val);
        }
    }
    return sum;
}
Also used : AbstractFunction(com.airbnb.aerosolve.core.function.AbstractFunction) Function(com.airbnb.aerosolve.core.function.Function) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) Map(java.util.Map)

Aggregations

Function (com.airbnb.aerosolve.core.function.Function)11 AbstractFunction (com.airbnb.aerosolve.core.function.AbstractFunction)9 HashMap (java.util.HashMap)8 Map (java.util.Map)8 AbstractMap (java.util.AbstractMap)6 SparseLabeledPoint (com.airbnb.aerosolve.core.features.SparseLabeledPoint)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ModelRecord (com.airbnb.aerosolve.core.ModelRecord)2 Linear (com.airbnb.aerosolve.core.function.Linear)2 Spline (com.airbnb.aerosolve.core.function.Spline)2 DebugScoreRecord (com.airbnb.aerosolve.core.DebugScoreRecord)1 ModelHeader (com.airbnb.aerosolve.core.ModelHeader)1 FunctionUtil.toFloat (com.airbnb.aerosolve.core.function.FunctionUtil.toFloat)1 PriorityQueue (java.util.PriorityQueue)1 Test (org.junit.Test)1