Use PyTorch in Deephaven
This guide shows you how to use PyTorch in Deephaven queries.
PyTorch is an open-source machine learning library for Python, widely used for deep learning, with a Pythonic API for building and training neural networks. Given Deephaven's strength in real-time and big-data processing, it is natural to pair the two.
The two examples below solve the same problem: classifying the Iris dataset from Deephaven's examples repository. The first example uses PyTorch with pandas, whereas the second integrates Deephaven tables to perform predictions on live data.
Install PyTorch
PyTorch is not included in Deephaven's default server Docker image. To use it within Deephaven, you can install it yourself or choose one of the Deephaven Docker deployments that includes it:
- Without example data:
- With example data:
The Iris dataset
The Iris flower dataset is a popular dataset commonly used in introductory machine learning applications. Introduced by R.A. Fisher 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. The following values are measured in centimeters for Iris-setosa, Iris-virginica, and Iris-versicolor flowers:
- Petal length
- Petal width
- Sepal length
- Sepal width
Predicting a flower's species from these measurements is a classification problem suitable for a supervised machine learning algorithm.
Note
In this guide, we read data from Deephaven's examples repository. You can also load files from the /data directory, which the Deephaven Docker Compose deployments mount into the container 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 uses PyTorch and pandas to classify Iris flowers from measurements. We define and create a feed-forward neural network, train it, and test it. In a feed-forward network, data passes through each layer once, in one direction, from input to output. We measure accuracy as the percentage of test-set predictions that match the known classes.
Let's first import all the packages we need.
Next, we import our data, encode the class labels we want to predict as integers, 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 activation function. The seed fixes the network's starting weights, so training gives the same result every run.
Now comes the fun bit: training our network. Each training epoch is one full pass of the training data through the network. We calculate loss using cross entropy, and we optimize the network with the Adam algorithm.
Lastly, we check how well our model worked.
A simple model like this typically classifies most of the test set correctly. Without the fixed seeds, the data split and the starting weights would be random, and the exact numbers would vary between runs.
This example follows a basic formula for using a neural network to solve a supervised learning problem:
- Import data from an external source and encode the target labels as integers.
- Split the data into training and testing sets, and convert them to tensors.
- 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 on the testing set.
In this case, a simple neural network is suitable for a simple dataset.
Classify the Iris dataset with Deephaven tables
So we just classified the Iris dataset using a simple feed-forward neural network. That's kind of cool, but the dataset is static. It would be cooler to classify a live feed of incoming data, and with Deephaven, the same model can do that.
Let's get there in two steps. First, we train the model and make predictions on a static Deephaven table. Then we use the trained model on a ticking table, a table whose rows change as new data arrives. The deephaven.learn package connects the model to both tables.
Train and predict on a static table
We start by importing everything we need.
Just as we did before, we next import our data and encode the class labels as integers. This time, we use read_csv to import it into a Deephaven table. The get_class_number function maps each class name to the same integers as the first example, and update calls it from a query string on every row to replace the Class strings with those integers.
Next, we define and create the neural network. It is the same IrisANN network as in the first example, with one added line, x = x.float(). The gather function later in this section delivers the measurements as 64-bit doubles, and this line converts them to the 32-bit floats the network's layers use. As before, the seed fixes the starting weights.
This time, the learn function runs the model on table data. It gathers table columns into model inputs, calls a model function on each batch of rows, and scatters the results into columns of an output table. Use deephaven.learn for AI/ML applications covers each step in detail.
learn needs three kinds of function:
- A model function, which trains the model or makes predictions.
- A gather function for each input.
- A scatter function for each output.
This guide calls learn twice, once to train and once to predict, so it needs two model functions. We start with those, wrapping training and prediction so that learn can call them. The training loop is the same as in the first example, except that it converts the targets with long. The gather function delivers the class labels as 32-bit integers, and CrossEntropyLoss expects 64-bit integers.
Next, we define the functions that learn passes data through: two gather functions and one scatter function. The gather functions use gather.table_to_numpy_2d to copy table data into NumPy arrays, and then torch.from_numpy turns each array into a PyTorch tensor.
table_to_tensor_doublegathers the measurement columns into a tensor of 64-bit doubles, which is whyforwardconverts its input withfloat.table_to_tensor_intgathers theClasscolumn into a tensor of 32-bit integers, whichtrain_modelconverts withlongto the 64-bit integers thatCrossEntropyLossexpects.get_predicted_classextracts one prediction as an integer.learncalls it once per row to fill the new output column.
Now we pass these functions to learn, which runs them on the iris table. The first call to learn trains the model. The second call uses the trained model to predict each row's species and writes the predictions to a new PredictedClass column.
The iris_predicted_static table shows each row's PredictedClass beside its known Class, using the encoding from iris_mappings: 0 is Iris-setosa, 1 is Iris-virginica, and 2 is Iris-versicolor.
We've done much the same thing as before: static predictions on static data. The difference is that we use a Deephaven table, and the model trains and predicts on all 150 rows rather than on separate training and testing sets. That's not that exciting. We really want to perform the prediction stage on a live data feed.
Predict on live data
To demonstrate predictions on live data, we create simulated Iris measurements in a ticking table. We can create an in-memory ticking table using DynamicTableWriter. To keep the simulated measurements realistic, we use the range of each measurement in the Iris dataset:
| Column | Minimum (cm) | Maximum (cm) |
|---|---|---|
PetalLengthCM | 1.0 | 6.9 |
PetalWidthCM | 0.1 | 2.5 |
SepalLengthCM | 4.3 | 7.9 |
SepalWidthCM | 2.0 | 4.4 |
The write_to_iris function below uses these ranges to write a simulated measurement to the table once per second for 30 seconds. We apply our model to those measurements as they arrive and predict which Iris species they belong to.
First, we create a ticking table and write data to it.
Now we use learn on the ticking table. All it takes to change from static to live data is one argument: the call is the same as the static prediction above, except that the table argument is live_iris. On a ticking table, learn:
- Processes only the rows added in each update cycle, in batches of at most
batch_sizerows, so each new measurement gets a prediction as it arrives. - Works on add-only and blink tables. By default, it raises an error on other ticking tables.
DynamicTableWriter produces an append-only table, which is a kind of add-only table, so it qualifies. For tables whose rows are modified or removed, use one of the approaches in AI/ML workflows.
As new measurements arrive, iris_predicted_live gains rows with a PredictedClass value for each one. The values use the encoding from iris_mappings: 0 is Iris-setosa, 1 is Iris-virginica, and 2 is Iris-versicolor.
Related documentation
- Docker bind mounts
- Incremental update model
- Table types
- Install and run with Docker
- How to install and use Python packages
- How to use
deephaven.learn - AI/ML workflows
- How to use scikit-learn
- How to use TensorFlow
- How to use TensorBoard
- Python functions in query strings
- How to write data to an in-memory, real-time table
DynamicTableWriterread_csvupdatedeephaven.learnPydocdeephaven.learn.gatherPydoc