use of com.airbnb.aerosolve.core.util.FloatVector in project aerosolve by airbnb.
the class FullRankLinearModel method loadInternal.
@Override
protected void loadInternal(ModelHeader header, BufferedReader reader) throws IOException {
long rows = header.getNumRecords();
labelDictionary = new ArrayList<>();
for (LabelDictionaryEntry entry : header.getLabelDictionary()) {
labelDictionary.add(entry);
}
buildLabelToIndex();
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, FloatVector> inner = weightVector.get(family);
if (inner == null) {
inner = new HashMap<>();
weightVector.put(family, inner);
}
FloatVector vec = new FloatVector(record.getWeightVector().size());
for (int j = 0; j < record.getWeightVector().size(); j++) {
vec.values[j] = record.getWeightVector().get(j).floatValue();
}
inner.put(name, vec);
}
}
use of com.airbnb.aerosolve.core.util.FloatVector in project aerosolve by airbnb.
the class FullRankLinearModel method scoreItem.
// In the binary case this is just the score for class 0.
// Ideally use a binary model for binary classification.
@Override
public float scoreItem(FeatureVector combinedItem) {
// Not supported.
assert (false);
Map<String, Map<String, Double>> flatFeatures = Util.flattenFeature(combinedItem);
FloatVector sum = scoreFlatFeature(flatFeatures);
return sum.values[0];
}
use of com.airbnb.aerosolve.core.util.FloatVector 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;
}
use of com.airbnb.aerosolve.core.util.FloatVector in project aerosolve by airbnb.
the class LowRankLinearModel method loadInternal.
@Override
protected void loadInternal(ModelHeader header, BufferedReader reader) throws IOException {
long rows = header.getNumRecords();
labelDictionary = new ArrayList<>();
for (LabelDictionaryEntry entry : header.getLabelDictionary()) {
labelDictionary.add(entry);
}
buildLabelToIndex();
labelWeightVector = new HashMap<>();
embeddingDimension = header.getLabelEmbedding().entrySet().iterator().next().getValue().size();
for (Map.Entry<String, java.util.List<Double>> labelRepresentation : header.getLabelEmbedding().entrySet()) {
java.util.List<Double> values = labelRepresentation.getValue();
String labelKey = labelRepresentation.getKey();
FloatVector labelWeight = new FloatVector(embeddingDimension);
for (int i = 0; i < embeddingDimension; i++) {
labelWeight.set(i, values.get(i).floatValue());
}
labelWeightVector.put(labelKey, labelWeight);
}
featureWeightVector = 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, FloatVector> inner = featureWeightVector.get(family);
if (inner == null) {
inner = new HashMap<>();
featureWeightVector.put(family, inner);
}
FloatVector vec = new FloatVector(record.getWeightVector().size());
for (int j = 0; j < record.getWeightVector().size(); j++) {
vec.values[j] = record.getWeightVector().get(j).floatValue();
}
inner.put(name, vec);
}
}
use of com.airbnb.aerosolve.core.util.FloatVector 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();
}
Aggregations