0

I used a loop to iterate over the TensorFlow graph and retrieve the values of constant Tensors.

I tried to find a similar way to retrieve the values of Variable tensors by iterating through the elements but I did not find any solution.

Here is a sample code: The session run method is already invoked. This loop iterates over the graph and retrieves the values of Tensor Constants.

for n in tf.get_default_graph().as_graph_def().node:
    if 'Const' in n.name:
        if not n.attr["value"].tensor.tensor_shape.dim:
            const[n.name] = n.attr.get('value').tensor.int_val[0]
        else:
            const[n.name] = tensor_util.MakeNdarray(n.attr['value'].tensor)
  1. The snippet:

    n.attr.get('value').tensor.int_val[0]

    gets the value of constant if it is a single number

  2. otherwise the statement bellow retrieves the values of the tensor and stores them into a ndarray.

    tensor_util.MakeNdarray(n.attr['value'].tensor)

So I tried this:

if 'Variable' in n.name:
    var = tensor_util.MakeNdarray(n.attr['value'].tensor)

I am aware that I can retrieve the values of the variables with session run() or eval() methods for the specified elements. But here I would like to loop over the graph elements.

Related links:

  1. How do I get the current value of a Variable?

  2. https://www.tensorflow.org/guide/variables

  3. How to access tensor_content values in TensorProto in TensorFlow?

user2394863
  • 11
  • 1
  • 4

1 Answers1

0

Resolved:

After debugging and observing the TensorBoard graph I realized that only the Variable_weights/initial_value node has the actual values after session run. So the solution above is working since Variable_weights/initial_value is a constant TensorFlow node that is automatically created and is the input to the assigned node of TensorFlow Variable.

Every Variable in the TensorFlow graph has 4 operations/nodes. The Variable, Identity, Assign and Const which is the initial_value.

At first, I thought that the 'Variable' node will return the values.

But eventually, I invoked the snippet bellow in "initial_value" node and gets the values.

Solution:

if 'Variable_weights/initial_value' in n.name: var = tensor_util.MakeNdarray(n.attr['value'].tensor)

user2394863
  • 11
  • 1
  • 4