Search in sources :

Example 6 with Function

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

the class AdditiveModel method scoreFeatures.

public float scoreFeatures(SparseLabeledPoint point, double dropout, Random rand) {
    float prediction = 0;
    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];
            prediction += function.evaluate(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];
            prediction += function.evaluate(value);
        }
    }
    return prediction;
}
Also used : AbstractFunction(com.airbnb.aerosolve.core.function.AbstractFunction) Function(com.airbnb.aerosolve.core.function.Function) SparseLabeledPoint(com.airbnb.aerosolve.core.features.SparseLabeledPoint)

Example 7 with Function

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

the class AdditiveModel method debugScoreComponents.

@Override
public List<DebugScoreRecord> debugScoreComponents(FeatureVector combinedItem) {
    Map<String, Map<String, Double>> flatFeatures = Util.flattenFeature(combinedItem);
    List<DebugScoreRecord> scoreRecordsList = new ArrayList<>();
    for (Map.Entry<String, Map<String, Double>> featureFamily : flatFeatures.entrySet()) {
        Map<String, Function> familyWeightMap = weights.get(featureFamily.getKey());
        if (familyWeightMap == null)
            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();
            float weight = func.evaluate(val);
            DebugScoreRecord record = new DebugScoreRecord();
            record.setFeatureFamily(featureFamily.getKey());
            record.setFeatureName(feature.getKey());
            record.setFeatureValue(val);
            record.setFeatureWeight(weight);
            scoreRecordsList.add(record);
        }
    }
    Map<String, List<Double>> denseFeatures = combinedItem.getDenseFeatures();
    if (denseFeatures != null) {
        Map<String, Function> denseWeights = getOrCreateDenseWeights();
        for (Map.Entry<String, List<Double>> feature : denseFeatures.entrySet()) {
            String featureName = feature.getKey();
            Function fun = denseWeights.get(featureName);
            float[] val = toFloat(feature.getValue());
            float weight = fun.evaluate(val);
            DebugScoreRecord record = new DebugScoreRecord();
            record.setFeatureFamily(DENSE_FAMILY);
            record.setFeatureName(feature.getKey());
            record.setDenseFeatureValue(feature.getValue());
            record.setFeatureWeight(weight);
            scoreRecordsList.add(record);
        }
    }
    return scoreRecordsList;
}
Also used : DebugScoreRecord(com.airbnb.aerosolve.core.DebugScoreRecord) ArrayList(java.util.ArrayList) AbstractFunction(com.airbnb.aerosolve.core.function.AbstractFunction) Function(com.airbnb.aerosolve.core.function.Function) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) Map(java.util.Map)

Example 8 with Function

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

the class AdditiveModel method debugScoreItem.

@Override
public float debugScoreItem(FeatureVector combinedItem, StringBuilder builder) {
    Map<String, Map<String, Double>> flatFeatures = Util.flattenFeature(combinedItem);
    float sum = 0.0f;
    // order by the absolute value
    PriorityQueue<Map.Entry<String, Float>> scores = new PriorityQueue<>(100, new LinearModel.EntryComparator());
    for (Map.Entry<String, Map<String, Double>> featureFamily : flatFeatures.entrySet()) {
        Map<String, Function> familyWeightMap = weights.get(featureFamily.getKey());
        if (familyWeightMap == null)
            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();
            float subScore = func.evaluate(val);
            sum += subScore;
            String str = featureFamily.getKey() + ":" + feature.getKey() + "=" + val + " = " + subScore + "<br>\n";
            scores.add(new AbstractMap.SimpleEntry<>(str, subScore));
        }
    }
    Map<String, List<Double>> denseFeatures = combinedItem.getDenseFeatures();
    if (denseFeatures != null) {
        assert (denseWeights != null);
        for (Map.Entry<String, List<Double>> feature : denseFeatures.entrySet()) {
            String featureName = feature.getKey();
            Function fun = denseWeights.get(featureName);
            float[] val = toFloat(feature.getValue());
            float subScore = fun.evaluate(val);
            sum += subScore;
            String str = DENSE_FAMILY + ":" + featureName + "=" + val + " = " + subScore + "<br>\n";
            scores.add(new AbstractMap.SimpleEntry<>(str, subScore));
        }
    }
    final int MAX_COUNT = 100;
    builder.append("Top scores ===>\n");
    if (!scores.isEmpty()) {
        int count = 0;
        float subsum = 0.0f;
        while (!scores.isEmpty()) {
            Map.Entry<String, Float> entry = scores.poll();
            builder.append(entry.getKey());
            float val = entry.getValue();
            subsum += val;
            count = count + 1;
            if (count >= MAX_COUNT) {
                builder.append("Leftover = " + (sum - subsum) + '\n');
                break;
            }
        }
    }
    builder.append("Total = " + sum + '\n');
    return sum;
}
Also used : PriorityQueue(java.util.PriorityQueue) SparseLabeledPoint(com.airbnb.aerosolve.core.features.SparseLabeledPoint) AbstractMap(java.util.AbstractMap) AbstractFunction(com.airbnb.aerosolve.core.function.AbstractFunction) Function(com.airbnb.aerosolve.core.function.Function) FunctionUtil.toFloat(com.airbnb.aerosolve.core.function.FunctionUtil.toFloat) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) Map(java.util.Map)

Example 9 with Function

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

the class AdditiveModel method loadInternal.

@Override
protected void loadInternal(ModelHeader header, BufferedReader reader) throws IOException {
    long rows = header.getNumRecords();
    slope = header.getSlope();
    offset = header.getOffset();
    weights = new HashMap<>();
    for (long i = 0; i < rows; i++) {
        String line = reader.readLine();
        ModelRecord record = Util.decodeModel(line);
        String family = record.getFeatureFamily();
        String name = record.getFeatureName();
        Map<String, Function> inner = weights.get(family);
        if (inner == null) {
            inner = new HashMap<>();
            weights.put(family, inner);
        }
        inner.put(name, AbstractFunction.buildFunction(record));
    }
}
Also used : AbstractFunction(com.airbnb.aerosolve.core.function.AbstractFunction) Function(com.airbnb.aerosolve.core.function.Function) ModelRecord(com.airbnb.aerosolve.core.ModelRecord)

Example 10 with Function

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

the class AdditiveModel method save.

@Override
public void save(BufferedWriter writer) throws IOException {
    ModelHeader header = new ModelHeader();
    header.setModelType("additive");
    header.setSlope(slope);
    header.setOffset(offset);
    long count = 0;
    for (Map.Entry<String, Map<String, Function>> familyMap : weights.entrySet()) {
        count += familyMap.getValue().size();
    }
    header.setNumRecords(count);
    ModelRecord headerRec = new ModelRecord();
    headerRec.setModelHeader(header);
    writer.write(Util.encode(headerRec));
    writer.newLine();
    for (Map.Entry<String, Map<String, Function>> familyMap : weights.entrySet()) {
        String featureFamily = familyMap.getKey();
        for (Map.Entry<String, Function> feature : familyMap.getValue().entrySet()) {
            Function func = feature.getValue();
            String featureName = feature.getKey();
            writer.write(Util.encode(func.toModelRecord(featureFamily, featureName)));
            writer.newLine();
        }
    }
    writer.flush();
}
Also used : AbstractFunction(com.airbnb.aerosolve.core.function.AbstractFunction) Function(com.airbnb.aerosolve.core.function.Function) ModelHeader(com.airbnb.aerosolve.core.ModelHeader) ModelRecord(com.airbnb.aerosolve.core.ModelRecord) 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