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]));
}
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;
}
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;
}
Aggregations