Google's proud machine learning library, TensorFlow 1.0, has been released. Take a peek at what has been updated ... Java API!?
Experimental APIs for Java and Go
The fact that the Java API can be used should be called from Scala, which is the JVM language, so I tried it immediately: raised_hands:
tensorflow/tensorflow/java/README.md Simply download, unzip and deploy the JAR and native libraries according to. It looks like this on Mac OS X.
$ tree
.
├── build.sbt
├── jni
│ └── libtensorflow_jni.dylib
├── lib
│ └── libtensorflow-1.0.0-PREVIEW1.jar
└── src
└── main
└── scala
└── Main.scala
This is a test code that only calculates the element product of vector A (1, 2, 3) and vector B (4, 5, 6).
src/main/scala/Main.scala
import org.tensorflow._
object Main extends App {
val graph = new Graph()
val a = graph.opBuilder("Const", "a").
setAttr("dtype", DataType.INT32).
setAttr("value", Tensor.create(Array(1, 2, 3))).
build().
output(0)
val b = graph.opBuilder("Const", "b").
setAttr("dtype", DataType.INT32).
setAttr("value", Tensor.create(Array(4, 5, 6))).
build().
output(0)
val c = graph.opBuilder("Mul", "c").
addInput(a).
addInput(b).
build().
output(0)
val session = new Session(graph)
val out = new Array[Int](3)
session.runner().fetch("c").run().get(0).copyTo(out)
println(out.mkString(", "))
}
Please note that Graph and Session / tensorflow / Session), Tensor seems to not release resources unless you explicitly call close ()
, Be careful when writing decent code.
$ sbt run -Djava.library.path=./jni
...
4, 10, 18
Yes, we have calculated the vector C (4, 10, 18).
Machine learning, especially deep learning, has a strong Python culture, and as a person who likes statically typed languages, I was a little bit nervous, but using a language that I'm familiar with in this way raises my tension: heart_eyes: Of course, there are good Java libraries such as Deeplearning4j, but the latest learning models are often implemented in TensorFlow ...
The troublesomeness of machine learning depends largely on the data shaping of the pre-processing, and I feel that the threshold will be lowered if you can write it with one code. It seems that only the minimum Java API is prepared yet, but I hope it will become richer and richer in the future: joy :: joy :: joy:
build.sbt
name := "tensorflow-scala"
scalaVersion := "2.12.1"
Recommended Posts