Comments (13)
@bipptech This is a great start. Some of the items I think we will need to account for in the interface are this:
- Building a schema, perhaps with a builder class?
- Building the dataset. We want to expose Java data structures and have them converted to Scala data structures. So we will need to build a map from Java to Scala types and work with that.
- For loading/serializing models, we want to account for passing in a BundleContext manually or using the default. We can encapsulate this functionality in a Java class
- We need to account for manually creating a BundleContext from Java.
I am thinking right now that all "Try" return types in MLeap can just be unwrapped and the exception thrown for Java to catch. This seems like a more canonical Java way to handle exceptions.
from mleap.
@bipptech Just introduces a PR for a simple Java interface that should handle most basic needs of MLeap runtime. Let me know if we need anything more to get you guys up and running with Java.
from mleap.
@bipptech I think this would require a fix if it's a compile time issue. Perhaps create a Java interface that LeapFrame inherits from would work well.
from mleap.
This will most likely be implemented as a combination of the following:
- Making sure case classes have constructors that are compatible with Java
- Builders that are Java-friendly
- Java-friendly wrapper classes when all else fails
from mleap.
val bundle = (for (bundleFile <- managed(BundleFile("jar:file:/tmp/mleap-model.zip"))) yield {
bundleFile.loadMleapBundle().get
}).opt.get
val schema = StructType(StructField("id", IntegerType()),
StructField("description", StringType())).get
val data = LocalDataset(Row(1, "random text"))
val frame = LeapFrame(schema, data)
val mleapPipeline = bundle.root
val frame2 = mleapPipeline.transform(frame).get
frame2.printSchema()
frame2.show()
I am writing an evaluator/predictor in java which will be deployed on tomcat. Could you help me convert above scala code in java?
from mleap.
@bipptech Executing Scala code from Java can be difficult for a variety of reasons. MLeap's biggest hurdle is all of the implicits it depends on. Let me flip this question on you. How would you like to see that code written in Java? We can work from there to design the Java interface to MLeap.
from mleap.
First of all, i would like to thank you for your great service to ML community. Earlier I used to use spark in client mode for predictions and now I use MLeap. Its performance is beyond comparison.
I wrote a scala object
object MLeapPredictor {
private val bundle = (for (bundleFile <- managed(BundleFile("jar:file:/tmp/mleap-model.zip"))) yield {
bundleFile.loadMleapBundle().get
}).opt.get
private val schema = StructType(StructField("id", IntegerType()),
StructField("description", StringType())).get
def predict(description: String) = {
val data = LocalDataset(Row(1, description))
val frame = LeapFrame(schema, data)
val mleapPipeline = bundle.root
val frame2 = mleapPipeline.transform(frame).get.select("predictedLabel").get
val data2 = frame2.dataset
data2(0).getString(0)
}
}
and called MLeapPredictor.predict("<description>")
from a java class. Both these classes (scala and java ones) are bundled in a war and deployed in tomcat. It works perfectly fine.
However, it would be nice to have a java interface so that a Java developer also gets benefited from this wonderful tool.
I would propose:
- Load the bundle as a model
Model model = MLeap.loadModel("<file-path>") ;
- Initialize LeapFrame with schema and data
LeapFrame frame = new LeapFrame(<schema>, <data>) ;
- Transform the data
LeapFrame frame2 = model.transform(frame);
from mleap.
I get a compile-time error when i try to invoke select() method on DefaultLeapFrame.
error:
error: incompatible types: String cannot be converted to Seq
frame2.select("predictedLabel");
code:
LeapFrameBuilder builder = new LeapFrameBuilder();
List<StructField> fields = new ArrayList<StructField>();
fields.add(builder.createField("id", builder.createInt()));
fields.add(builder.createField("description", builder.createString()));
fields.add(builder.createField("label", builder.createString()));
StructType schema = builder.createSchema(fields);
List<Row> rows = new ArrayList<Row>();
rows.add(builder.createRow(1, "some random text", "1"));
LocalDataset data = builder.createDataset(rows);
DefaultLeapFrame frame = builder.createFrame(schema, data);
MleapContext mleapContext = new ContextBuilder().createMleapContext();
BundleBuilder bundleBuilder = new BundleBuilder();
Transformer mleapPipeline = bundleBuilder.load(new File("/tmp/mleap-model.zip"), mleapContext).root();
DefaultLeapFrame frame2 = mleapPipeline.transform(frame).get();
frame2.select("predictedLabel");
from mleap.
@bipptech I think this is because Scala variable-length arguments are handled differently than Java, and it is expecting a Seq[String].
from mleap.
I see an overloaded method which expects String* arguments when i press ctr+space in eclipse, but somehow it's not available at compile-time.
Would this require a fix or is there something I need to do differently?
from mleap.
I get a compile-time error when i try to invoke select() method on DefaultLeapFrame.
error:
error: incompatible types: String cannot be converted to Seq frame2.select("predictedLabel");
code:
LeapFrameBuilder builder = new LeapFrameBuilder(); List<StructField> fields = new ArrayList<StructField>(); fields.add(builder.createField("id", builder.createInt())); fields.add(builder.createField("description", builder.createString())); fields.add(builder.createField("label", builder.createString())); StructType schema = builder.createSchema(fields); List<Row> rows = new ArrayList<Row>(); rows.add(builder.createRow(1, "some random text", "1")); LocalDataset data = builder.createDataset(rows); DefaultLeapFrame frame = builder.createFrame(schema, data); MleapContext mleapContext = new ContextBuilder().createMleapContext(); BundleBuilder bundleBuilder = new BundleBuilder(); Transformer mleapPipeline = bundleBuilder.load(new File("/tmp/mleap-model.zip"), mleapContext).root(); DefaultLeapFrame frame2 = mleapPipeline.transform(frame).get(); frame2.select("predictedLabel");
Hello, my leapframe type is an 80 dimension tensor, how can i define my input data like your "rows.add(builder.createRow(1, "some random text", "1"));"?
thanks
from mleap.
You can take a look at https://github.com/combust/mleap/blob/master/mleap-runtime/src/test/scala/ml/combust/mleap/runtime/javadsl/JavaDSLSpec.java#L38 where it's possible to pass in dense or sparse vectors that will be converted to tensors by mleap.
from mleap.
You can take a look at https://github.com/combust/mleap/blob/master/mleap-runtime/src/test/scala/ml/combust/mleap/runtime/javadsl/JavaDSLSpec.java#L38 where it's possible to pass in dense or sparse vectors that will be converted to tensors by mleap.
hot to get tensor value in java with index?
from mleap.
Related Issues (20)
- MLeap Transformer issue. HOT 1
- MathBinary input validation
- Mleap and python 3.8 HOT 27
- Exception in thread "Thread-4" java.lang.NoClassDefFoundError: ml/combust/bundle/HasBundleRegistry HOT 3
- Need help installing MLeap and XGBoost on databricks HOT 6
- Using XGBoost with the newest mleap=0.22.0 in Python 3.8 HOT 10
- Error while testing root project HOT 2
- org.apache.spark.sql.mleap.TypeConverters can not convert 2D tensor to Matrix
- Tensor to Proto Bug with SparseTensor: " java.lang.IllegalArgumentException: size of dimensions must equals size of values"
- org.apache.spark.ml.parity.SparkParityBase.spark should be a method
- Please release version 0.22.0 and 0.23.0 of mleap-spring-boot on dockerhub HOT 5
- Error building mleap-spring-boot HOT 7
- Mleap Spring Boot Swagger Documentation Seems Incorrect HOT 4
- Update springboot to 2.7.10 and snakeyaml to 2.0
- Reporting a security issue in MLeap HOT 1
- Question about Mleap Spring Boot API HOT 2
- How to use XGBoost PySpark API with MLeap? HOT 3
- Need Help on Contributing towards Mleap for 2.13 HOT 3
- Pyspark DecisionTreeRegressionModel bundle does not include all attributes HOT 7
- Getting Key Not Found Exception while Serializing to a mleap bundle HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mleap.