Search in sources :

Example 6 with ModelHeader

use of com.airbnb.aerosolve.core.ModelHeader in project aerosolve by airbnb.

the class LinearModel method save.

// save model
public void save(BufferedWriter writer) throws IOException {
    ModelHeader header = new ModelHeader();
    header.setModelType("linear");
    header.setSlope(slope);
    header.setOffset(offset);
    long count = 0;
    for (Map.Entry<String, Map<String, Float>> familyMap : weights.entrySet()) {
        for (Map.Entry<String, Float> feature : familyMap.getValue().entrySet()) {
            count++;
        }
    }
    header.setNumRecords(count);
    ModelRecord headerRec = new ModelRecord();
    headerRec.setModelHeader(header);
    writer.write(Util.encode(headerRec));
    writer.newLine();
    for (Map.Entry<String, Map<String, Float>> familyMap : weights.entrySet()) {
        for (Map.Entry<String, Float> feature : familyMap.getValue().entrySet()) {
            ModelRecord record = new ModelRecord();
            record.setFeatureFamily(familyMap.getKey());
            record.setFeatureName(feature.getKey());
            record.setFeatureWeight(feature.getValue());
            writer.write(Util.encode(record));
            writer.newLine();
        }
    }
    writer.flush();
}
Also used : ModelHeader(com.airbnb.aerosolve.core.ModelHeader) ModelRecord(com.airbnb.aerosolve.core.ModelRecord)

Example 7 with ModelHeader

use of com.airbnb.aerosolve.core.ModelHeader in project aerosolve by airbnb.

the class MaxoutModel method save.

public void save(BufferedWriter writer) throws IOException {
    ModelHeader header = new ModelHeader();
    header.setModelType("maxout");
    header.setNumHidden(numHidden);
    long count = 0;
    for (Map.Entry<String, Map<String, WeightVector>> familyMap : weightVector.entrySet()) {
        for (Map.Entry<String, WeightVector> feature : familyMap.getValue().entrySet()) {
            count++;
        }
    }
    header.setNumRecords(count);
    ModelRecord headerRec = new ModelRecord();
    headerRec.setModelHeader(header);
    writer.write(Util.encode(headerRec));
    writer.newLine();
    for (Map.Entry<String, Map<String, WeightVector>> familyMap : weightVector.entrySet()) {
        for (Map.Entry<String, WeightVector> 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().weights.values.length; i++) {
                arrayList.add((double) feature.getValue().weights.values[i]);
            }
            record.setWeightVector(arrayList);
            record.setScale(feature.getValue().scale);
            writer.write(Util.encode(record));
            writer.newLine();
        }
    }
    writer.flush();
}
Also used : ModelHeader(com.airbnb.aerosolve.core.ModelHeader) ModelRecord(com.airbnb.aerosolve.core.ModelRecord)

Example 8 with ModelHeader

use of com.airbnb.aerosolve.core.ModelHeader in project aerosolve by airbnb.

the class MlpModel method save.

public void save(BufferedWriter writer) throws IOException {
    ModelHeader header = new ModelHeader();
    header.setModelType("multilayer_perceptron");
    header.setNumHiddenLayers(numHiddenLayers);
    ArrayList<Integer> nodeNum = new ArrayList<>();
    for (int i = 0; i < numHiddenLayers + 1; i++) {
        // this includes the number of node at the output layer
        nodeNum.add(layerNodeNumber.get(i));
    }
    header.setNumberHiddenNodes(nodeNum);
    long count = 0;
    for (Map.Entry<String, Map<String, FloatVector>> familyMap : inputLayerWeights.entrySet()) {
        count += familyMap.getValue().entrySet().size();
    }
    // number of record for the input layer weights
    header.setNumRecords(count);
    ModelRecord headerRec = new ModelRecord();
    headerRec.setModelHeader(header);
    writer.write(Util.encode(headerRec));
    writer.newLine();
    // save the input layer weight, one record per feature
    for (Map.Entry<String, Map<String, FloatVector>> familyMap : inputLayerWeights.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().length(); i++) {
                arrayList.add((double) feature.getValue().values[i]);
            }
            record.setWeightVector(arrayList);
            writer.write(Util.encode(record));
            writer.newLine();
        }
    }
    // save the bias for each layer after input layer, one record per layer
    for (int i = 0; i < numHiddenLayers + 1; i++) {
        ArrayList<Double> arrayList = new ArrayList<>();
        FloatVector layerBias = bias.get(i);
        int n = layerBias.length();
        ModelRecord record = new ModelRecord();
        for (int j = 0; j < n; j++) {
            arrayList.add((double) layerBias.get(j));
        }
        record.setWeightVector(arrayList);
        record.setFunctionForm(activationFunction.get(i));
        writer.write(Util.encode(record));
        writer.newLine();
    }
    // save the hiddenLayerWeights, one record per (layer + node)
    for (int i = 0; i < numHiddenLayers; i++) {
        ArrayList<FloatVector> weights = hiddenLayerWeights.get(i);
        for (int j = 0; j < layerNodeNumber.get(i); j++) {
            FloatVector w = weights.get(j);
            ModelRecord record = new ModelRecord();
            ArrayList<Double> arrayList = new ArrayList<>();
            for (int k = 0; k < w.length(); k++) {
                arrayList.add((double) w.get(k));
            }
            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)

Example 9 with ModelHeader

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

Example 10 with ModelHeader

use of com.airbnb.aerosolve.core.ModelHeader 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

ModelHeader (com.airbnb.aerosolve.core.ModelHeader)14 ModelRecord (com.airbnb.aerosolve.core.ModelRecord)14 FeatureVector (com.airbnb.aerosolve.core.FeatureVector)4 FloatVector (com.airbnb.aerosolve.core.util.FloatVector)4 Test (org.junit.Test)4 BufferedReader (java.io.BufferedReader)3 BufferedWriter (java.io.BufferedWriter)3 CharArrayWriter (java.io.CharArrayWriter)3 IOException (java.io.IOException)3 StringReader (java.io.StringReader)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 MulticlassScoringResult (com.airbnb.aerosolve.core.MulticlassScoringResult)1 AbstractFunction (com.airbnb.aerosolve.core.function.AbstractFunction)1 Function (com.airbnb.aerosolve.core.function.Function)1 SupportVector (com.airbnb.aerosolve.core.util.SupportVector)1 AbstractMap (java.util.AbstractMap)1