Search in sources :

Example 16 with FloatVector

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

the class FullRankLinearModel method loadInternal.

@Override
protected void loadInternal(ModelHeader header, BufferedReader reader) throws IOException {
    long rows = header.getNumRecords();
    labelDictionary = new ArrayList<>();
    for (LabelDictionaryEntry entry : header.getLabelDictionary()) {
        labelDictionary.add(entry);
    }
    buildLabelToIndex();
    weightVector = 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, FloatVector> inner = weightVector.get(family);
        if (inner == null) {
            inner = new HashMap<>();
            weightVector.put(family, inner);
        }
        FloatVector vec = new FloatVector(record.getWeightVector().size());
        for (int j = 0; j < record.getWeightVector().size(); j++) {
            vec.values[j] = record.getWeightVector().get(j).floatValue();
        }
        inner.put(name, vec);
    }
}
Also used : LabelDictionaryEntry(com.airbnb.aerosolve.core.LabelDictionaryEntry) ModelRecord(com.airbnb.aerosolve.core.ModelRecord) FloatVector(com.airbnb.aerosolve.core.util.FloatVector)

Example 17 with FloatVector

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

the class FullRankLinearModel method scoreItem.

// In the binary case this is just the score for class 0.
// Ideally use a binary model for binary classification.
@Override
public float scoreItem(FeatureVector combinedItem) {
    // Not supported.
    assert (false);
    Map<String, Map<String, Double>> flatFeatures = Util.flattenFeature(combinedItem);
    FloatVector sum = scoreFlatFeature(flatFeatures);
    return sum.values[0];
}
Also used : FloatVector(com.airbnb.aerosolve.core.util.FloatVector)

Example 18 with FloatVector

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

the class KernelModel method scoreItem.

@Override
public float scoreItem(FeatureVector combinedItem) {
    Map<String, Map<String, Double>> flatFeatures = Util.flattenFeature(combinedItem);
    FloatVector vec = dictionary.makeVectorFromSparseFloats(flatFeatures);
    float sum = 0.0f;
    for (int i = 0; i < supportVectors.size(); i++) {
        SupportVector sv = supportVectors.get(i);
        sum += sv.evaluate(vec);
    }
    return sum;
}
Also used : FloatVector(com.airbnb.aerosolve.core.util.FloatVector) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) Map(java.util.Map) SupportVector(com.airbnb.aerosolve.core.util.SupportVector)

Example 19 with FloatVector

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

the class LowRankLinearModel method loadInternal.

@Override
protected void loadInternal(ModelHeader header, BufferedReader reader) throws IOException {
    long rows = header.getNumRecords();
    labelDictionary = new ArrayList<>();
    for (LabelDictionaryEntry entry : header.getLabelDictionary()) {
        labelDictionary.add(entry);
    }
    buildLabelToIndex();
    labelWeightVector = new HashMap<>();
    embeddingDimension = header.getLabelEmbedding().entrySet().iterator().next().getValue().size();
    for (Map.Entry<String, java.util.List<Double>> labelRepresentation : header.getLabelEmbedding().entrySet()) {
        java.util.List<Double> values = labelRepresentation.getValue();
        String labelKey = labelRepresentation.getKey();
        FloatVector labelWeight = new FloatVector(embeddingDimension);
        for (int i = 0; i < embeddingDimension; i++) {
            labelWeight.set(i, values.get(i).floatValue());
        }
        labelWeightVector.put(labelKey, labelWeight);
    }
    featureWeightVector = 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, FloatVector> inner = featureWeightVector.get(family);
        if (inner == null) {
            inner = new HashMap<>();
            featureWeightVector.put(family, inner);
        }
        FloatVector vec = new FloatVector(record.getWeightVector().size());
        for (int j = 0; j < record.getWeightVector().size(); j++) {
            vec.values[j] = record.getWeightVector().get(j).floatValue();
        }
        inner.put(name, vec);
    }
}
Also used : LabelDictionaryEntry(com.airbnb.aerosolve.core.LabelDictionaryEntry) FloatVector(com.airbnb.aerosolve.core.util.FloatVector) java.util(java.util) ModelRecord(com.airbnb.aerosolve.core.ModelRecord)

Example 20 with FloatVector

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

the class LowRankLinearModel method save.

public void save(BufferedWriter writer) throws IOException {
    ModelHeader header = new ModelHeader();
    header.setModelType("low_rank_linear");
    long count = 0;
    for (Map.Entry<String, Map<String, FloatVector>> familyMap : featureWeightVector.entrySet()) {
        count += familyMap.getValue().entrySet().size();
    }
    header.setNumRecords(count);
    header.setLabelDictionary(labelDictionary);
    Map<String, java.util.List<Double>> labelEmbedding = new HashMap<>();
    for (Map.Entry<String, FloatVector> labelRepresentation : labelWeightVector.entrySet()) {
        float[] values = labelRepresentation.getValue().getValues();
        ArrayList<Double> arrayList = new ArrayList<>();
        for (int i = 0; i < embeddingDimension; i++) {
            arrayList.add((double) values[i]);
        }
        labelEmbedding.put(labelRepresentation.getKey(), arrayList);
    }
    header.setLabelEmbedding(labelEmbedding);
    ModelRecord headerRec = new ModelRecord();
    headerRec.setModelHeader(header);
    writer.write(Util.encode(headerRec));
    writer.newLine();
    for (Map.Entry<String, Map<String, FloatVector>> familyMap : featureWeightVector.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<>();
            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 : FloatVector(com.airbnb.aerosolve.core.util.FloatVector) ModelHeader(com.airbnb.aerosolve.core.ModelHeader) ModelRecord(com.airbnb.aerosolve.core.ModelRecord)

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