I am trying to get tensorflow
for java to work on Scala. I am use the tensorflow java library without any wrapper for Scala.
At sbt
I have:
If I run the HelloWord
found here, it WORKS fine, with the Scala adaptations:
import org.tensorflow.Graph
import org.tensorflow.Session
import org.tensorflow.Tensor
import org.tensorflow.TensorFlow
val g = new Graph()
val value = "Hello from " + TensorFlow.version()
val t = Tensor.create(value.getBytes("UTF-8"))
// The Java API doesn't yet include convenience functions for adding operations.
g.opBuilder("Const", "MyConst").setAttr("dtype", t.dataType()).setAttr("value", t).build();
val s = new Session(g)
val output = s.runner().fetch("MyConst").run().get(0)
However, if I try to use Scala reflection to compile the function from a string, it DOES NOT WORK. Here is the snippet I used to run:
import scala.reflect.runtime.{universe => ru}
import scala.tools.reflect.ToolBox
val fnStr = """
{() =>
import org.tensorflow.Graph
import org.tensorflow.Session
import org.tensorflow.Tensor
import org.tensorflow.TensorFlow
val g = new Graph()
val value = "Hello from " + TensorFlow.version()
val t = Tensor.create(value.getBytes("UTF-8"))
g.opBuilder("Const", "MyConst").setAttr("dtype", t.dataType()).setAttr("value", t).build();
val s = new Session(g)
s.runner().fetch("MyConst").run().get(0)
}
"""
val mirror = ru.runtimeMirror(getClass.getClassLoader)
val tb = mirror.mkToolBox()
var t = tb.parse(fnStr)
val fn = tb.eval(t).asInstanceOf[() => Any]
// and finally, executing the function
fn()
Here simplified build.sbt
to reproduce the error above:
lazy val commonSettings = Seq(
scalaVersion := "2.12.10",
libraryDependencies ++= {
Seq(
// To support runtime compilation
"org.scala-lang" % "scala-reflect" % scalaVersion.value,
"org.scala-lang" % "scala-compiler" % scalaVersion.value,
// for tensorflow4java
"org.tensorflow" % "tensorflow" % "1.15.0",
"org.tensorflow" % "proto" % "1.15.0",
"org.tensorflow" % "libtensorflow_jni" % "1.15.0"
)
}
)
lazy val `test-proj` = project
.in(file("."))
.settings(commonSettings)
When running the above, for example with sbt console
, I get the following error and stack trace:
java.lang.NoSuchMethodError: org.tensorflow.Session.runner()Lorg/tensorflow/Session$$Runner;
at __wrapper$1$f093d26a3c504d4381a37ef78b6c3d54.__wrapper$1$f093d26a3c504d4381a37ef78b6c3d54$.$anonfun$wrapper$1(<no source file>:15)
Please ignore the memory-leaks that the previous code has given that no resources context (to close()) is used