Getting Started with Tensorflow

MLeap Tensorflow integration provides support for including Tensorflowgraphs as a transform step in your ML pipelines. In the future we mayprovide more compatibility. For right now, Tensorflow integration shouldbe considered experimental as both Tensorflow and MLeap integration withTensorflow are still stabilizing.

Building MLeap-Tensorflow

MLeap Tensorflow modules are not included in maven central, and mustinstead be built from source along with the Tensorflow JNI support. Seeinstructions for buildingthe Tensorflow module.

Using MLeap-Tensorflow

Once you have everything built, it’s easy to incorporate Tensorflow intoyour MLeap pipelines.

First, include the module as a project dependency:

  1. libraryDependencies += "ml.combust.mleap" %% "mleap-tensorflow" % "0.13.0"

Then we can start using Tensorflow graphs, let’s build a simple one thatmultiplies two tensors:

  1. import ml.combust.mleap.core.types._
  2. import ml.combust.mleap.runtime.frame.{DefaultLeapFrame, Row}
  3. import ml.combust.mleap.tensor.Tensor
  4. import ml.combust.mleap.tensorflow.{TensorflowModel, TensorflowTransformer}
  5. import org.tensorflow
  6. // Initialize our Tensorflow demo graph
  7. val graph = new tensorflow.Graph
  8. // Build placeholders for our input values
  9. val inputA = graph.opBuilder("Placeholder", "InputA").
  10. setAttr("dtype", tensorflow.DataType.FLOAT).
  11. build()
  12. val inputB = graph.opBuilder("Placeholder", "InputB").
  13. setAttr("dtype", tensorflow.DataType.FLOAT).
  14. build()
  15. // Multiply the two placeholders and put the result in
  16. // The "MyResult" tensor
  17. graph.opBuilder("Mul", "MyResult").
  18. setAttr("T", tensorflow.DataType.FLOAT).
  19. addInput(inputA.output(0)).
  20. addInput(inputB.output(0)).
  21. build()
  22. // Build the MLeap model wrapper around the Tensorflow graph
  23. val model = TensorflowModel(graph,
  24. // Must specify inputs and input types for converting to TF tensors
  25. inputs = Seq(("InputA", TensorType.Float()), ("InputB", TensorType.Float())),
  26. // Likewise, specify the output values so we can convert back to MLeap
  27. // Types properly
  28. outputs = Seq(("MyResult", TensorType.Float())))
  29. // Connect our Leap Frame values to the Tensorflow graph
  30. // Inputs and outputs
  31. val shape = NodeShape().
  32. // Column "input_a" gets sent to the TF graph as the input "InputA"
  33. withInput("InputA", "input_a").
  34. // Column "input_b" gets sent to the TF graph as the input "InputB"
  35. withInput("InputB", "input_b").
  36. // TF graph output "MyResult" gets placed in the leap frame as col
  37. // "my_result"
  38. withOutput("MyResult", "my_result")
  39. // Create the MLeap transformer that executes the TF model against
  40. // A leap frame
  41. val transformer = TensorflowTransformer(shape = shape, model = model)
  42. // Create a sample leap frame to transform with the Tensorflow graph
  43. val schema = StructType(StructField("input_a", ScalarType.Float), StructField("input_b", ScalarType.Float)).get
  44. val dataset = Seq(Row(5.6f, 7.9f),
  45. Row(3.4f, 6.7f),
  46. Row(1.2f, 9.7f))
  47. val frame = DefaultLeapFrame(schema, dataset)
  48. // Transform the leap frame and make sure it behaves as expected
  49. val data = transformer.transform(frame).get.dataset
  50. assert(data(0)(2).asInstanceOf[Tensor[Float]].get(0).get == 5.6f * 7.9f)
  51. assert(data(1)(2).asInstanceOf[Tensor[Float]].get(0).get == 3.4f * 6.7f)
  52. assert(data(2)(2).asInstanceOf[Tensor[Float]].get(0).get == 1.2f * 9.7f)
  53. // Cleanup the transformer
  54. // This closes the TF session and graph resources
  55. transformer.close()

For more information on how Tensorflow integration works:

  1. Details on data conversion and integration here.
  2. How we serialize MLeap bundles with Tensorflow graphs here