Search in sources :

Example 1 with Mapper

use of com.alibaba.alink.common.mapper.Mapper in project Alink by alibaba.

the class TFTableModelClassificationModelMapper method predictResultDetail.

@Override
protected Tuple2<Object, String> predictResultDetail(SlicedSelectedSample selection) throws Exception {
    Row output = new Row(selection.length());
    selection.fillRow(output);
    for (Mapper mapper : mappers) {
        output = mapper.map(output);
    }
    FloatTensor tensor = (FloatTensor) output.getField(predColId);
    Object predLabel = PredictionExtractUtils.extractFromTensor(tensor, sortedLabels, predDetail, isOutputLogits);
    return Tuple2.of(predLabel, JsonConverter.toJson(predDetail));
}
Also used : ModelMapper(com.alibaba.alink.common.mapper.ModelMapper) RichModelMapper(com.alibaba.alink.common.mapper.RichModelMapper) TFTableModelPredictModelMapper(com.alibaba.alink.operator.common.tensorflow.TFTableModelPredictModelMapper) Mapper(com.alibaba.alink.common.mapper.Mapper) FloatTensor(com.alibaba.alink.common.linalg.tensor.FloatTensor) Row(org.apache.flink.types.Row)

Example 2 with Mapper

use of com.alibaba.alink.common.mapper.Mapper in project Alink by alibaba.

the class PipelineModel method collectLocalPredictor.

@Override
public LocalPredictor collectLocalPredictor(TableSchema inputSchema) throws Exception {
    if (params.get(ModelStreamScanParams.MODEL_STREAM_FILE_PATH) != null) {
        BatchOperator<?> modelSave = ModelExporterUtils.serializePipelineStages(Arrays.asList(transformers), params);
        TableSchema extendSchema = getOutSchema(this, inputSchema);
        BatchOperator<?> model = new TableSourceBatchOp(DataSetConversionUtil.toTable(modelSave.getMLEnvironmentId(), modelSave.getDataSet().map(new PipelineModelMapper.ExtendPipelineModelRow(extendSchema.getFieldNames().length + 1)), PipelineModelMapper.getExtendModelSchema(modelSave.getSchema(), extendSchema.getFieldNames(), extendSchema.getFieldTypes())));
        List<Row> modelRows = model.collect();
        ModelMapper mapper = new PipelineModelMapper(model.getSchema(), inputSchema, this.params);
        mapper.loadModel(modelRows);
        return new LocalPredictor(mapper);
    }
    if (null == transformers || transformers.length == 0) {
        throw new RuntimeException("PipelineModel is empty.");
    }
    List<BatchOperator<?>> allModelData = new ArrayList<>();
    for (TransformerBase<?> transformer : transformers) {
        if (!(transformer instanceof LocalPredictable)) {
            throw new RuntimeException(transformer.getClass().toString() + " not support local predict.");
        }
        if (transformer instanceof ModelBase) {
            allModelData.add(((ModelBase<?>) transformer).getModelData());
        }
    }
    List<List<Row>> allModelDataRows;
    if (!allModelData.isEmpty()) {
        allModelDataRows = BatchOperator.collect(allModelData.toArray(new BatchOperator<?>[0]));
    } else {
        allModelDataRows = new ArrayList<>();
    }
    TableSchema schema = inputSchema;
    int numMapperModel = 0;
    List<Mapper> mappers = new ArrayList<>();
    for (TransformerBase<?> transformer : transformers) {
        Mapper mapper;
        if (transformer instanceof MapModel) {
            mapper = ModelExporterUtils.createMapperFromStage(transformer, ((MapModel<?>) transformer).modelData.getSchema(), schema, allModelDataRows.get(numMapperModel));
            numMapperModel++;
        } else if (transformer instanceof BaseRecommender) {
            mapper = ModelExporterUtils.createMapperFromStage(transformer, ((BaseRecommender<?>) transformer).modelData.getSchema(), schema, allModelDataRows.get(numMapperModel));
            numMapperModel++;
        } else {
            mapper = ModelExporterUtils.createMapperFromStage(transformer, null, schema, null);
        }
        mappers.add(mapper);
        schema = mapper.getOutputSchema();
    }
    return new LocalPredictor(mappers.toArray(new Mapper[0]));
}
Also used : TableSchema(org.apache.flink.table.api.TableSchema) ArrayList(java.util.ArrayList) PipelineModelMapper(com.alibaba.alink.common.mapper.PipelineModelMapper) TableSourceBatchOp(com.alibaba.alink.operator.batch.source.TableSourceBatchOp) BatchOperator(com.alibaba.alink.operator.batch.BatchOperator) ModelMapper(com.alibaba.alink.common.mapper.ModelMapper) PipelineModelMapper(com.alibaba.alink.common.mapper.PipelineModelMapper) ModelMapper(com.alibaba.alink.common.mapper.ModelMapper) PipelineModelMapper(com.alibaba.alink.common.mapper.PipelineModelMapper) Mapper(com.alibaba.alink.common.mapper.Mapper) BaseRecommender(com.alibaba.alink.pipeline.recommendation.BaseRecommender) ArrayList(java.util.ArrayList) List(java.util.List) Row(org.apache.flink.types.Row)

Example 3 with Mapper

use of com.alibaba.alink.common.mapper.Mapper in project Alink by alibaba.

the class MapStreamOp method linkFrom.

@Override
public T linkFrom(StreamOperator<?>... inputs) {
    StreamOperator<?> in = checkAndGetFirst(inputs);
    try {
        Mapper mapper = this.mapperBuilder.apply(in.getSchema(), this.getParams());
        DataStream<Row> resultRows;
        if (getParams().get(MapperParams.NUM_THREADS) <= 1) {
            resultRows = in.getDataStream().map(new MapperAdapter(mapper));
        } else {
            resultRows = in.getDataStream().flatMap(new MapperAdapterMT(mapper, getParams().get(MapperParams.NUM_THREADS)));
        }
        this.setOutput(resultRows, mapper.getOutputSchema());
        return (T) this;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : MapperAdapterMT(com.alibaba.alink.common.mapper.MapperAdapterMT) Mapper(com.alibaba.alink.common.mapper.Mapper) MapperAdapterMT(com.alibaba.alink.common.mapper.MapperAdapterMT) Row(org.apache.flink.types.Row) MapperAdapter(com.alibaba.alink.common.mapper.MapperAdapter)

Example 4 with Mapper

use of com.alibaba.alink.common.mapper.Mapper in project Alink by alibaba.

the class VectorToTensorMapperTest method testFloatType.

@Test
public void testFloatType() throws Exception {
    final Mapper mapper = new VectorToTensorMapper(new TableSchema(new String[] { "vec" }, new TypeInformation<?>[] { VectorTypes.DENSE_VECTOR }), new Params().set(VectorToTensorParams.SELECTED_COL, "vec").set(VectorToTensorParams.TENSOR_SHAPE, new Long[] { 2L, 3L }).set(VectorToTensorParams.TENSOR_DATA_TYPE, DataType.FLOAT));
    Assert.assertEquals(TensorTypes.FLOAT_TENSOR, mapper.getOutputSchema().getFieldTypes()[0]);
    final DoubleTensor tensor = DoubleTensor.of(TensorUtil.getTensor("FLOAT#6#0.0 0.1 1.0 1.1 2.0 2.1 "));
    final FloatTensor expect = FloatTensor.of(tensor.reshape(new Shape(2L, 3L)));
    final Tensor<?> result = (Tensor<?>) mapper.map(Row.of(tensor.toVector())).getField(0);
    Assert.assertEquals(expect, result);
}
Also used : Mapper(com.alibaba.alink.common.mapper.Mapper) DoubleTensor(com.alibaba.alink.common.linalg.tensor.DoubleTensor) Shape(com.alibaba.alink.common.linalg.tensor.Shape) DoubleTensor(com.alibaba.alink.common.linalg.tensor.DoubleTensor) Tensor(com.alibaba.alink.common.linalg.tensor.Tensor) FloatTensor(com.alibaba.alink.common.linalg.tensor.FloatTensor) TableSchema(org.apache.flink.table.api.TableSchema) VectorToTensorParams(com.alibaba.alink.params.dataproc.VectorToTensorParams) Params(org.apache.flink.ml.api.misc.param.Params) FloatTensor(com.alibaba.alink.common.linalg.tensor.FloatTensor) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) Test(org.junit.Test)

Example 5 with Mapper

use of com.alibaba.alink.common.mapper.Mapper in project Alink by alibaba.

the class VectorToTensorMapperTest method testReshape.

@Test
public void testReshape() throws Exception {
    final Mapper mapper = new VectorToTensorMapper(new TableSchema(new String[] { "vec" }, new TypeInformation<?>[] { VectorTypes.DENSE_VECTOR }), new Params().set(VectorToTensorParams.SELECTED_COL, "vec").set(VectorToTensorParams.TENSOR_SHAPE, new Long[] { 2L, 3L }));
    final DoubleTensor tensor = DoubleTensor.of(TensorUtil.getTensor("FLOAT#6#0.0 0.1 1.0 1.1 2.0 2.1 "));
    final DoubleTensor expect = tensor.reshape(new Shape(2L, 3L));
    final Tensor<?> result = (Tensor<?>) mapper.map(Row.of(tensor.toVector())).getField(0);
    Assert.assertEquals(expect, result);
}
Also used : Mapper(com.alibaba.alink.common.mapper.Mapper) DoubleTensor(com.alibaba.alink.common.linalg.tensor.DoubleTensor) Shape(com.alibaba.alink.common.linalg.tensor.Shape) DoubleTensor(com.alibaba.alink.common.linalg.tensor.DoubleTensor) Tensor(com.alibaba.alink.common.linalg.tensor.Tensor) FloatTensor(com.alibaba.alink.common.linalg.tensor.FloatTensor) TableSchema(org.apache.flink.table.api.TableSchema) VectorToTensorParams(com.alibaba.alink.params.dataproc.VectorToTensorParams) Params(org.apache.flink.ml.api.misc.param.Params) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) Test(org.junit.Test)

Aggregations

Mapper (com.alibaba.alink.common.mapper.Mapper)28 TableSchema (org.apache.flink.table.api.TableSchema)22 Params (org.apache.flink.ml.api.misc.param.Params)19 TypeInformation (org.apache.flink.api.common.typeinfo.TypeInformation)18 Test (org.junit.Test)17 FloatTensor (com.alibaba.alink.common.linalg.tensor.FloatTensor)12 DoubleTensor (com.alibaba.alink.common.linalg.tensor.DoubleTensor)10 Tensor (com.alibaba.alink.common.linalg.tensor.Tensor)9 Row (org.apache.flink.types.Row)8 ModelMapper (com.alibaba.alink.common.mapper.ModelMapper)7 ToTensorParams (com.alibaba.alink.params.dataproc.ToTensorParams)7 StringTensor (com.alibaba.alink.common.linalg.tensor.StringTensor)6 Shape (com.alibaba.alink.common.linalg.tensor.Shape)5 PipelineModelMapper (com.alibaba.alink.common.mapper.PipelineModelMapper)5 ArrayList (java.util.ArrayList)5 ComboModelMapper (com.alibaba.alink.common.mapper.ComboModelMapper)4 ToVectorParams (com.alibaba.alink.params.dataproc.ToVectorParams)4 DenseVector (com.alibaba.alink.common.linalg.DenseVector)3 SparseVector (com.alibaba.alink.common.linalg.SparseVector)3 ComboMapper (com.alibaba.alink.common.mapper.ComboMapper)3