autodeployai / pmml4s

PMML scoring library for Scala
https://www.pmml4s.org/
Apache License 2.0
62 stars 10 forks source link

How to serialize and load model #2

Closed apoorv22 closed 4 years ago

scorebot commented 4 years ago

@apoorv22 PMML4S is just an evaluator library to consume an existing PMML, then make predictions against input data, you could refer to the default README.md for API of both Scala and Java.

It does not generate a PMML model, there are several other libraries to serialize models into PMML, for example, https://github.com/nyoka-pmml/nyoka

apoorv22 commented 4 years ago

@scorebot Thanks, I just wanted to know how can we serialize and save the Model object so that we can load it at a later point.Figured out a way to serialize :+1: One particular issue i encountered while doing the serialization was if i use the predict method on the Model and then try to serialize it fails,Here is the scenario

import java.util.Base64

val model = Model.fromFile(filePath)
val res = model.predict(Map("a" -> 1))
val encodedModel = Base64.getEncoder.encodeToString(bytes)

This is the stacktrace

Exception in thread "main" java.lang.RuntimeException: Class org.pmml4s.data.GenericMutableSeriesWithSchema does not implement Serializable or externalizable
    at org.nustaq.serialization.FSTClazzInfo.<init>(FSTClazzInfo.java:144)
    at org.nustaq.serialization.FSTClazzInfoRegistry.getCLInfo(FSTClazzInfoRegistry.java:129)
    at org.nustaq.serialization.FSTObjectOutput.getFstClazzInfo(FSTObjectOutput.java:534)
    at org.nustaq.serialization.FSTObjectOutput.writeObjectWithContext(FSTObjectOutput.java:416)
    at org.nustaq.serialization.FSTObjectOutput.writeObjectWithContext(FSTObjectOutput.java:369)
    at org.nustaq.serialization.FSTObjectOutput.writeObjectFields(FSTObjectOutput.java:664)
    at org.nustaq.serialization.FSTObjectOutput.defaultWriteObject(FSTObjectOutput.java:546)
    at org.nustaq.serialization.FSTObjectOutput.writeObjectWithContext(FSTObjectOutput.java:458)
    at org.nustaq.serialization.FSTObjectOutput.writeObjectWithContext(FSTObjectOutput.java:369)
    at org.nustaq.serialization.FSTObjectOutput.writeObjectFields(FSTObjectOutput.java:664)
    at org.nustaq.serialization.FSTObjectOutput.defaultWriteObject(FSTObjectOutput.java:546)
    at org.nustaq.serialization.FSTObjectOutput.writeObjectWithContext(FSTObjectOutput.java:458)
    at org.nustaq.serialization.FSTObjectOutput.writeObjectWithContext(FSTObjectOutput.java:369)
    at org.nustaq.serialization.FSTObjectOutput.writeObjectFields(FSTObjectOutput.java:664)
    at org.nustaq.serialization.FSTObjectOutput.defaultWriteObject(FSTObjectOutput.java:546)
    at org.nustaq.serialization.FSTObjectOutput.writeObjectWithContext(FSTObjectOutput.java:458)
    at org.nustaq.serialization.FSTObjectOutput.writeObjectWithContext(FSTObjectOutput.java:369)
    at org.nustaq.serialization.FSTObjectOutput.writeObjectFields(FSTObjectOutput.java:664)
    at org.nustaq.serialization.FSTObjectOutput.defaultWriteObject(FSTObjectOutput.java:546)
    at org.nustaq.serialization.FSTObjectOutput.writeObjectWithContext(FSTObjectOutput.java:458)
    at org.nustaq.serialization.FSTObjectOutput.writeObjectInternal(FSTObjectOutput.java:327)
    at org.nustaq.serialization.FSTObjectOutput.writeObject(FSTObjectOutput.java:294)
    at org.nustaq.serialization.FSTObjectOutput.writeObject(FSTObjectOutput.java:204)
    at org.nustaq.serialization.FSTConfiguration.asByteArray(FSTConfiguration.java:1182)
    at Parsing$.main(Parsing.scala:44)
    at Parsing.main(Parsing.scala)
scorebot commented 4 years ago

@apoorv22 I'm sorry, I misunderstood your question. I have fixed the issue of model serialization. You need to update the source code and build it by yourself, then try again. Please, let me know if you still have problem.

scorebot commented 4 years ago

@apoorv22 Did my fix resolve the serialization issue?

scorebot commented 4 years ago

@apoorv22 I close this issue now. if you have other problems, please feel free to open new issues.