Getting Started with 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

    First, include the module as a project dependency:

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

    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