Giter VIP home page Giter VIP logo

Comments (13)

hollinwilkins avatar hollinwilkins commented on August 11, 2024 1

@bipptech This is a great start. Some of the items I think we will need to account for in the interface are this:

  1. Building a schema, perhaps with a builder class?
  2. 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.
  3. 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
  4. 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.

hollinwilkins avatar hollinwilkins commented on August 11, 2024 1

@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.

#188

from mleap.

hollinwilkins avatar hollinwilkins commented on August 11, 2024 1

@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.

hollinwilkins avatar hollinwilkins commented on August 11, 2024

This will most likely be implemented as a combination of the following:

  1. Making sure case classes have constructors that are compatible with Java
  2. Builders that are Java-friendly
  3. Java-friendly wrapper classes when all else fails

from mleap.

bipptech avatar bipptech commented on August 11, 2024
    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.

hollinwilkins avatar hollinwilkins commented on August 11, 2024

@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.

bipptech avatar bipptech commented on August 11, 2024

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:

  1. Load the bundle as a model
    Model model = MLeap.loadModel("<file-path>") ;
  2. Initialize LeapFrame with schema and data
    LeapFrame frame = new LeapFrame(<schema>, <data>) ;
  3. Transform the data
    LeapFrame frame2 = model.transform(frame);

from mleap.

bipptech avatar bipptech commented on August 11, 2024

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.

hollinwilkins avatar hollinwilkins commented on August 11, 2024

@bipptech I think this is because Scala variable-length arguments are handled differently than Java, and it is expecting a Seq[String].

from mleap.

bipptech avatar bipptech commented on August 11, 2024

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.

siyouhe666 avatar siyouhe666 commented on August 11, 2024

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.

ancasarb avatar ancasarb commented on August 11, 2024

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.

marvinxu-free avatar marvinxu-free commented on August 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.