Home Cryptocurency ExchangesInstall TensorFlow for Java  |  TensorFlow
Install TensorFlow for Java  |  TensorFlow

Install TensorFlow for Java  |  TensorFlow

TensorFlow provides a
Java API
particularly useful for loading models created with Python and running them
within a Java application.

Caution: The TensorFlow Java API is not covered by the TensorFlow
API stability guarantees.

Supported Platforms

TensorFlow for Java is supported on the following systems:

  • Ubuntu 16.04 or higher; 64-bit, x86
  • macOS 10.12.6 (Sierra) or higher
  • Windows 7 or higher; 64-bit, x86

To install TensorFlow on Android, see
Android TensorFlow support
and the
TensorFlow Android Camera Demo.

TensorFlow with Apache Maven

To use TensorFlow with Apache Maven,
add the dependency to the project’s pom.xml file:

org.tensorflow
tensorflow
1.14.0

GPU support

If your system has GPU support, add the following TensorFlow
dependencies to the project’s pom.xml file:

org.tensorflow
libtensorflow
1.14.0

org.tensorflow
libtensorflow_jni_gpu
1.14.0

Example program

This example shows how to build an Apache Maven project with TensorFlow. First,
add the TensorFlow dependency to the project’s pom.xml file:

4.0.0
org.myorg
hellotensorflow
1.0-SNAPSHOT

HelloTensorFlow


1.7
1.7

org.tensorflow
tensorflow
1.14.0

Create the source file (src/main/java/HelloTensorFlow.java):

import org.tensorflow.Graph;
import org.tensorflow.Session;
import org.tensorflow.Tensor;
import org.tensorflow.TensorFlow;

public class HelloTensorFlow {
public static void main(String[] args) throws Exception {
try (Graph g = new Graph()) {
final String value = “Hello from ” + TensorFlow.version();

// Construct the computation graph with a single operation, a constant
// named “MyConst” with a value “value”.
try (Tensor 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();
}

// Execute the “MyConst” operation in a Session.
try (Session s = new Session(g);
// Generally, there may be multiple output tensors,
// all of them must be closed to prevent resource leaks.
Tensor output = s.runner().fetch(“MyConst”).run().get(0)) {
System.out.println(new String(output.bytesValue(), “UTF-8”));
}
}
}
}

Compile and execute:

mvn -q compile exec:java # Use -q to hide logging

The command outputs: Hello from version

Success: TensorFlow for Java is configured.

TensorFlow with the JDK

TensorFlow can be used with the JDK through the Java Native Interface (JNI).

Download

  1. Download the TensorFlow Jar Archive (JAR): libtensorflow.jar
  2. Download and extract the Java Native Interface (JNI) file for your operating
    system and processor support:

Note: On Windows, the native library (tensorflow_jni.dll) requires
msvcp140.dll at runtime. See the Windows build from source
guide to install the Visual C++ 2015 Redistributable.

Compile

Using the HelloTensorFlow.java file from the previous example,
compile a program that uses TensorFlow. Make sure the libtensorflow.jar is
accessible to your classpath:

javac -cp libtensorflow-1.14.0.jar HelloTensorFlow.java

Run

To execute a TensorFlow Java program, the JVM must access libtensorflow.jar and
the extracted JNI library.

Linux / mac OS

java -cp libtensorflow-1.14.0.jar:. -Djava.library.path=./jni HelloTensorFlow

Windows

java -cp libtensorflow-1.14.0.jar;. -Djava.library.path=jni HelloTensorFlow

The command outputs: Hello from version

Success: TensorFlow for Java is configured.

Build from source

TensorFlow is open source. Read
the instructions
to build TensorFlow’s Java and native libraries from source code.

Source

Leave a Reply

Your email address will not be published. Required fields are marked *

Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.