use of com.airbnb.aerosolve.core.util.FloatVector in project aerosolve by airbnb.
the class FullRankLinearModel method scoreFlatFeature.
public FloatVector scoreFlatFeature(Map<String, Map<String, Double>> flatFeatures) {
int dim = labelDictionary.size();
FloatVector sum = new FloatVector(dim);
for (Map.Entry<String, Map<String, Double>> entry : flatFeatures.entrySet()) {
Map<String, FloatVector> family = weightVector.get(entry.getKey());
if (family != null) {
for (Map.Entry<String, Double> feature : entry.getValue().entrySet()) {
FloatVector vec = family.get(feature.getKey());
if (vec != null) {
sum.multiplyAdd(feature.getValue().floatValue(), vec);
}
}
}
}
return sum;
}
use of com.airbnb.aerosolve.core.util.FloatVector in project aerosolve by airbnb.
the class FullRankLinearModel method debugScoreComponents.
@Override
public List<DebugScoreRecord> debugScoreComponents(FeatureVector combinedItem) {
Map<String, Map<String, Double>> flatFeatures = Util.flattenFeature(combinedItem);
List<DebugScoreRecord> scoreRecordsList = new ArrayList<>();
int dim = labelDictionary.size();
for (Map.Entry<String, Map<String, Double>> entry : flatFeatures.entrySet()) {
String familyKey = entry.getKey();
Map<String, FloatVector> family = weightVector.get(familyKey);
if (family != null) {
for (Map.Entry<String, Double> feature : entry.getValue().entrySet()) {
String featureKey = feature.getKey();
FloatVector featureWeights = family.get(featureKey);
float val = feature.getValue().floatValue();
if (featureWeights != null) {
for (int i = 0; i < dim; i++) {
DebugScoreRecord record = new DebugScoreRecord();
record.setFeatureFamily(familyKey);
record.setFeatureName(featureKey);
record.setFeatureValue(val);
record.setFeatureWeight(featureWeights.get(i));
record.setLabel(labelDictionary.get(i).label);
scoreRecordsList.add(record);
}
}
}
}
}
return scoreRecordsList;
}
use of com.airbnb.aerosolve.core.util.FloatVector in project aerosolve by airbnb.
the class FullRankLinearModel method scoreItemMulticlass.
public ArrayList<MulticlassScoringResult> scoreItemMulticlass(FeatureVector combinedItem) {
ArrayList<MulticlassScoringResult> results = new ArrayList<>();
Map<String, Map<String, Double>> flatFeatures = Util.flattenFeature(combinedItem);
FloatVector sum = scoreFlatFeature(flatFeatures);
for (int i = 0; i < labelDictionary.size(); i++) {
MulticlassScoringResult result = new MulticlassScoringResult();
result.setLabel(labelDictionary.get(i).getLabel());
result.setScore(sum.values[i]);
results.add(result);
}
return results;
}
use of com.airbnb.aerosolve.core.util.FloatVector in project aerosolve by airbnb.
the class FullRankLinearModel method save.
public void save(BufferedWriter writer) throws IOException {
ModelHeader header = new ModelHeader();
header.setModelType("full_rank_linear");
long count = 0;
for (Map.Entry<String, Map<String, FloatVector>> familyMap : weightVector.entrySet()) {
count += familyMap.getValue().entrySet().size();
}
header.setNumRecords(count);
header.setLabelDictionary(labelDictionary);
ModelRecord headerRec = new ModelRecord();
headerRec.setModelHeader(header);
writer.write(Util.encode(headerRec));
writer.newLine();
for (Map.Entry<String, Map<String, FloatVector>> familyMap : weightVector.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<Double>();
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();
}
use of com.airbnb.aerosolve.core.util.FloatVector 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);
}
}
Aggregations