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)
The snippet:
n.attr.get('value').tensor.int_val[0]
gets the value of constant if it is a single number
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: