TensorFlow

TensorFlow is an open-source framework for machine learning created by Google. It supports deep-learning and general numerical computations on CPUs, GPUs, and clusters of GPUs. It is subject to the terms and conditions of the Apache 2.0 License.

In the sections below, we provide guidance on installing TensorFlow on Azure Databricks and give an example of running TensorFlow programs. See Integrating Deep Learning Libraries with Apache Spark for an example of integrating a deep learning library with Spark.

Note

This guide is not a comprehensive guide on TensorFlow. See the TensorFlow website.

Install TensorFlow

TensorFlow versions included in Databricks Runtime ML

Databricks Runtime ML includes TensorFlow and TensorBoard so you can use these libraries without installing any packages. Here are the Tensorflow versions included:

  • Databricks Runtime 5.5 ML: 1.13.1
  • Databricks Runtime 5.1 - 5.4 ML: 1.12.0
  • Databricks Runtime 5.0 ML: 1.10.0

Install TensorFlow on Databricks Runtime ML

Install TensorFlow 1.14 and 2.0 Beta on Databricks Runtime 5.5 ML

Install TensorFlow as a Databricks PyPI library. Specify it as following and replace <tensorflow version> with either 1.14.0 or 2.0.0-beta1:

  • CPU: tensorflow==<tensorflow version>
  • GPU: tensorflow-gpu==<tensorflow version>

Install TensorFlow 1.14 and 2.0 Beta on Databricks Runtime 5.4 ML

Azure Databricks provides instructions for installing newer releases of TensorFlow on Databricks Runtime ML, so that you can try out the latest features in TensorFlow. Azure Databricks recommends installing newer versions of TensorFlow on Databricks Runtime 5.4 ML using init scripts. Replace <tensorflow version> with either 1.14.0 or 2.0.0-beta1 below:

  • Init script for CPU clusters:

    #!/bin/bash
    
    set -e
    
    /databricks/python/bin/python -V
    . /databricks/conda/etc/profile.d/conda.sh
    conda activate /databricks/python
    
    conda install -y -c anaconda wrapt=1.11.1
    
    pip install tensorflow==<tensorflow version>
    
  • Init script for GPU clusters:

    #!/bin/bash
    
    set -e
    
    /databricks/python/bin/python -V
    . /databricks/conda/etc/profile.d/conda.sh
    conda activate /databricks/python
    
    conda uninstall -y cudnn
    DEBIAN_FRONTEND=noninteractive sudo apt-get install -yq --no-install-recommends \
            libcudnn7=7.5.0.56-1+cuda10.0  \
            libcudnn7-dev=7.5.0.56-1+cuda10.0
    conda install -y -c anaconda wrapt=1.11.1 cudatoolkit=10.0
    
    pip install tensorflow-gpu==<tensorflow version>
    

Install TensorFlow on Databricks Runtime

Install TensorFlow on Databricks Runtime using Databricks Library Utilities by executing the command below in a notebook cell. Replace <tensorflow version> with either 1.14.0 or 2.0.0-beta1 below:

  • CPU: dbutils.library.installPyPI("tensorflow", version="<tensorflow version>" )
  • GPU: dbutils.library.installPyPI("tensorflow-gpu", version="<tensorflow version>")

Due to package dependencies, there might be compatibility issues.

TensorBoard

TensorBoard is TensorFlow’s suite of visualization tools for debugging, optimizing, and understanding TensorFlow programs.

Note

  • TensorBoard is supported in Databricks Runtime versions 5.0 and above. Earlier versions, including Databricks Runtime 4.3 and Databricks Runtime 4.1 ML, do not include TensorBoard support.
  • You must install TensorFlow as a Databricks PyPI library.

Using TensorBoard

To start TensorBoard from your notebook, use the dbutils.tensorboard utility.

dbutils.tensorboard.start("/tmp/tensorflow_log_dir")

This command displays a link that, when clicked, opens TensorBoard in a new tab.

../../../_images/tensorboard.png

TensorBoard reads from the same log directory that you write to in TensorFlow (for example, tf.summary.FileWriter("/tmp/tensorflow_log_dir", graph=sess.graph)). For the best performance, we recommend you use a local directory on the driver, for example, /tmp/tensorflow_log_dir, to store your log files and copy to persistent storage as needed.

TensorBoard continues to run until you either stop it with dbutils.tensorboard.stop() or you shut down your cluster. Only one instance of TensorBoard can run on a cluster at a time.

Note

If you attach TensorFlow to your cluster as a Databricks library, you may need to reattach your notebook before starting TensorBoard.

Use TensorFlow on a single node

To test and migrate single-machine TensorFlow workflows, you can start with a driver-only cluster on Azure Databricks by setting the number of workers to zero. Though Apache Spark is not functional under this setting, it is a cost-effective way to run single-machine TensorFlow workflows. This example shows how you can run TensorFlow, with TensorBoard monitoring on a driver-only cluster.

Spark-TensorFlow data conversion

spark-tensorflow-connector is a library within the TensorFlow ecosystem that enables conversion between Spark DataFrames and TFRecords (a popular format for storing data for TensorFlow). With spark-tensorflow-connector, you can use Spark DataFrame APIs to read TFRecords files into DataFrames and write DataFrames as TFRecords.

Installation

Note

The spark-tensorflow-connector library is included in Databricks Runtime ML, a machine learning runtime that provides a ready-to-go environment for machine learning and data science. Instead of installing the library using the instructions below, you can simply create a cluster using Databricks Runtime ML. See Overview of Databricks Runtime for Machine Learning.

To use spark-tensorflow-connector on Azure Databricks, you’ll need to build the project JAR locally, upload it to Azure Databricks, and attach it to your cluster as a library.

  1. Ensure you have Maven in your PATH (see the Maven installation instructions if needed).

  2. Clone the TensorFlow ecosystem repository and cd into the spark-tensorflow-connector subdirectory:

    git clone https://github.com/tensorflow/ecosystem
    cd ecosystem/spark/spark-tensorflow-connector
    
  3. Follow the instructions in the README to build the project locally. For the build to succeed, you may need to modify the test configuration so that tests run serially. You can do this by adding a <configuration> tag to the scalatest plugin in ecosystem/spark/spark-tensorflow-connector/pom.xml:

    <configuration>
       <parallel>false</parallel>
    </configuration>
    

    The build command prints the path of the spark-tensorflow-connector JAR, for example:

    Installing /Users/<yourusername>/ecosystem/spark/spark-tensorflow-connector/target/spark-tensorflow-connector_2.11-1.6.0.jar
    to /Users/<yourusername>/.m2/repository/org/tensorflow/spark-tensorflow-connector_2.11/1.6.0/spark-tensorflow-connector_2.11-1.6.0.jar
    
  4. Upload this JAR to Azure Databricks as a library and attach it to your cluster. You should now be able to run the example notebook (adapted from the spark-tensorflow-connector usage examples):