|
| 1 | +# Install TensorFlow Java |
| 2 | + |
| 3 | +[TensorFlow Java](https://github.com/tensorflow/java) can run on any JVM for |
| 4 | +building, training and deploying machine learning models. It supports both CPU |
| 5 | +and GPU execution, in graph or eager mode, and presents a rich API for using |
| 6 | +TensorFlow in a JVM environment. Java and other JVM languages, like Scala and |
| 7 | +Kotlin, are frequently used in large and small enterprises all over the world, |
| 8 | +which makes TensorFlow Java a strategic choice for adopting machine learning at |
| 9 | +a large scale. |
| 10 | + |
| 11 | +Caution: The TensorFlow Java API is *not* covered by the TensorFlow |
| 12 | +[API stability guarantees](../guide/versions.md). |
| 13 | + |
| 14 | +## Requirements |
| 15 | + |
| 16 | +TensorFlow Java runs on Java 8 and above, and supports out-of-the-box the |
| 17 | +following platforms: |
| 18 | + |
| 19 | +* Ubuntu 16.04 or higher; 64-bit, x86 |
| 20 | +* macOS 10.12.6 (Sierra) or higher; 64-bit, x86 |
| 21 | +* Windows 7 or higher; 64-bit, x86 |
| 22 | + |
| 23 | +*Note: To use TensorFlow on Android, see |
| 24 | +[TensorFlow Lite](https://tensorflow.org/lite)* |
| 25 | + |
| 26 | +## Versions |
| 27 | + |
| 28 | +TensorFlow Java has its own release cycle, independent from the |
| 29 | +[TensorFlow runtime](https://github.com/tensorflow/tensorflow). Consequently, |
| 30 | +its version does not match the version of TensorFlow runtime it runs on. Consult |
| 31 | +the TensorFlow Java |
| 32 | +[versioning table](https://github.com/tensorflow/java/#tensorflow-version-support) |
| 33 | +to list all versions available and their mapping with the TensorFlow runtime. |
| 34 | + |
| 35 | +## Artifacts |
| 36 | + |
| 37 | +There are |
| 38 | +[several ways](https://github.com/tensorflow/java/#using-maven-artifacts) to add |
| 39 | +TensorFlow Java to your project. The easiest one is to add a dependency on the |
| 40 | +`tensorflow-core-platform` artifact, which includes both the TensorFlow Java |
| 41 | +Core API and the native dependencies it requires to run on all supported |
| 42 | +platforms. |
| 43 | + |
| 44 | +You can also select one of the following extensions instead of the pure CPU |
| 45 | +version: |
| 46 | + |
| 47 | +* `tensorflow-core-platform-mkl`: Support for Intel® MKL-DNN on all platforms |
| 48 | +* `tensorflow-core-platform-gpu`: Support for CUDA® on Linux and Windows |
| 49 | + platforms |
| 50 | +* `tensorflow-core-platform-mkl-gpu`: Support for Intel® MKL-DNN and CUDA® on |
| 51 | + Linux platform. |
| 52 | + |
| 53 | +In addition, a separate dependency on the `tensorflow-framework` library can be |
| 54 | +added to benefit from a rich set of utilities for TensorFlow-based machine |
| 55 | +learning on the JVM. |
| 56 | + |
| 57 | +## Installing with Maven |
| 58 | + |
| 59 | +To include TensorFlow in your [Maven](http://maven.apache.org) application, add |
| 60 | +a dependency on its [artifacts](#artifacts) to your project's `pom.xml` file. |
| 61 | +For example, |
| 62 | + |
| 63 | +```xml |
| 64 | +<dependency> |
| 65 | + <groupId>org.tensorflow</groupId> |
| 66 | + <artifactId>tensorflow-core-platform</artifactId> |
| 67 | + <version>0.3.1</version> |
| 68 | +</dependency> |
| 69 | +``` |
| 70 | + |
| 71 | +### Reducing Number of Dependencies |
| 72 | + |
| 73 | +It is important to note that adding a dependency on a `tensorflow-core-platform` |
| 74 | +artifact will import native libraries for all supported platforms, which can |
| 75 | +significantly increase the size of your project. |
| 76 | + |
| 77 | +If you wish to target a subset of the available platforms then you can exclude |
| 78 | +the unnecessary artifacts from the other platforms using the |
| 79 | +[Maven Dependency Exclusion](https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html#dependency-exclusions) |
| 80 | +feature. |
| 81 | + |
| 82 | +Another way to select which platforms you want to include in your application is |
| 83 | +to set JavaCPP system properties, in your Maven command line or in your |
| 84 | +`pom.xml`. Please see JavaCPP |
| 85 | +[documentation](https://github.com/bytedeco/javacpp-presets/wiki/Reducing-the-Number-of-Dependencies) |
| 86 | +for more details. |
| 87 | + |
| 88 | +### Using Snapshots |
| 89 | + |
| 90 | +The latest TensorFlow Java development snapshots from the TensorFlow Java source |
| 91 | +repository are available on the [OSS Sonatype](https://oss.sonatype.org) Nexus |
| 92 | +repository. To depend on these artifacts, make sure to configure the OSS |
| 93 | +snapshots repository in your `pom.xml`. |
| 94 | + |
| 95 | +```xml |
| 96 | +<repositories> |
| 97 | + <repository> |
| 98 | + <id>tensorflow-snapshots</id> |
| 99 | + <url>https://oss.sonatype.org/content/repositories/snapshots/</url> |
| 100 | + <snapshots> |
| 101 | + <enabled>true</enabled> |
| 102 | + </snapshots> |
| 103 | + </repository> |
| 104 | +</repositories> |
| 105 | + |
| 106 | +<dependencies> |
| 107 | + <dependency> |
| 108 | + <groupId>org.tensorflow</groupId> |
| 109 | + <artifactId>tensorflow-core-platform</artifactId> |
| 110 | + <version>0.4.0-SNAPSHOT</version> |
| 111 | + </dependency> |
| 112 | +</dependencies> |
| 113 | +``` |
| 114 | + |
| 115 | +## Installing with Gradle |
| 116 | + |
| 117 | +To include TensorFlow in your [Gradle](https://gradle.org) application, add a |
| 118 | +dependency on its [artifacts](#artifacts) to your project's `build.gradle` file. |
| 119 | +For example, |
| 120 | + |
| 121 | +```groovy |
| 122 | +repositories { |
| 123 | + mavenCentral() |
| 124 | +} |
| 125 | +
|
| 126 | +dependencies { |
| 127 | + compile group: 'org.tensorflow', name: 'tensorflow-core-platform', version: '0.3.1' |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +### Reducing Number of Dependencies |
| 132 | + |
| 133 | +Excluding native artifacts from TensorFlow Java with Gradle is not as easy as |
| 134 | +with Maven. We recommend that you use Gradle JavaCPP plugins to reduce this |
| 135 | +number of dependencies. |
| 136 | + |
| 137 | +Please read at Gradle JavaCPP |
| 138 | +[documentation](https://github.com/bytedeco/gradle-javacpp) for more details. |
| 139 | + |
| 140 | +## Installing from Sources |
| 141 | + |
| 142 | +To build TensorFlow Java from sources, and possibly customize it, please read |
| 143 | +the following |
| 144 | +[instructions](https://github.com/tensorflow/java/blob/master/CONTRIBUTING.md#building). |
| 145 | + |
| 146 | +*Note: Only official builds distributed by TensorFlow are supported by its |
| 147 | +maintainers and custom builds should be used at the user's risk.* |
| 148 | + |
| 149 | +# Example Program |
| 150 | + |
| 151 | +This example shows how to build an Apache Maven project with TensorFlow. First, |
| 152 | +add the TensorFlow dependency to the project's `pom.xml` file: |
| 153 | + |
| 154 | +```xml |
| 155 | +<project> |
| 156 | + <modelVersion>4.0.0</modelVersion> |
| 157 | + <groupId>org.myorg</groupId> |
| 158 | + <artifactId>hellotensorflow</artifactId> |
| 159 | + <version>1.0-SNAPSHOT</version> |
| 160 | + |
| 161 | + <properties> |
| 162 | + <exec.mainClass>HelloTensorFlow</exec.mainClass> |
| 163 | + <!-- Minimal version for compiling TensorFlow Java is JDK 8 --> |
| 164 | + <maven.compiler.source>1.8</maven.compiler.source> |
| 165 | + <maven.compiler.target>1.8</maven.compiler.target> |
| 166 | + </properties> |
| 167 | + |
| 168 | + <dependencies> |
| 169 | + <!-- Include TensorFlow (pure CPU only) for all supported platforms --> |
| 170 | + <dependency> |
| 171 | + <groupId>org.tensorflow</groupId> |
| 172 | + <artifactId>tensorflow-core-platform</artifactId> |
| 173 | + <version>0.3.1</version> |
| 174 | + </dependency> |
| 175 | + </dependencies> |
| 176 | +</project> |
| 177 | +``` |
| 178 | + |
| 179 | +Create the source file `src/main/java/HelloTensorFlow.java`: |
| 180 | + |
| 181 | +```java |
| 182 | +import org.tensorflow.ConcreteFunction; |
| 183 | +import org.tensorflow.Signature; |
| 184 | +import org.tensorflow.Tensor; |
| 185 | +import org.tensorflow.TensorFlow; |
| 186 | +import org.tensorflow.op.Ops; |
| 187 | +import org.tensorflow.op.core.Placeholder; |
| 188 | +import org.tensorflow.op.math.Add; |
| 189 | +import org.tensorflow.types.TInt32; |
| 190 | + |
| 191 | +public class HelloTensorFlow { |
| 192 | + |
| 193 | + public static void main(String[] args) throws Exception { |
| 194 | + System.out.println("Hello TensorFlow " + TensorFlow.version()); |
| 195 | + |
| 196 | + try (ConcreteFunction dbl = ConcreteFunction.create(HelloTensorFlow::dbl); |
| 197 | + TInt32 x = TInt32.scalarOf(10); |
| 198 | + Tensor dblX = dbl.call(x)) { |
| 199 | + System.out.println(x.getInt() + " doubled is " + ((TInt32)dblX).getInt()); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + private static Signature dbl(Ops tf) { |
| 204 | + Placeholder<TInt32> x = tf.placeholder(TInt32.class); |
| 205 | + Add<TInt32> dblX = tf.math.add(x, x); |
| 206 | + return Signature.builder().input("x", x).output("dbl", dblX).build(); |
| 207 | + } |
| 208 | +} |
| 209 | +``` |
| 210 | + |
| 211 | +Compile and execute: |
| 212 | + |
| 213 | +<pre class="devsite-terminal prettyprint lang-bsh"> |
| 214 | +mvn -q compile exec:java |
| 215 | +</pre> |
| 216 | + |
| 217 | +The command prints TensorFlow version and a simple calculation. |
| 218 | + |
| 219 | +Success! TensorFlow Java is configured. |
0 commit comments