<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Vertica Documentation – TensorFlow models</title>
    <link>/en/data-analysis/ml-predictive-analytics/using-external-models-with/tensorflow-models/</link>
    <description>Recent content in TensorFlow models on Vertica Documentation</description>
    <generator>Hugo -- gohugo.io</generator>
    
	  <atom:link href="/en/data-analysis/ml-predictive-analytics/using-external-models-with/tensorflow-models/index.xml" rel="self" type="application/rss+xml" />
    
    
      
        
      
    
    
    <item>
      <title>Data-Analysis: TensorFlow integration and directory structure</title>
      <link>/en/data-analysis/ml-predictive-analytics/using-external-models-with/tensorflow-models/tensorflow-integration-and-directory-structure/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/data-analysis/ml-predictive-analytics/using-external-models-with/tensorflow-models/tensorflow-integration-and-directory-structure/</guid>
      <description>
        
        
        &lt;p&gt;This page covers importing Tensorflow models into Vertica, making predictions on data in the Vertica database, and exporting the model for use on another Vertica cluster or a third-party platform.&lt;/p&gt;
&lt;p&gt;For a start-to-finish example through each operation, see &lt;a href=&#34;../../../../../en/data-analysis/ml-predictive-analytics/using-external-models-with/tensorflow-models/tensorflow-example/&#34;&gt;TensorFlow example&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Vertica supports models created with either TensorFlow 1.x and 2.x, but 2.x is strongly recommended.&lt;/p&gt;
&lt;p&gt;To use TensorFlow with Vertica, install the TFIntegration UDX package on any node. You only need to do this once:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;$ /opt/vertica/bin/admintools -t install_package -d &lt;span class=&#34;code-variable&#34;&gt;database_name&lt;/span&gt; -p &amp;#39;&lt;span class=&#34;code-variable&#34;&gt;password&lt;/span&gt;&amp;#39; --package TFIntegration
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&#34;directory-and-file-structure-for-tensorflow-models&#34;&gt;Directory and file structure for TensorFlow models&lt;/h2&gt;
&lt;p&gt;Before importing your models, you should have a separate directory for each model that contains one of each of the following. Note that Vertica uses the directory name as the model name when you import it:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;&lt;code&gt;model_name&lt;/code&gt;&lt;/em&gt;&lt;code&gt;.pb&lt;/code&gt;: a trained model in frozen graph format&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;tf_model_desc.json&lt;/code&gt;: a description of the model&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can generate both files for a given TensorFlow 2 model with the script &lt;a href=&#34;https://github.com/vertica/Machine-Learning-Examples/blob/master/TensorFlow/freeze_tf2_model.py&#34;&gt;&lt;code&gt;freeze_tf2_model.py&lt;/code&gt;&lt;/a&gt; included in &lt;code&gt;Machine-Learning-Examples/TensorFlow&lt;/code&gt; repository (or in &lt;code&gt;opt/vertica/packages/TFIntegration/examples&lt;/code&gt;), though you may need to make modifications to the description based on your use-case and how you want your Vertica database to interact with your model:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;$ python3 Tensorflow/freeze_tf2_model.py your/model/directory
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;For example, the &lt;code&gt;tf_models&lt;/code&gt; directory contains two models, &lt;code&gt;tf_mnist_estimator&lt;/code&gt; and &lt;code&gt;tf_mnist_keras&lt;/code&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;tf_models/
├── tf_mnist_estimator
│   ├── mnist_estimator.pb
│   └── tf_model_desc.json
└── tf_mnist_keras
    ├── mnist_keras.pb
    └── tf_model_desc.json
&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&#34;tf_model_descjson&#34;&gt;tf_model_desc.json&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;tf_model_desc.json&lt;/code&gt; file forms the bridge between TensorFlow and Vertica. It describes the structure of the model so that Vertica can correctly match up its inputs and outputs to input/output tables.&lt;/p&gt;
&lt;p&gt;Notice that the &lt;code&gt;freeze_tf2_model.py&lt;/code&gt; script automatically generates this file for your model, and this generated file can often be used as-is. For more complex models or use cases, you might have to edit this file. For a detailed breakdown of each field, see &lt;a href=&#34;../../../../../en/data-analysis/ml-predictive-analytics/using-external-models-with/tensorflow-models/tf-model-desc-json-overview/&#34;&gt;tf_model_desc.json overview&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;importing-tensorflow-models-into-vertica&#34;&gt;Importing TensorFlow models into Vertica&lt;/h2&gt;
&lt;p&gt;To import TensorFlow models, use &lt;a href=&#34;../../../../../en/sql-reference/functions/ml-functions/model-management/import-models/&#34;&gt;IMPORT_MODELS&lt;/a&gt; with the category &lt;code&gt;&#39;TENSORFLOW&#39;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Import a single model. Keep in mind that the Vertica database uses the directory name as the model name:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;select IMPORT_MODELS ( &amp;#39;/path/tf_models/tf_mnist_keras&amp;#39; USING PARAMETERS category=&amp;#39;TENSORFLOW&amp;#39;);
 import_models
---------------
 Success
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Import all models in the directory (where each model has its own directory) with a wildcard (*):&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;select IMPORT_MODELS (&amp;#39;/path/tf_models/*&amp;#39; USING PARAMETERS category=&amp;#39;TENSORFLOW&amp;#39;);
 import_models
---------------
 Success
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;predicting-with-an-imported-tensorflow-model&#34;&gt;Predicting with an imported TensorFlow model&lt;/h2&gt;
&lt;p&gt;After importing your TensorFlow model, you can use the model to predict on data in a Vertica table.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&#34;../../../../../en/sql-reference/functions/ml-functions/transformation-functions/predict-tensorflow/&#34;&gt;PREDICT_TENSORFLOW&lt;/a&gt; function is different from the other predict functions in that it does not accept any parameters that affect the input columns such as &amp;quot;exclude_columns&amp;quot; or &amp;quot;id_column&amp;quot;; rather, the function always predicts on all the input columns provided. However, it does accept a &lt;code&gt;num_passthru_cols&lt;/code&gt; parameter which allows the user to &amp;quot;skip&amp;quot; some number of input columns, as shown below.&lt;/p&gt;
&lt;p&gt;The OVER(PARTITION BEST) clause tells Vertica to parallelize the operation across multiple nodes. See 
Window Partition Clause for details:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; select PREDICT_TENSORFLOW (*
                   USING PARAMETERS model_name=&amp;#39;tf_mnist_keras&amp;#39;, 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
...
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;exporting-tensorflow-models&#34;&gt;Exporting TensorFlow models&lt;/h2&gt;
&lt;p&gt;Vertica exports the model as a frozen graph, which can then be re-imported at any time. Keep in mind that models that are saved as a frozen graph cannot be trained further.&lt;/p&gt;
&lt;p&gt;Use &lt;a href=&#34;../../../../../en/sql-reference/functions/ml-functions/model-management/export-models/&#34;&gt;EXPORT_MODELS&lt;/a&gt; to export TensorFlow models. For example, to export the &lt;code&gt;tf_mnist_keras&lt;/code&gt; model to the &lt;code&gt;/path/to/export/to&lt;/code&gt; directory:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT EXPORT_MODELS (&amp;#39;/path/to/export/to&amp;#39;, &amp;#39;tf_mnist_keras&amp;#39;);
 export_models
---------------
 Success
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;When you export a TensorFlow model, the Vertica database creates and uses the specified directory to store files that describe the model:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;$ ls tf_mnist_keras/
crc.json  metadata.json  mnist_keras.pb  model.json  tf_model_desc.json
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The &lt;code&gt;.pb&lt;/code&gt; and &lt;code&gt;tf_model_desc.json&lt;/code&gt; files describe the model, and the rest are organizational files created by the Vertica database.

&lt;table class=&#34;table table-bordered&#34; &gt;



&lt;tr&gt; 

&lt;th &gt;
File Name&lt;/th&gt; 

&lt;th &gt;
Purpose&lt;/th&gt;&lt;/tr&gt;

&lt;tr&gt; 

&lt;td &gt;
&lt;code&gt;model_name.pb&lt;/code&gt;&lt;/td&gt; 

&lt;td &gt;
Frozen graph of the model.&lt;/td&gt;&lt;/tr&gt;

&lt;tr&gt; 

&lt;td &gt;
&lt;code&gt;tf_model_desc.json&lt;/code&gt;&lt;/td&gt; 

&lt;td &gt;
Describes the model.&lt;/td&gt;&lt;/tr&gt;

&lt;tr&gt; 

&lt;td &gt;
&lt;code&gt;crc.json&lt;/code&gt;&lt;/td&gt; 

&lt;td &gt;
Keeps track of files in this directory and their sizes. It is used for importing models.&lt;/td&gt;&lt;/tr&gt;

&lt;tr&gt; 

&lt;td &gt;
&lt;code&gt;metadata.json&lt;/code&gt;&lt;/td&gt; 

&lt;td &gt;
Contains Vertica version, model type, and other information.&lt;/td&gt;&lt;/tr&gt;

&lt;tr&gt; 

&lt;td &gt;
&lt;code&gt;model.json&lt;/code&gt;&lt;/td&gt; 

&lt;td &gt;
More verbose version of &lt;code&gt;tf_model_desc.json&lt;/code&gt;.&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/p&gt;
&lt;h2 id=&#34;see-also&#34;&gt;See also&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/data-analysis/ml-predictive-analytics/using-external-models-with/tensorflow-models/&#34;&gt;TensorFlow models&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/data-analysis/ml-predictive-analytics/using-external-models-with/tensorflow-models/tensorflow-example/&#34;&gt;TensorFlow example&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/sql-reference/functions/ml-functions/model-management/import-models/&#34;&gt;IMPORT_MODELS&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/sql-reference/functions/ml-functions/model-management/export-models/&#34;&gt;EXPORT_MODELS&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/sql-reference/functions/ml-functions/transformation-functions/predict-tensorflow/&#34;&gt;PREDICT_TENSORFLOW&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

      </description>
    </item>
    
    <item>
      <title>Data-Analysis: TensorFlow example</title>
      <link>/en/data-analysis/ml-predictive-analytics/using-external-models-with/tensorflow-models/tensorflow-example/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/data-analysis/ml-predictive-analytics/using-external-models-with/tensorflow-models/tensorflow-example/</guid>
      <description>
        
        
        &lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;TensorFlow scripts and datasets are included in the GitHub repository under &lt;a href=&#34;https://github.com/vertica/Machine-Learning-Examples/tree/master/TensorFlow&#34;&gt;Machine-Learning-Examples/TensorFlow&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The example below creates a Keras (a TensorFlow API) neural network model trained on the &lt;a href=&#34;http://yann.lecun.com/exdb/mnist/&#34;&gt;MNIST handwritten digit classification dataset&lt;/a&gt;, the layers of which are shown below.&lt;/p&gt;
&lt;p&gt;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:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;inputs = keras.Input(shape=(28, 28, 1), name=&amp;#34;image&amp;#34;)
x = layers.Conv2D(32, 5, activation=&amp;#34;relu&amp;#34;)(inputs)
x = layers.MaxPooling2D(2)(x)
x = layers.Conv2D(64, 5, activation=&amp;#34;relu&amp;#34;)(x)
x = layers.MaxPooling2D(2)(x)
x = layers.Flatten()(x)
x = layers.Dense(10, activation=&amp;#39;softmax&amp;#39;, name=&amp;#39;OUTPUT&amp;#39;)(x)
tfmodel = keras.Model(inputs, x)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;For more information on how TensorFlow interacts with your Vertica database and how to import more complex models, see &lt;a href=&#34;../../../../../en/data-analysis/ml-predictive-analytics/using-external-models-with/tensorflow-models/tensorflow-integration-and-directory-structure/&#34;&gt;TensorFlow integration and directory structure&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;prepare-a-tensorflow-model-for-vertica&#34;&gt;Prepare a TensorFlow model for Vertica&lt;/h2&gt;
&lt;p&gt;The following procedures take place outside of Vertica.&lt;/p&gt;
&lt;h3 id=&#34;train-and-save-a-tensorflow-model&#34;&gt;Train and save a TensorFlow model&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;https://www.tensorflow.org/install&#34;&gt;Install TensorFlow 2&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Train your model. For this particular example, you can run &lt;a href=&#34;https://github.com/vertica/Machine-Learning-Examples/tree/master/TensorFlow/train_simple_model.py&#34;&gt;train_simple_model.py&lt;/a&gt; to train and save the model. Otherwise, &lt;a href=&#34;https://www.tensorflow.org/tutorials/quickstart/beginner&#34;&gt; and more generally, you can manually train your model in Python&lt;/a&gt; and then &lt;a href=&#34;https://www.tensorflow.org/tutorials/keras/save_and_load&#34;&gt;save it&lt;/a&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;$ mymodel.save(&amp;#39;my_saved_model_dir&amp;#39;)
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Run the &lt;code&gt;freeze_tf2_model.py&lt;/code&gt; script included in the &lt;a href=&#34;https://github.com/vertica/Machine-Learning-Examples/tree/master/TensorFlow&#34;&gt;Machine-Learning-Examples repository&lt;/a&gt; or in &lt;code&gt;opt/vertica/packages/TFIntegration/examples&lt;/code&gt;, specifying your model and an output directory (optional, defaults to &lt;code&gt;frozen_tfmodel&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;This script transforms your saved model into the Vertica-compatible frozen graph format and creates the &lt;code&gt;tf_model_desc.json&lt;/code&gt; file, which describes how Vertica should translate its tables to TensorFlow tensors:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;$ ./freeze_tf2_model.py path/to/tf/model frozen_model_dir
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;import-tensorflow-models-and-make-predictions-in-vertica&#34;&gt;Import TensorFlow models and make predictions in Vertica&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;If you haven&#39;t already, as &lt;code&gt;dbadmin&lt;/code&gt;, install the TFIntegration UDX package on any node. You only need to do this once. &lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;$ /opt/vertica/bin/admintools -t install_package -d &lt;span class=&#34;code-variable&#34;&gt;database_name&lt;/span&gt; -p &amp;#39;&lt;span class=&#34;code-variable&#34;&gt;password&lt;/span&gt;&amp;#39; --package TFIntegration
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Copy the the &lt;code&gt;directory&lt;/code&gt; to any node in your Vertica cluster and import the model:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT IMPORT_MODELS(&amp;#39;path/to/frozen_model_dir&amp;#39; USING PARAMETERS category=&amp;#39;TENSORFLOW&amp;#39;);
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Import the dataset you want to make a prediction on. For this example:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Copy the &lt;code&gt;Machine-Learning-Examples/TensorFlow/data&lt;/code&gt; directory to any node on your Vertica cluster.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;From that &lt;code&gt;data&lt;/code&gt; directory, run the SQL script &lt;code&gt;load_tf_data.sql&lt;/code&gt; to load the MNIST dataset:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;$ vsql -f load_tf_data.sql
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Make a prediction with your model on your dataset with &lt;a href=&#34;../../../../../en/sql-reference/functions/ml-functions/transformation-functions/predict-tensorflow/&#34;&gt;PREDICT_TENSORFLOW&lt;/a&gt;. In this example, the model is used to classify the images of handwritten numbers in the MNIST dataset:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT PREDICT_TENSORFLOW (*
                   USING PARAMETERS model_name=&amp;#39;tf_mnist_keras&amp;#39;, 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
...
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;To export the model with &lt;a href=&#34;../../../../../en/sql-reference/functions/ml-functions/model-management/export-models/&#34;&gt;EXPORT_MODELS&lt;/a&gt;:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT EXPORT_MODELS(&amp;#39;/path/to/export/to&amp;#39;, &amp;#39;tf_mnist_keras&amp;#39;);
 EXPORT_MODELS
---------------
 Success
(1 row)
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&#34;alert admonition note&#34; role=&#34;alert&#34;&gt;
&lt;h4 class=&#34;admonition-head&#34;&gt;Note&lt;/h4&gt;

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.

&lt;/div&gt;
&lt;h2 id=&#34;tensorflow-1-deprecated&#34;&gt;TensorFlow 1 (deprecated)&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;https://www.tensorflow.org/install/pip&#34;&gt;Install TensorFlow 1.15&lt;/a&gt; with Python 3.7 or below.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Run &lt;code&gt;train_save_model.py&lt;/code&gt; in &lt;code&gt;Machine-Learning-Examples/TensorFlow/tf1&lt;/code&gt; to train and save the model in TensorFlow and frozen graph formats.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;see-also&#34;&gt;See also&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/data-analysis/ml-predictive-analytics/using-external-models-with/tensorflow-models/&#34;&gt;TensorFlow models&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/extending/udxs/&#34;&gt;User-defined extensions&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

      </description>
    </item>
    
    <item>
      <title>Data-Analysis: tf_model_desc.json overview</title>
      <link>/en/data-analysis/ml-predictive-analytics/using-external-models-with/tensorflow-models/tf-model-desc-json-overview/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/data-analysis/ml-predictive-analytics/using-external-models-with/tensorflow-models/tf-model-desc-json-overview/</guid>
      <description>
        
        
        &lt;p&gt;Before importing your externally trained TensorFlow models, you must:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;save the model in frozen graph (&lt;code&gt;.pb&lt;/code&gt;) format&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;create &lt;code&gt;tf_model_desc.json&lt;/code&gt;, which describes to your Vertica database how to map its inputs and outputs to input/output tables&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Conveniently, the script &lt;a href=&#34;https://github.com/vertica/Machine-Learning-Examples/blob/master/TensorFlow/freeze_tf2_model.py&#34;&gt;&lt;code&gt;freeze_tf2_model.py&lt;/code&gt;&lt;/a&gt; included in the &lt;code&gt;TensorFlow&lt;/code&gt; directory of the &lt;a href=&#34;https://github.com/vertica/Machine-Learning-Examples&#34;&gt;Machine-Learning-Examples&lt;/a&gt; repository (and in &lt;code&gt;opt/vertica/packages/TFIntegration/examples&lt;/code&gt;) will do both of these automatically. In most cases, the generated &lt;code&gt;tf_model_desc.json&lt;/code&gt; can be used as-is, but for more complex datasets and use cases, you might need to edit it.&lt;/p&gt;
&lt;h2 id=&#34;tf_model_descjson-for-an-example-dataset&#34;&gt;tf_model_desc.json for an example dataset&lt;/h2&gt;
&lt;p&gt;The following &lt;code&gt;tf_model_desc.json&lt;/code&gt; is generated from the MNIST handwriting dataset used by the &lt;a href=&#34;../../../../../en/data-analysis/ml-predictive-analytics/using-external-models-with/tensorflow-models/tensorflow-example/&#34;&gt;TensorFlow example&lt;/a&gt;.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;{
    &amp;#34;frozen_graph&amp;#34;: &amp;#34;mnist_keras.pb&amp;#34;,
    &amp;#34;input_desc&amp;#34;: [
        {
            &amp;#34;op_name&amp;#34;: &amp;#34;image_input&amp;#34;,
            &amp;#34;tensor_map&amp;#34;: [
                {
                    &amp;#34;idx&amp;#34;: 0,
                    &amp;#34;dim&amp;#34;: [
                        1,
                        28,
                        28,
                        1
                    ],
                    &amp;#34;col_start&amp;#34;: 0
                }
            ]
        }
    ],
    &amp;#34;output_desc&amp;#34;: [
        {
            &amp;#34;op_name&amp;#34;: &amp;#34;OUTPUT/Softmax&amp;#34;,
            &amp;#34;tensor_map&amp;#34;: [
                {
                    &amp;#34;idx&amp;#34;: 0,
                    &amp;#34;dim&amp;#34;: [
                        1,
                        10
                    ],
                    &amp;#34;col_start&amp;#34;: 0
                }
            ]
        }
    ]
}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This file describes the structure of the model&#39;s inputs and outputs. It must contain a &lt;code&gt;frozen_graph&lt;/code&gt; field that matches the filename of the .pb model, an &lt;strong&gt;input_desc&lt;/strong&gt; field, and an &lt;em&gt;output_desc&lt;/em&gt; field.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;input_desc&lt;/strong&gt; and &lt;strong&gt;output_desc&lt;/strong&gt;: the descriptions of the input and output nodes in the TensorFlow graph. Each of these include the following fields:
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;op_name&lt;/strong&gt;: the name of the operation node which is set when creating and training the model. You can typically retrieve the names of these parameters from &lt;em&gt;&lt;code&gt;tfmodel&lt;/code&gt;&lt;/em&gt;&lt;code&gt;.inputs&lt;/code&gt; and &lt;em&gt;&lt;code&gt;tfmodel&lt;/code&gt;&lt;/em&gt;&lt;code&gt;.outputs&lt;/code&gt;. For example:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;
$ print({t.name:t for t in tfmodel.inputs})
{&amp;#39;image_input:0&amp;#39;: &amp;lt;tf.Tensor &amp;#39;image_input:0&amp;#39; shape=(?, 28, 28, 1) dtype=float32&amp;gt;}
&lt;/code&gt;&lt;/pre&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;$ print({t.name:t for t in tfmodel.outputs})
{&amp;#39;OUTPUT/Softmax:0&amp;#39;: &amp;lt;tf.Tensor &amp;#39;OUTPUT/Softmax:0&amp;#39; shape=(?, 10) dtype=float32&amp;gt;}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In this case, the respective values for &lt;strong&gt;op_name&lt;/strong&gt; would be the following.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;input_desc&lt;/code&gt;: &lt;code&gt;image_input&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;output_desc&lt;/code&gt;: &lt;code&gt;OUTPUT/Softmax&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For a more detailed example of this process, review the code for &lt;code&gt;freeze_tf2_model.py&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;em&gt;tensor_map&lt;/em&gt;: how to map the tensor to Vertica columns, which can be specified with the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;idx&lt;/strong&gt;: the index of the tensor (should be 0 for the first input/output, 1 for the second input/output, etc.)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;dim&lt;/strong&gt;: the vector holding the dimensions of the tensor; it provides the number of columns&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;col_start&lt;/strong&gt; (only used if &lt;strong&gt;col_idx&lt;/strong&gt; is not specified): the starting column index. When used with &lt;strong&gt;dim&lt;/strong&gt;, it specifies a range of indices of Vertica columns starting at &lt;strong&gt;col_start&lt;/strong&gt; and ending at &lt;strong&gt;col_start&lt;/strong&gt;+&lt;strong&gt;dim&lt;/strong&gt;; Vertica starts at the column specified by the index &lt;strong&gt;col_start&lt;/strong&gt; and gets the next &lt;strong&gt;dim&lt;/strong&gt; columns&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;col_idx&lt;/strong&gt;: the indices in the Vertica columns corresponding to the flattened tensors. This allows you explicitly specify the indices of the Vertica columns that couldn&#39;t otherwise be specified as a simple range with &lt;strong&gt;col_start&lt;/strong&gt; and &lt;strong&gt;dim&lt;/strong&gt; (e.g. 1, 3, 5, 7)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;data_type&lt;/strong&gt; (not shown): the data type of the input or output, one of the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;TF_FLOAT (default)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;TF_DOUBLE&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;TF_INT8&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;TF_INT16&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;TF_INT32&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;TF_INT64&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;complex-tf_model_descjson-example&#34;&gt;Complex tf_model_desc.json example&lt;/h3&gt;
&lt;p&gt;Below is a more complex example that includes multiple inputs and outputs:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;{
    &amp;#34;input_desc&amp;#34;: [
        {
            &amp;#34;op_name&amp;#34;: &amp;#34;input1&amp;#34;,
            &amp;#34;tensor_map&amp;#34;: [
                {
                    &amp;#34;idx&amp;#34;: 0,
                    &amp;#34;dim&amp;#34;: [
                        4
                    ],
                    &amp;#34;col_idx&amp;#34;: [
                        0,
                        1,
                        2,
                        3
                    ]
                },
                {
                    &amp;#34;idx&amp;#34;: 1,
                    &amp;#34;dim&amp;#34;: [
                        2,
                        2
                    ],
                    &amp;#34;col_start&amp;#34;: 4
                }
            ]
        },
        {
            &amp;#34;op_name&amp;#34;: &amp;#34;input2&amp;#34;,
            &amp;#34;tensor_map&amp;#34;: [
                {
                    &amp;#34;idx&amp;#34;: 0,
                    &amp;#34;dim&amp;#34;: [],
                    &amp;#34;col_idx&amp;#34;: [
                        8
                    ]
                },
                {
                    &amp;#34;idx&amp;#34;: 1,
                    &amp;#34;dim&amp;#34;: [
                        2
                    ],
                    &amp;#34;col_start&amp;#34;: 9
                }
            ]
        }
    ],
    &amp;#34;output_desc&amp;#34;: [
        {
            &amp;#34;op_name&amp;#34;: &amp;#34;output&amp;#34;,
            &amp;#34;tensor_map&amp;#34;: [
                {
                    &amp;#34;idx&amp;#34;: 0,
                    &amp;#34;dim&amp;#34;: [
                        2
                    ],
                    &amp;#34;col_start&amp;#34;: 0
                }
            ]
        }
    ]
}
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;see-also&#34;&gt;See also&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/data-analysis/ml-predictive-analytics/using-external-models-with/tensorflow-models/&#34;&gt;TensorFlow models&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/data-analysis/ml-predictive-analytics/using-external-models-with/tensorflow-models/tensorflow-example/&#34;&gt;TensorFlow example&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

      </description>
    </item>
    
  </channel>
</rss>
