TensorFlow example
Vertica uses the TFIntegration UDX package to integrate with TensorFlow. You can train your models outside of your Vertica database, then import them to Vertica and make predictions on your data.
TensorFlow scripts and datasets are included in the GitHub repository under Machine-Learning-Examples/TensorFlow.
The example below creates a Keras (a TensorFlow API) neural network model trained on the MNIST handwritten digit classification dataset, the layers of which are shown below.
The data is fed through each layer from top to bottom, and each layer modifies the input before returning a score. In this example, the data passed in is a set of images of handwritten Arabic numerals and the output would be the probability of the input image being a particular digit:
inputs = keras.Input(shape=(28, 28, 1), name="image")
x = layers.Conv2D(32, 5, activation="relu")(inputs)
x = layers.MaxPooling2D(2)(x)
x = layers.Conv2D(64, 5, activation="relu")(x)
x = layers.MaxPooling2D(2)(x)
x = layers.Flatten()(x)
x = layers.Dense(10, activation='softmax', name='OUTPUT')(x)
tfmodel = keras.Model(inputs, x)
For more information on how TensorFlow interacts with your Vertica database and how to import more complex models, see TensorFlow integration and directory structure.
Prepare a TensorFlow model for Vertica
The following procedures take place outside of Vertica.
Train and save a TensorFlow model
-
Train your model. For this particular example, you can run train_simple_model.py to train and save the model. Otherwise, and more generally, you can manually train your model in Python and then save it:
$ mymodel.save('my_saved_model_dir')
-
Run the
freeze_tf2_model.py
script included in the Machine-Learning-Examples repository or inopt/vertica/packages/TFIntegration/examples
, specifying your model, an output directory (optional, defaults tofrozen_tfmodel
), and the input and output column type (0 for primitive, 1 for complex).This script transforms your saved model into the Vertica-compatible frozen graph format and creates the
tf_model_desc.json
file, which describes how Vertica should translate its tables to TensorFlow tensors:$ ./freeze_tf2_model.py path/to/tf/model frozen_model_dir 0
Import TensorFlow models and make predictions in Vertica
-
If you haven't already, as
dbadmin
, install the TFIntegration UDX package on any node. You only need to do this once.$ /opt/vertica/bin/admintools -t install_package -d database_name -p 'password' --package TFIntegration
-
Copy the the
directory
to any node in your Vertica cluster and import the model:=> SELECT IMPORT_MODELS('path/to/frozen_model_dir' USING PARAMETERS category='TENSORFLOW');
-
Import the dataset you want to make a prediction on. For this example:
-
Copy the
Machine-Learning-Examples/TensorFlow/data
directory to any node on your Vertica cluster. -
From that
data
directory, run the SQL scriptload_tf_data.sql
to load the MNIST dataset:$ vsql -f load_tf_data.sql
-
-
Make a prediction with your model on your dataset with PREDICT_TENSORFLOW. In this example, the model is used to classify the images of handwritten numbers in the MNIST dataset:
=> SELECT PREDICT_TENSORFLOW (* USING PARAMETERS model_name='tf_mnist_keras', num_passthru_cols=1) OVER(PARTITION BEST) FROM tf_mnist_test_images; --example output, the skipped columns are displayed as the first columns of the output ID | col0 | col1 | col2 | col3 | col4 | col5 | col6 | col7 | col8 | col9 ----+------+------+------+------+------+------+------+------+------+------ 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 3 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 6 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 ...
Note
Because acolumn-type
of 0 was used when calling thefreeze_tf2_model.py
script, the PREDICT_TENSORFLOW function must be used to make predictions. Ifcolumn-type
was 1, meaning the model accepts complex input and output types, predictions must be made with the PREDICT_TENSORFLOW_SCALAR function. -
To export the model with EXPORT_MODELS:
=> SELECT EXPORT_MODELS('/path/to/export/to', 'tf_mnist_keras'); EXPORT_MODELS --------------- Success (1 row)
Note
While you cannot continue training a TensorFlow model after you export it from Vertica, you can use it to predict on data in another Vertica cluster, or outside Vertica on another platform.TensorFlow 1 (deprecated)
-
Install TensorFlow 1.15 with Python 3.7 or below.
-
Run
train_save_model.py
inMachine-Learning-Examples/TensorFlow/tf1
to train and save the model in TensorFlow and frozen graph formats.Note
Vertica does not supply a script to save and freeze TensorFlow 1 models that use complex column types.