Search in sources :

Example 1 with SupportVector

use of com.airbnb.aerosolve.core.util.SupportVector 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)

Example 2 with SupportVector

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

the class KernelModel method save.

@Override
public void save(BufferedWriter writer) throws IOException {
    ModelHeader header = new ModelHeader();
    header.setModelType("kernel");
    header.setDictionary(dictionary.getDictionary());
    long count = supportVectors.size();
    header.setNumRecords(count);
    ModelRecord headerRec = new ModelRecord();
    headerRec.setModelHeader(header);
    writer.write(Util.encode(headerRec));
    writer.newLine();
    for (SupportVector sv : supportVectors) {
        writer.write(Util.encode(sv.toModelRecord()));
        writer.newLine();
    }
    writer.flush();
}
Also used : ModelHeader(com.airbnb.aerosolve.core.ModelHeader) ModelRecord(com.airbnb.aerosolve.core.ModelRecord) SupportVector(com.airbnb.aerosolve.core.util.SupportVector)

Example 3 with SupportVector

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

the class KernelModel method loadInternal.

@Override
protected void loadInternal(ModelHeader header, BufferedReader reader) throws IOException {
    long rows = header.getNumRecords();
    dictionary = new StringDictionary(header.getDictionary());
    supportVectors = new ArrayList<>();
    for (long i = 0; i < rows; i++) {
        String line = reader.readLine();
        ModelRecord record = Util.decodeModel(line);
        supportVectors.add(new SupportVector(record));
    }
}
Also used : ModelRecord(com.airbnb.aerosolve.core.ModelRecord) SupportVector(com.airbnb.aerosolve.core.util.SupportVector) StringDictionary(com.airbnb.aerosolve.core.util.StringDictionary)

Example 4 with SupportVector

use of com.airbnb.aerosolve.core.util.SupportVector 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)

Aggregations

SupportVector (com.airbnb.aerosolve.core.util.SupportVector)4 ModelRecord (com.airbnb.aerosolve.core.ModelRecord)2 FloatVector (com.airbnb.aerosolve.core.util.FloatVector)2 ModelHeader (com.airbnb.aerosolve.core.ModelHeader)1 StringDictionary (com.airbnb.aerosolve.core.util.StringDictionary)1 AbstractMap (java.util.AbstractMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1