Use TensorFlow in Deephaven

This guide shows how to use TensorFlow in Deephaven queries.

TensorFlow is an open-source machine learning library for Python, and one of the most widely used. Its high-level Keras API supports building, training, and deploying models.

Deephaven's server Docker image doesn't include TensorFlow. To use it, install it yourself or choose one of the Deephaven Docker deployments that includes it:

Both examples below classify the Iris dataset from Deephaven's examples repository. The first example uses TensorFlow with pandas. The second reads the data into a Deephaven table and uses deephaven.learn to make predictions, first on a static table and then on a live table that gains rows as new data arrives.

Note

In this guide, we read data from Deephaven's examples repository. You can also load files from the /data directory in the Docker container, which Deephaven's Docker Compose deployments mount from ./data in the deployment directory. See Docker bind mounts to learn more about the relation between locations in the container and the local file system.

Classify the Iris dataset with pandas

This first example shows how to use TensorFlow to classify Iris flowers from measurements.

The Iris flower dataset is common in introductory machine learning applications. R.A. Fisher introduced the dataset in his 1936 paper The use of multiple measurements in taxonomic problems. It contains 150 samples, 50 from each of three species of Iris flower. For each Iris-setosa, Iris-virginica, and Iris-versicolor flower, the dataset records these measurements in centimeters:

  • Petal length
  • Petal width
  • Sepal length
  • Sepal width

Predicting a flower's species from labeled measurements is a classification problem, so it suits a supervised machine learning algorithm. We use a feed-forward neural network, in which data flows from the input layer through the hidden layers to the output layer with no loops.

The steps are to define the network, train it on most of the data, and test it on the rest. Its accuracy is the fraction of test-set predictions that match the known species.

First, we import the packages the example needs.

Next, we import the data, convert the Class labels from flower names to the integers 0, 1, and 2, and split the data into training and testing sets. The random_state argument fixes the shuffle, so the split is the same every run.

With that done, we can define and create our neural network. We use a simple feed-forward neural network with two hidden layers. Both hidden layers use the ReLU (rectified linear unit) activation function. The output layer has three units, one per species, and uses softmax to turn them into class probabilities. Setting a random seed first fixes the network's starting weights, so training gives the same result each run.

Now comes the fun part: training the network. We use sparse categorical cross entropy to calculate loss, and the Adam algorithm to optimize the network. Training runs for 100 epochs. An epoch is one full pass over the training data.

model.evaluate prints the loss and accuracy on the test set. Without the fixed seeds, the data split and the starting weights would be random, and the exact numbers would vary between runs.

Let's break down the code above into reusable steps that apply to a variety of supervised machine learning problems:

  • Import data from an external source.
  • Convert the class labels to integers.
  • Split the data into training and testing sets.
  • Define the machine learning model.
  • Set the loss calculation and optimization routines.
  • Train the model over a set number of training epochs.
  • Calculate the model's accuracy.

So we just classified the Iris dataset with a simple feed-forward neural network. That's kind of cool, but it would be cooler to classify a live feed of incoming data. Let's do that with Deephaven!

Classify the Iris dataset with Deephaven tables

This example reads the same Iris data into a Deephaven table, trains a model with the same architecture on all 150 rows with deephaven.learn, and uses it to predict species first on that static table and then on a live, append-only table that gains rows as new data arrives.

deephaven.learn works on static tables and on add-only and blink ticking tables. Append-only tables are a kind of add-only table. On any other ticking table, it raises an error. For tables that modify or remove rows, see AI/ML workflows.

Train and predict on a static table

First, we import everything the example needs, including what the live-table step uses to write synthetic data: the DynamicTableWriter class, the deephaven.dtypes module, and the random, threading, and time modules.

As in the pandas example, we next import the data and convert the class labels, but this time into a Deephaven table with read_csv. The get_class_number function maps each class name to the same integers as the first example, and update calls it on every row. The -> np.intc type hint makes the replacement Class column an int column.

Next, we define the same neural network as in the first example: two ReLU hidden layers and a three-unit softmax output layer. As before, the seed fixes the starting weights.

The learn function passes a table's data to a model in batches of up to batch_size rows. Three of its arguments define what the model reads, what runs, and what it writes:

  • inputs is a list of learn.Input objects. Each one names the columns to read and a function that gathers them into an array. This learn.Input is unrelated to the Keras Input layer in the model.
  • model_func is the function that receives the gathered arrays.
  • outputs is a list of learn.Output objects. Each one names a new column and a function that fills it.

See How to use deephaven.learn for details.

Because learn calls the model through model_func, we wrap training in a function, train_model, and add a second function, predict_with_model, that returns the most likely class (0, 1, or 2) for each row of input features.

learn also calls the functions you pass to each learn.Input and learn.Output:

  • table_to_array_double and table_to_array_int are learn.Input functions. Each calls gather.table_to_numpy_2d to gather the current batch's rows from the named columns into a NumPy array of doubles or integers.
  • get_predicted_class is a learn.Output function. learn calls it once per row, with the model's predictions for the batch and that row's position in the batch. Its return value fills the row's PredictedClass cell.

Now we can train the model and predict the Iris species in a Deephaven table with learn.

The first call trains the model, so it has no outputs (outputs=None). The second call uses the trained model to predict the Iris species of each row and stores the predictions in a new PredictedClass column.

iris_predicted_static is the iris table plus a PredictedClass column, next to the known Class column.

This example trains on every row, so these are predictions on the training data. They don't measure accuracy. To measure it, hold out a test set as the pandas example does, train on the other rows, and compare PredictedClass with Class on the held-out rows.

Predict on a live table

So far, we've made static predictions on static data, only this time in a Deephaven table. That's not that exciting. What we really want is to predict on a live data feed as it arrives.

The model trained in Train and predict on a static table can do that. This step reuses the imports and functions defined there. It writes synthetic Iris measurements to a live table and predicts the species of each new row.

We can create an in-memory real-time table using DynamicTableWriter. The table it produces is append-only, so the prediction table keeps every row. learn works on this table because append-only is one of the table types learn supports.

DynamicTableWriter writes one row at a time, which is what this example needs. For most other live data, the real-time table guide recommends table_publisher.

To create synthetic measurements, we use these ranges from the Iris dataset:

ColumnMinimum (cm)Maximum (cm)
PetalLengthCM1.06.9
PetalWidthCM0.12.5
SepalLengthCM4.37.9
SepalWidthCM2.04.4

The write_to_iris function uses these ranges to write one synthetic measurement to the table each second for one minute.

First, we create a live table and write data to it.

Now we use learn on the live table. The call matches the static prediction call above, except that the table argument is now live_iris, the append-only table from DynamicTableWriter. That's all it takes to move from static to live predictions.

As new measurements arrive, iris_predicted_live gains rows with a PredictedClass value for each one: