Difference between initializing a Tensorflow Variable using np.zeros and using tf.zeros

Viewed 1946

Are there some differences between initializing a tensorflow Variable using np.zeros and using tf.zeros?

For example, if we take a look at the MNIST softmax tutorial ( https://github.com/tensorflow/tensorflow/blob/r1.1/tensorflow/examples/tutorials/mnist/mnist_softmax.py), the variables W and b are initialized in the following way.

  W = tf.Variable(tf.zeros([784, 10]))
  b = tf.Variable(tf.zeros([10]))

Instead of initializing using tf.zeros, the code still works fine if we use np.zeros as follows.

  W = tf.Variable(np.zeros([784, 10], dtype=np.float32))
  b = tf.Variable(np.zeros([10], dtype=np.float32))

So, I think there might be no difference. But, I tried the following code segment assuming that the following a and b variables are the same.

a = tf.Variable(tf.zeros((3, 2)), tf.float32)
b = tf.Variable(np.zeros((3, 2)), tf.float32)
sess = tf.InteractiveSession()
init = tf.global_variables_initializer()
sess.run(init)
a.eval()

The result of a.eval() is as follows:

array([[ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.]], dtype=float32)

But the result of b.eval() is as follows:

array([[ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.]])

So, even though the values are the same, there is a difference in dtype. Could you please explain to me why this difference exists?

1 Answers
Related