提示: 本文是模型部署方案的一部分
依赖
步骤
- 加载反序列化模型文件为PMML对象
- 优化模型,并写到新模型文件
我们开始吧
maven依赖
1 2 3 4 5 6 7 8 9 10 11 12 13
| <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <pmml-model.version>1.4.2</pmml-model.version> </properties>
<dependency> <groupId>org.jpmml</groupId> <artifactId>pmml-model</artifactId> <version>${pmml-model.version}</version> </dependency>
|
加载模型 & 更新模型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| package org.houqian.jpmml;
import com.fasterxml.jackson.core.JsonProcessingException; import org.dmg.pmml.PMML; import org.jpmml.model.PMMLUtil; import org.jpmml.model.visitors.LocatorNullifier; import org.xml.sax.SAXException;
import javax.xml.bind.JAXBException; import java.io.*;
public class Load {
public static void main(String[] args) throws FileNotFoundException, JAXBException, SAXException, JsonProcessingException { FileInputStream srcModelIs = new FileInputStream(new File("src/main/resources/pipeline.pmml.xml")); PMML pmml = PMMLUtil.unmarshal(srcModelIs);
optimize(pmml);
FileOutputStream newModelOs = new FileOutputStream(new File("src/main/resources/new-pipeline.pmml.xml")); store(pmml, newModelOs); }
public static void optimize(PMML pmml){ LocatorNullifier nullifier = new LocatorNullifier(); nullifier.applyTo(pmml); }
public static void store(PMML pmml, OutputStream os) throws JAXBException { org.jpmml.model.PMMLUtil.marshal(pmml, os); } }
|
笔者没有找到如何验证以上操作和模型正确性的关系,现在姑且先让它这样不报错跑通吧。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| ➜ src git:(master) ✗ tree -L 3 . ├── main │ ├── java │ │ ├── com │ │ └── org │ └── resources │ ├── application.properties │ ├── application.yml │ ├── import.sql │ ├── kafka │ ├── new-pipeline.pmml.xml <--生成的新模型文件 │ └── pipeline.pmml.xml <--之前sklearn输出的模型文件 └── test └── java └── org
9 directories, 5 files
|
参考
- https://github.com/jpmml/jpmml-model