Installation

TensorFlow for Scala is currently available for Scala 2.12 and for 2.13. The main line of development is version 2.12.11. Binary release artifacts are published to the Sonatype OSS Repository Hosting service and synced to Maven Central. Currently, given the beta status of this project, only snapshot releases are published.

Library Dependencies

To include the Sonatype repositories in your SBT build and use TensorFlow for Scala, you should add the following dependency:

sbt
libraryDependencies += "org.platanios" %% "tensorflow" % "0.4.1"
Maven
<dependency>
  <groupId>org.platanios</groupId>
  <artifactId>tensorflow_2.13</artifactId>
  <version>0.4.1</version>
</dependency>
Gradle
dependencies {
  compile group: 'org.platanios', name: 'tensorflow_2.13', version: '0.4.1'
}
Note

This requires that you have installed the TensorFlow dynamic library in your system. If you haven’t, please continue reading into the following section.

Scala version

If you are not using SBT, make sure to change the artifact identifier to match the version of Scala you are using.

Dependencies

TensorFlow Dynamic Library

TensorFlow for Scala is an API for TensorFlow. In order for it work, it requires that you have the main TensorFlow dynamic library installed. You have two options for dealing with this requirement:

Using Precompiled Binaries

Add the following dependency, instead of the previous one:

sbt
libraryDependencies += "org.platanios" %% "tensorflow" % "0.4.1" classifier "linux-cpu-x86_64"
Maven
<dependency>
  <groupId>org.platanios</groupId>
  <artifactId>tensorflow_2.13</artifactId>
  <version>0.4.1</version>
  <classifier>linux-cpu-x86_64</classifier>
</dependency>
Gradle
dependencies {
  compile group: 'org.platanios', name: 'tensorflow_2.13', version: '0.4.1', classifier: 'linux-cpu-x86_64'
}
Operating System

Make sure to replace linux-cpu-x86_64 with the string that corresponds to your platform.* Currently supported platforms are: linux-cpu-x86_64, linux-gpu-x86_64, and darwin-cpu-x86_64.

Compiling TensorFlow from Scratch

Compile the TensorFlow dynamic library yourself and install it in your system. This is the recommended approach if you care about performance, but it is also significantly more complicated and time consuming.

First, clone the TensorFlow repository:

git clone https://github.com/tensorflow/tensorflow.git <repository_directory>
cd <repository_directory>
git checkout 1aaa68d93c6b2f4151446eb211399b4330c96a09

Then, compile TensorFlow using the following commands:

./configure
bazel build --config=opt --cxxopt=-D_GLIBCXX_USE_CXX11_ABI=0 //tensorflow:libtensorflow.so

For details regarding the configuration options (e.g., GPU support), please refer to the relevant main TensorFlow documentation page.

Finally, copy the bazel-bin/tensorflow/libtensorflow.so file (possibly having a different extension, depending on the platform you’re using) file in a directory that is in LD_LIBRARY_PATH, or set LD_LIBRARY_PATH appropriately.

Compiling TensorFlow Scala

If you want to compile TensorFlow for Scala yourself and the libtensorflow.so file is not placed in one of the default system libraries directories, then set -Djava.library.path=<directory> (replacing <directory> with the directory containing the libtensorflow.so file) in the .jvmopts file at the root of the TensorFlow for Scala repository.

Protocol Buffers Compiler

TensorFlow for Scala also requires protoc, the Protocol Buffers compiler (at least version 3), to be installed. You also have two options for dealing with this requirement.

Using Precompiled Binaries

Download pre-built binaries from https://github.com/google/protobuf/releases/ (choose the protoc variant appropriate for your platform) and make sure that protoc is in the PATH (either by installing it in a location in the PATH, or by adding its location to the PATH).

Installing Using a Package Manager

Install it using a package manager:

  • On Debian/Ubuntu, you can install it with APT, using the following command:

    apt-get install protobuf-compiler
  • On Mac, you can install with Homebrew, using the following command:

    brew install protobuf