use of com.airbnb.aerosolve.core.function.Function in project aerosolve by airbnb.
the class AdditiveModel method scoreFeatures.
public float scoreFeatures(SparseLabeledPoint point, double dropout, Random rand) {
float prediction = 0;
for (int i = 0; i < point.indices.length; i++) {
if (dropout <= 0 || rand.nextDouble() > dropout) {
int index = point.indices[i];
Function function = weightVector[index];
float value = point.values[i];
prediction += function.evaluate(value);
}
}
for (int i = 0; i < point.denseIndices.length; i++) {
if (dropout <= 0 || rand.nextDouble() > dropout) {
int index = point.denseIndices[i];
Function function = weightVector[index];
float[] value = point.denseValues[i];
prediction += function.evaluate(value);
}
}
return prediction;
}
use of com.airbnb.aerosolve.core.function.Function in project aerosolve by airbnb.
the class AdditiveModel method debugScoreComponents.
@Override
public List<DebugScoreRecord> debugScoreComponents(FeatureVector combinedItem) {
Map<String, Map<String, Double>> flatFeatures = Util.flattenFeature(combinedItem);
List<DebugScoreRecord> scoreRecordsList = new ArrayList<>();
for (Map.Entry<String, Map<String, Double>> featureFamily : flatFeatures.entrySet()) {
Map<String, Function> familyWeightMap = weights.get(featureFamily.getKey());
if (familyWeightMap == null)
continue;
for (Map.Entry<String, Double> feature : featureFamily.getValue().entrySet()) {
Function func = familyWeightMap.get(feature.getKey());
if (func == null)
continue;
float val = feature.getValue().floatValue();
float weight = func.evaluate(val);
DebugScoreRecord record = new DebugScoreRecord();
record.setFeatureFamily(featureFamily.getKey());
record.setFeatureName(feature.getKey());
record.setFeatureValue(val);
record.setFeatureWeight(weight);
scoreRecordsList.add(record);
}
}
Map<String, List<Double>> denseFeatures = combinedItem.getDenseFeatures();
if (denseFeatures != null) {
Map<String, Function> denseWeights = getOrCreateDenseWeights();
for (Map.Entry<String, List<Double>> feature : denseFeatures.entrySet()) {
String featureName = feature.getKey();
Function fun = denseWeights.get(featureName);
float[] val = toFloat(feature.getValue());
float weight = fun.evaluate(val);
DebugScoreRecord record = new DebugScoreRecord();
record.setFeatureFamily(DENSE_FAMILY);
record.setFeatureName(feature.getKey());
record.setDenseFeatureValue(feature.getValue());
record.setFeatureWeight(weight);
scoreRecordsList.add(record);
}
}
return scoreRecordsList;
}
use of com.airbnb.aerosolve.core.function.Function in project aerosolve by airbnb.
the class AdditiveModel method debugScoreItem.
@Override
public float debugScoreItem(FeatureVector combinedItem, StringBuilder builder) {
Map<String, Map<String, Double>> flatFeatures = Util.flattenFeature(combinedItem);
float sum = 0.0f;
// order by the absolute value
PriorityQueue<Map.Entry<String, Float>> scores = new PriorityQueue<>(100, new LinearModel.EntryComparator());
for (Map.Entry<String, Map<String, Double>> featureFamily : flatFeatures.entrySet()) {
Map<String, Function> familyWeightMap = weights.get(featureFamily.getKey());
if (familyWeightMap == null)
continue;
for (Map.Entry<String, Double> feature : featureFamily.getValue().entrySet()) {
Function func = familyWeightMap.get(feature.getKey());
if (func == null)
continue;
float val = feature.getValue().floatValue();
float subScore = func.evaluate(val);
sum += subScore;
String str = featureFamily.getKey() + ":" + feature.getKey() + "=" + val + " = " + subScore + "<br>\n";
scores.add(new AbstractMap.SimpleEntry<>(str, subScore));
}
}
Map<String, List<Double>> denseFeatures = combinedItem.getDenseFeatures();
if (denseFeatures != null) {
assert (denseWeights != null);
for (Map.Entry<String, List<Double>> feature : denseFeatures.entrySet()) {
String featureName = feature.getKey();
Function fun = denseWeights.get(featureName);
float[] val = toFloat(feature.getValue());
float subScore = fun.evaluate(val);
sum += subScore;
String str = DENSE_FAMILY + ":" + featureName + "=" + val + " = " + subScore + "<br>\n";
scores.add(new AbstractMap.SimpleEntry<>(str, subScore));
}
}
final int MAX_COUNT = 100;
builder.append("Top 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 >= MAX_COUNT) {
builder.append("Leftover = " + (sum - subsum) + '\n');
break;
}
}
}
builder.append("Total = " + sum + '\n');
return sum;
}
use of com.airbnb.aerosolve.core.function.Function in project aerosolve by airbnb.
the class AdditiveModel method loadInternal.
@Override
protected void loadInternal(ModelHeader header, BufferedReader reader) throws IOException {
long rows = header.getNumRecords();
slope = header.getSlope();
offset = header.getOffset();
weights = 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, Function> inner = weights.get(family);
if (inner == null) {
inner = new HashMap<>();
weights.put(family, inner);
}
inner.put(name, AbstractFunction.buildFunction(record));
}
}
use of com.airbnb.aerosolve.core.function.Function 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();
}
Aggregations