title: How to retrieve all the tf.Variable values after initialization by looping through the graph object
body: <p>I used a loop to iterate over the TensorFlow graph and retrieve the values of constant Tensors.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <pre><code>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)
  </code></pre>
  
  <ol>
  <li><p>The snippet:</p>
  
  <p>n.attr.get('value').tensor.int_val[0]</p>
  
  <p>gets the value of constant if it is a single number</p></li>
  <li><p>otherwise the statement bellow retrieves the values of the tensor and stores them into a ndarray.</p>
  
  <p>tensor_util.MakeNdarray(n.attr['value'].tensor)</p></li>
  </ol>
  
  <p>So I tried this:</p>
  
  <pre><code>if 'Variable' in n.name:
      var = tensor_util.MakeNdarray(n.attr['value'].tensor)
  </code></pre>
  
  <p>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.</p>
  
  <p>Related links: </p>
  
  <ol>
  <li><p><a href="https://stackoverflow.com/questions/33679382/how-do-i-get-the-current-value-of-a-variable">How do I get the current value of a Variable?</a></p></li>
  <li><p><a href="https://www.tensorflow.org/guide/variables" rel="nofollow noreferrer">https://www.tensorflow.org/guide/variables</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/44125403/how-to-access-tensor-content-values-in-tensorproto-in-tensorflow">How to access tensor_content values in TensorProto in TensorFlow?</a></p></li>
  </ol>