A Beginner Guide to Get the Output and Weights from an Existing TensorFlow Model – TensorFlow Tutorial

By | October 25, 2021

In order to get the output or weights in an existing tensorflow model, you can do by following steps:

A Beginner Guide to Get the Output and Weights from an Existing TensorFlow Model - TensorFlow Tutorial

Step 1: Load an existing model

First, we should load the existing tensorflow model in your application script. We can use saver.restore() to load.

Here is the tutorial:

Steps to Load TensorFlow Model Using saver.restore() Correctly – TensorFlow Tutorial

After you have loaded the model, you can get its output and all weights in it.

Step 2: Get the output and weights in model

In tensorflow, we can use graph.get_operation_by_name(variable_name).outputs[0] to get the variable.

Here variable_name is the name of variable in your loaded tensorflow model.

For example, you may create a input tensor in your model.

        with tf.name_scope('input'):
            self.input_x = tf.placeholder(tf.float32, [None, self.feature_dim, None, 1], name="input_x")

You have trained this model, saved it. After you loaded it in another application, you can get this input variable as follows:

input_x = graph.get_operation_by_name("input/input_x").outputs[0]

Here input/input_x the name of variable self.input_x.

In order to get the name of all variables in one model, you can read this tutorial:

List All Variables including Constant and Placeholder in TensorFlow – TensorFlow Tutorial

Step 3: Print the value of weights in model

After getting the variable of a model, we can get its value and print it.

Here is the example:

inputx = sess.run(input_x)
print(inputx)

Leave a Reply