Search in sources :

Example 1 with BaseRecommender

use of com.alibaba.alink.pipeline.recommendation.BaseRecommender 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 2 with BaseRecommender

use of com.alibaba.alink.pipeline.recommendation.BaseRecommender in project Alink by alibaba.

the class ModelExporterUtils method createMapperFromStage.

// not open and load.
public static Mapper createMapperFromStage(PipelineStageBase<?> stage, TableSchema modelSchema, TableSchema inputSchema, List<Row> data) {
    Mapper mapper = null;
    if (stage instanceof MapModel) {
        MapModel<?> mapModel = (MapModel<?>) stage;
        mapper = mapModel.mapperBuilder.apply(modelSchema, inputSchema, mapModel.getParams());
        if (data != null) {
            ((ModelMapper) mapper).loadModel(data);
        }
    } else if (stage instanceof BaseRecommender) {
        mapper = RecommenderUtil.createRecommMapper((BaseRecommender<?>) stage, modelSchema, inputSchema, data);
    } else if (stage instanceof MapTransformer) {
        MapTransformer<?> mapTransformer = (MapTransformer<?>) stage;
        mapper = mapTransformer.mapperBuilder.apply(inputSchema, mapTransformer.getParams());
    } else {
        throw new RuntimeException("not support yet.");
    }
    if (mapper instanceof ComboModelMapper) {
        ((ComboModelMapper) mapper).newMapperList();
    }
    if (mapper instanceof ComboMapper) {
        ((ComboMapper) mapper).newMapperList();
    }
    return mapper;
}
Also used : ModelMapper(com.alibaba.alink.common.mapper.ModelMapper) ComboModelMapper(com.alibaba.alink.common.mapper.ComboModelMapper) ComboMapper(com.alibaba.alink.common.mapper.ComboMapper) PipelineModelMapper(com.alibaba.alink.common.mapper.PipelineModelMapper) Mapper(com.alibaba.alink.common.mapper.Mapper) ComboMapper(com.alibaba.alink.common.mapper.ComboMapper) BaseRecommender(com.alibaba.alink.pipeline.recommendation.BaseRecommender) ModelMapper(com.alibaba.alink.common.mapper.ModelMapper) ComboModelMapper(com.alibaba.alink.common.mapper.ComboModelMapper) PipelineModelMapper(com.alibaba.alink.common.mapper.PipelineModelMapper) ComboModelMapper(com.alibaba.alink.common.mapper.ComboModelMapper)

Example 3 with BaseRecommender

use of com.alibaba.alink.pipeline.recommendation.BaseRecommender in project Alink by alibaba.

the class PipelineModel method getOutSchema.

private TableSchema getOutSchema(PipelineModel pipelineModel, TableSchema inputSchema) {
    TableSchema outSchema = inputSchema;
    for (TransformerBase<?> transformer : pipelineModel.transformers) {
        TableSchema modelSchema = null;
        if (transformer instanceof MapModel) {
            modelSchema = ((MapModel<?>) transformer).modelData.getSchema();
        } else if (transformer instanceof BaseRecommender) {
            modelSchema = ((BaseRecommender<?>) transformer).modelData.getSchema();
        }
        Mapper mapper = ModelExporterUtils.createMapperFromStage(transformer, modelSchema, outSchema, null);
        outSchema = mapper.getOutputSchema();
    }
    return outSchema;
}
Also used : 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) TableSchema(org.apache.flink.table.api.TableSchema)

Aggregations

Mapper (com.alibaba.alink.common.mapper.Mapper)3 ModelMapper (com.alibaba.alink.common.mapper.ModelMapper)3 PipelineModelMapper (com.alibaba.alink.common.mapper.PipelineModelMapper)3 BaseRecommender (com.alibaba.alink.pipeline.recommendation.BaseRecommender)3 TableSchema (org.apache.flink.table.api.TableSchema)2 ComboMapper (com.alibaba.alink.common.mapper.ComboMapper)1 ComboModelMapper (com.alibaba.alink.common.mapper.ComboModelMapper)1 BatchOperator (com.alibaba.alink.operator.batch.BatchOperator)1 TableSourceBatchOp (com.alibaba.alink.operator.batch.source.TableSourceBatchOp)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Row (org.apache.flink.types.Row)1