use of com.alibaba.alink.operator.common.distance.FastDistanceData in project Alink by alibaba.
the class VectorModelData method computeDistiance.
@Override
protected ArrayList<Tuple2<Double, Object>> computeDistiance(Object input, Integer index, Integer topN, Tuple2<Double, Object> radius) {
FastDistanceData data = dictData.get(index);
DenseMatrix res = fastDistance.calc((FastDistanceVectorData) input, data);
ArrayList<Tuple2<Double, Object>> list = new ArrayList();
Row[] curRows = data.getRows();
for (int i = 0; i < data.getRows().length; i++) {
Tuple2<Double, Object> tuple = Tuple2.of(res.getData()[i], curRows[i].getField(0));
if (null == radius || radius.f0 == null || this.getQueueComparator().compare(radius, tuple) <= 0) {
list.add(tuple);
}
}
return list;
}
use of com.alibaba.alink.operator.common.distance.FastDistanceData in project Alink by alibaba.
the class VectorModelDataConverter method buildIndex.
@Override
public DataSet<Row> buildIndex(BatchOperator in, Params params) {
DataSet<Row> dataSet = in.getDataSet();
FastDistance fastDistance = params.get(HasFastDistanceType.DISTANCE_TYPE).getFastDistance();
DataSet<Row> index = dataSet.mapPartition(new RichMapPartitionFunction<Row, Row>() {
private static final long serialVersionUID = -6035963841026118219L;
@Override
public void mapPartition(Iterable<Row> values, Collector<Row> out) throws Exception {
List<FastDistanceData> list = fastDistance.prepareMatrixData(values, 1, 0);
for (FastDistanceData fastDistanceData : list) {
Row row = new Row(ROW_SIZE);
if (fastDistanceData instanceof FastDistanceMatrixData) {
row.setField(FASTDISTANCE_TYPE_INDEX, 1L);
FastDistanceMatrixData data = (FastDistanceMatrixData) fastDistanceData;
row.setField(DATA_INDEX, data.toString());
} else if (fastDistanceData instanceof FastDistanceVectorData) {
row.setField(FASTDISTANCE_TYPE_INDEX, 2L);
FastDistanceVectorData data = (FastDistanceVectorData) fastDistanceData;
row.setField(DATA_INDEX, data.toString());
} else if (fastDistanceData instanceof FastDistanceSparseData) {
row.setField(FASTDISTANCE_TYPE_INDEX, 3L);
FastDistanceSparseData data = (FastDistanceSparseData) fastDistanceData;
row.setField(DATA_INDEX, data.toString());
} else {
throw new RuntimeException(fastDistanceData.getClass().getName() + "is not supported!");
}
out.collect(row);
}
}
});
return index.mapPartition(new RichMapPartitionFunction<Row, Row>() {
private static final long serialVersionUID = 661383020005730224L;
@Override
public void mapPartition(Iterable<Row> values, Collector<Row> out) throws Exception {
Params meta = null;
if (getRuntimeContext().getIndexOfThisSubtask() == 0) {
meta = params;
}
new VectorModelDataConverter().save(Tuple2.of(meta, values), out);
}
}).name("build_model");
}
Aggregations