Call TensorFlow Java API from Scala

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

Announcing TensorFlow 1.0

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:

Preparation

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

code

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.

Run

$ sbt run -Djava.library.path=./jni
...
4, 10, 18

Yes, we have calculated the vector C (4, 10, 18).

Impressions

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:

environment

build.sbt


name := "tensorflow-scala"
scalaVersion := "2.12.1"

Recommended Posts

Call TensorFlow Java API from Scala
Call GitHub API from Java Socket API part2
Call Java from JRuby
Call Kotlin's sealed class from Java
Call API [Call]
Data processing using stream API from Java 8
Call GitHub's Rest API from Java's Socket API
Call Java library from C with JNI
API integration from Java with Jersey Client
Call Java method from JavaScript executed in Java
Call the Windows Notification API in Java
Hit the Salesforce REST API from Java
Call Java methods from Nim using jnim
Call API [Preparation]
Call API [Handling]
Java Stream API
Call the Microsoft Emotion API by sending image data directly from Java.
Use native libraries from Scala via Java CPP + Java
Get unixtime (seconds) from ZonedDateTime in Scala / Java
Use Matplotlib from Java or Scala with Matplotlib4j
Call API [Core Edition]
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Pack API response (java)
How to write Scala from the perspective of Java
Call Amazon Product Advertising API 5.0 (PA-API v5) in Java
Behavior when calling Java variadic methods from Scala / Kotlin / Java
[Java] Stream API / map
Eval Java source from Java
Call a method with a Kotlin callback block from Java
Docker-Client Java API Troubleshooting
6 features I missed after returning to Java from Scala
Access API.AI from Java
Java8 Stream API practice
Zabbix API in Java
Use TensorFlow from JRuby
JAVA constructor call processing
Call a program written in Swift from Processing (Java)
Call this cat API of Metadata Co., Ltd. in Java.
Implement Java Interface in JRuby class and call it from Java
How to call and use API in Java (Spring Boot)
[Kotlin] Get Java Constructor / Method from KFunction and call it
Migration from Cobol to JAVA
Java update for Scala users
Java starting from beginner, override
Creating ElasticSearch index from Java
Java Stream API cheat sheet
New features from Java7 to Java8
Use Face API from Ruby
Java Stream API in 5 minutes
Connect from Java to PostgreSQL
Multi-stage selection (Java / Groovy / Scala)
Java, instance starting from beginner
[Java] Stream API --Stream termination processing
[Java] Stream API --Stream intermediate processing
Java starting from beginner, inheritance
Java life starting from scratch
[Java] Introduction to Stream API
From Ineffective Java to Effective Java
JavaScript as seen from Java
Java Basic Learning Content 8 (Java API)