Search in sources :

Example 1 with FloatVector

use of com.airbnb.aerosolve.core.util.FloatVector in project aerosolve by airbnb.

the class FullRankLinearModel method scoreFlatFeature.

public FloatVector scoreFlatFeature(Map<String, Map<String, Double>> flatFeatures) {
    int dim = labelDictionary.size();
    FloatVector sum = new FloatVector(dim);
    for (Map.Entry<String, Map<String, Double>> entry : flatFeatures.entrySet()) {
        Map<String, FloatVector> family = weightVector.get(entry.getKey());
        if (family != null) {
            for (Map.Entry<String, Double> feature : entry.getValue().entrySet()) {
                FloatVector vec = family.get(feature.getKey());
                if (vec != null) {
                    sum.multiplyAdd(feature.getValue().floatValue(), vec);
                }
            }
        }
    }
    return sum;
}
Also used : FloatVector(com.airbnb.aerosolve.core.util.FloatVector)

Example 2 with FloatVector

use of com.airbnb.aerosolve.core.util.FloatVector in project aerosolve by airbnb.

the class FullRankLinearModel method debugScoreComponents.

@Override
public List<DebugScoreRecord> debugScoreComponents(FeatureVector combinedItem) {
    Map<String, Map<String, Double>> flatFeatures = Util.flattenFeature(combinedItem);
    List<DebugScoreRecord> scoreRecordsList = new ArrayList<>();
    int dim = labelDictionary.size();
    for (Map.Entry<String, Map<String, Double>> entry : flatFeatures.entrySet()) {
        String familyKey = entry.getKey();
        Map<String, FloatVector> family = weightVector.get(familyKey);
        if (family != null) {
            for (Map.Entry<String, Double> feature : entry.getValue().entrySet()) {
                String featureKey = feature.getKey();
                FloatVector featureWeights = family.get(featureKey);
                float val = feature.getValue().floatValue();
                if (featureWeights != null) {
                    for (int i = 0; i < dim; i++) {
                        DebugScoreRecord record = new DebugScoreRecord();
                        record.setFeatureFamily(familyKey);
                        record.setFeatureName(featureKey);
                        record.setFeatureValue(val);
                        record.setFeatureWeight(featureWeights.get(i));
                        record.setLabel(labelDictionary.get(i).label);
                        scoreRecordsList.add(record);
                    }
                }
            }
        }
    }
    return scoreRecordsList;
}
Also used : DebugScoreRecord(com.airbnb.aerosolve.core.DebugScoreRecord) FloatVector(com.airbnb.aerosolve.core.util.FloatVector)

Example 3 with FloatVector

use of com.airbnb.aerosolve.core.util.FloatVector in project aerosolve by airbnb.

the class FullRankLinearModel method scoreItemMulticlass.

public ArrayList<MulticlassScoringResult> scoreItemMulticlass(FeatureVector combinedItem) {
    ArrayList<MulticlassScoringResult> results = new ArrayList<>();
    Map<String, Map<String, Double>> flatFeatures = Util.flattenFeature(combinedItem);
    FloatVector sum = scoreFlatFeature(flatFeatures);
    for (int i = 0; i < labelDictionary.size(); i++) {
        MulticlassScoringResult result = new MulticlassScoringResult();
        result.setLabel(labelDictionary.get(i).getLabel());
        result.setScore(sum.values[i]);
        results.add(result);
    }
    return results;
}
Also used : MulticlassScoringResult(com.airbnb.aerosolve.core.MulticlassScoringResult) FloatVector(com.airbnb.aerosolve.core.util.FloatVector)

Example 4 with FloatVector

use of com.airbnb.aerosolve.core.util.FloatVector in project aerosolve by airbnb.

the class FullRankLinearModel method save.

public void save(BufferedWriter writer) throws IOException {
    ModelHeader header = new ModelHeader();
    header.setModelType("full_rank_linear");
    long count = 0;
    for (Map.Entry<String, Map<String, FloatVector>> familyMap : weightVector.entrySet()) {
        count += familyMap.getValue().entrySet().size();
    }
    header.setNumRecords(count);
    header.setLabelDictionary(labelDictionary);
    ModelRecord headerRec = new ModelRecord();
    headerRec.setModelHeader(header);
    writer.write(Util.encode(headerRec));
    writer.newLine();
    for (Map.Entry<String, Map<String, FloatVector>> familyMap : weightVector.entrySet()) {
        for (Map.Entry<String, FloatVector> feature : familyMap.getValue().entrySet()) {
            ModelRecord record = new ModelRecord();
            record.setFeatureFamily(familyMap.getKey());
            record.setFeatureName(feature.getKey());
            ArrayList<Double> arrayList = new ArrayList<Double>();
            for (int i = 0; i < feature.getValue().values.length; i++) {
                arrayList.add((double) feature.getValue().values[i]);
            }
            record.setWeightVector(arrayList);
            writer.write(Util.encode(record));
            writer.newLine();
        }
    }
    writer.flush();
}
Also used : ModelHeader(com.airbnb.aerosolve.core.ModelHeader) ModelRecord(com.airbnb.aerosolve.core.ModelRecord) FloatVector(com.airbnb.aerosolve.core.util.FloatVector)

Example 5 with FloatVector

use of com.airbnb.aerosolve.core.util.FloatVector in project aerosolve by airbnb.

the class KernelModel method onlineUpdate.

@Override
public void onlineUpdate(float grad, float learningRate, Map<String, Map<String, Double>> flatFeatures) {
    FloatVector vec = dictionary.makeVectorFromSparseFloats(flatFeatures);
    float deltaG = -learningRate * grad;
    for (SupportVector sv : supportVectors) {
        float response = sv.evaluateUnweighted(vec);
        float deltaW = deltaG * response;
        sv.setWeight(sv.getWeight() + deltaW);
    }
}
Also used : FloatVector(com.airbnb.aerosolve.core.util.FloatVector) SupportVector(com.airbnb.aerosolve.core.util.SupportVector)

Aggregations

FloatVector (com.airbnb.aerosolve.core.util.FloatVector)26 ModelRecord (com.airbnb.aerosolve.core.ModelRecord)8 HashMap (java.util.HashMap)5 ModelHeader (com.airbnb.aerosolve.core.ModelHeader)4 MulticlassScoringResult (com.airbnb.aerosolve.core.MulticlassScoringResult)3 Map (java.util.Map)3 LabelDictionaryEntry (com.airbnb.aerosolve.core.LabelDictionaryEntry)2 SupportVector (com.airbnb.aerosolve.core.util.SupportVector)2 ArrayList (java.util.ArrayList)2 DebugScoreRecord (com.airbnb.aerosolve.core.DebugScoreRecord)1 FeatureVector (com.airbnb.aerosolve.core.FeatureVector)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 CharArrayWriter (java.io.CharArrayWriter)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 java.util (java.util)1 AbstractMap (java.util.AbstractMap)1 Test (org.junit.Test)1