Search in sources :

Example 11 with FloatVector

use of com.airbnb.aerosolve.core.util.FloatVector 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 12 with FloatVector

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

the class MaxoutModel method debugScoreItem.

@Override
public float debugScoreItem(FeatureVector combinedItem, StringBuilder builder) {
    Map<String, Map<String, Double>> flatFeatures = Util.flattenFeature(combinedItem);
    FloatVector response = getResponse(flatFeatures);
    FloatVector.MinMaxResult result = response.getMinMaxResult();
    float sum = result.maxValue - result.minValue;
    PriorityQueue<Map.Entry<String, Float>> scores = new PriorityQueue<>(100, new LinearModel.EntryComparator());
    float[] biasWt = bias.weights.getValues();
    float biasScore = biasWt[result.maxIndex] - biasWt[result.minIndex];
    scores.add(new AbstractMap.SimpleEntry<String, Float>("bias = " + biasScore + " <br>\n", biasScore));
    for (Map.Entry<String, Map<String, Double>> featureFamily : flatFeatures.entrySet()) {
        Map<String, WeightVector> familyWeightMap = weightVector.get(featureFamily.getKey());
        if (familyWeightMap == null)
            continue;
        for (Map.Entry<String, Double> feature : featureFamily.getValue().entrySet()) {
            WeightVector weightVec = familyWeightMap.get(feature.getKey());
            if (weightVec == null)
                continue;
            float val = feature.getValue().floatValue();
            float[] wt = weightVec.weights.getValues();
            float p = wt[result.maxIndex] * weightVec.scale;
            float n = wt[result.minIndex] * weightVec.scale;
            float subscore = val * (p - n);
            String str = featureFamily.getKey() + ":" + feature.getKey() + "=" + val + " * (" + p + " - " + n + ") = " + subscore + "<br>\n";
            scores.add(new AbstractMap.SimpleEntry<String, Float>(str, subscore));
        }
    }
    builder.append("Top 15 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 >= 15) {
                builder.append("Leftover = " + (sum - subsum) + '\n');
                break;
            }
        }
    }
    builder.append("Total = " + sum + '\n');
    return sum;
}
Also used : FloatVector(com.airbnb.aerosolve.core.util.FloatVector)

Example 13 with FloatVector

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

the class MaxoutModel method loadInternal.

@Override
protected void loadInternal(ModelHeader header, BufferedReader reader) throws IOException {
    long rows = header.getNumRecords();
    numHidden = header.getNumHidden();
    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, WeightVector> inner = weightVector.get(family);
        if (inner == null) {
            inner = new HashMap<>();
            weightVector.put(family, inner);
        }
        WeightVector vec = new WeightVector();
        vec.scale = (float) record.getScale();
        vec.weights = new FloatVector(numHidden);
        for (int j = 0; j < numHidden; j++) {
            vec.weights.values[j] = record.getWeightVector().get(j).floatValue();
        }
        inner.put(name, vec);
    }
    Map<String, WeightVector> special = weightVector.get("$SPECIAL");
    assert (special != null);
    bias = special.get("$BIAS");
    assert (bias != null);
}
Also used : ModelRecord(com.airbnb.aerosolve.core.ModelRecord) FloatVector(com.airbnb.aerosolve.core.util.FloatVector)

Example 14 with FloatVector

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

the class LowRankLinearModelTest method testLoad.

@Test
public void testLoad() {
    CharArrayWriter charWriter = new CharArrayWriter();
    BufferedWriter writer = new BufferedWriter(charWriter);
    ModelHeader header = new ModelHeader();
    header.setModelType("low_rank_linear");
    header.setLabelDictionary(makeLabelDictionary());
    Map<String, FloatVector> labelWeightVector = makeLabelWeightVector();
    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 < 3; i++) {
            arrayList.add((double) values[i]);
        }
        labelEmbedding.put(labelRepresentation.getKey(), arrayList);
    }
    header.setLabelEmbedding(labelEmbedding);
    header.setNumRecords(4);
    ArrayList<Double> ws = new ArrayList<>();
    ws.add(1.0);
    ws.add(0.0);
    ws.add(0.0);
    ModelRecord record1 = new ModelRecord();
    record1.setModelHeader(header);
    ModelRecord record2 = new ModelRecord();
    record2.setFeatureFamily("a");
    record2.setFeatureName("cat");
    record2.setWeightVector(ws);
    ModelRecord record3 = new ModelRecord();
    record3.setFeatureFamily("a");
    record3.setFeatureName("dog");
    record3.setWeightVector(ws);
    ModelRecord record4 = new ModelRecord();
    record4.setFeatureFamily("a");
    record4.setFeatureName("fish");
    record4.setWeightVector(ws);
    ModelRecord record5 = new ModelRecord();
    record5.setFeatureFamily("a");
    record5.setFeatureName("horse");
    record5.setWeightVector(ws);
    try {
        writer.write(Util.encode(record1) + "\n");
        writer.write(Util.encode(record2) + "\n");
        writer.write(Util.encode(record3) + "\n");
        writer.write(Util.encode(record4) + "\n");
        writer.write(Util.encode(record5) + "\n");
        writer.close();
    } catch (IOException e) {
        assertTrue("Could not write", false);
    }
    String serialized = charWriter.toString();
    assertTrue(serialized.length() > 0);
    StringReader strReader = new StringReader(serialized);
    BufferedReader reader = new BufferedReader(strReader);
    FeatureVector animalFv = makeFeatureVector("animal");
    FeatureVector colorFv = makeFeatureVector("color");
    try {
        Optional<AbstractModel> model = ModelFactory.createFromReader(reader);
        assertTrue(model.isPresent());
        ArrayList<MulticlassScoringResult> s1 = model.get().scoreItemMulticlass(animalFv);
        assertEquals(s1.size(), 3);
        assertEquals(0.0f, s1.get(0).score, 3.0f);
        assertEquals(0.0f, s1.get(1).score, 1e-10f);
        assertEquals(0.0f, s1.get(2).score, 1e-10f);
        ArrayList<MulticlassScoringResult> s2 = model.get().scoreItemMulticlass(colorFv);
        assertEquals(s2.size(), 3);
        assertEquals(0.0f, s2.get(0).score, 1e-10f);
        assertEquals(0.0f, s2.get(1).score, 1e-10f);
        assertEquals(0.0f, s2.get(2).score, 1e-10f);
    } catch (IOException e) {
        assertTrue("Could not read", false);
    }
}
Also used : FeatureVector(com.airbnb.aerosolve.core.FeatureVector) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FloatVector(com.airbnb.aerosolve.core.util.FloatVector) MulticlassScoringResult(com.airbnb.aerosolve.core.MulticlassScoringResult) CharArrayWriter(java.io.CharArrayWriter) BufferedWriter(java.io.BufferedWriter) ModelHeader(com.airbnb.aerosolve.core.ModelHeader) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) BufferedReader(java.io.BufferedReader) ModelRecord(com.airbnb.aerosolve.core.ModelRecord) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 15 with FloatVector

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

the class AbstractModel method scoreToProbability.

// Populates a multiclass result probability using softmax over the scores by default.
// Some models might override this if their scores are already probabilities e.g. random forests.
public void scoreToProbability(ArrayList<MulticlassScoringResult> results) {
    FloatVector vec = new FloatVector(results.size());
    for (int i = 0; i < results.size(); i++) {
        vec.values[i] = (float) results.get(i).score;
    }
    vec.softmax();
    for (int i = 0; i < results.size(); i++) {
        results.get(i).probability = vec.values[i];
    }
}
Also used : FloatVector(com.airbnb.aerosolve.core.util.FloatVector)

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