无法在 tensorflow 中打印 tf.zeros

Unable to print tf.zeros in tensorflow

weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels]))
biases = tf.Variable(tf.zeros([num_labels]))`

这是我遇到的在tensorflow中使用梯度下降最小化损失的部分代码。 我了解发生了什么以及 tf.zeros 正在做什么,但是当我尝试 运行 以下代码时它显示错误::

 sess = tf.IntearctiveSession()
 tensor = tf.Variable(tf.zeros(shape=(10)))
 print(tensor.eval())
 sess.close()

print(tensor.eval()) 中发生错误。 谁能指出我哪里理解错了?

你好像忘了 initialize your variable(s)。试试这个:

sess = tf.IntearctiveSession()
tensor = tf.Variable(tf.zeros(shape=(10)))
sess.run(tf.global_variables_initializer())  # Now all variables are initialized
print(tensor.eval())
sess.close()