title: How to print a Tensor Flow variable to 2 decimal places?
body: <pre><code>weights = tf.Variable(tf.truncated_normal([2,3]))
  with tf.Session() as sess:
      sess.run(tf.global_variables_initializer())
      print('Weights:')
      print(sess.run(weights))
  
      print("{0:2f}".format(sess.run(weights)))
  </code></pre>
  
  <p>The first print statement works as expected. </p>
  
  <pre><code>Weights:
  [[ 0.30919516  0.29567152  0.11157229] 
   [ 0.26642913 -0.21269836 -0.58886886]]
  </code></pre>
  
  <p>The second print statement with str.format() gives the below error.</p>
  
  <pre><code>---------------------------------------------------------------------------
  TypeError                                 Traceback (most recent call last)
  &lt;ipython-input-20-b0e4f93d01f8&gt; in &lt;module&gt;()
    4     print('Weights:')
    5     print(sess.run(weights))
  ----&gt; 6     print("{0:2f}".format(sess.run(weights)))
  
  &gt; TypeError: non-empty format string passed to object.__format__
  </code></pre>
  
  <p>In addition to the below answer, I also found np.set_printoptions(precision=2) also works.</p>
  
  <pre><code>np.set_printoptions(precision=2)
  weights = tf.Variable(tf.truncated_normal([2,3]))
  with tf.Session() as sess:
      sess.run(tf.global_variables_initializer())
      print('Weights:')
      print(sess.run(weights)) 
  
  Weights:
  [[ 0.01 -0.42 -1.57]
   [-0.44  1.62  0.27]]
  </code></pre>