不能直接用print的原因:
print只能打印输出shape的信息,而要打印输出tensor的值,需要借助 class tf.Session, class tf.InteractiveSession。
因为我们在建立graph的时候,只建立tensor的结构形状信息,并没有执行数据的操作。
法一:
>>> import tensorflow as tf
>>> a=tf.constant([
... [[1.0,2.0,3.0,4.0],
... [5.0,6.0,7.0,8.0],
... [8.0,7.0,6.0,5.0],
... [4.0,3.0,2.0,1.0]],
... [[4.0,3.0,2.0,1.0],
... [8.0,7.0,6.0,5.0],
... [1.0,2.0,3.0,4.0],
... [5.0,6.0,7.0,8.0]]
... ])
# Launch the graph in a session.
>>> sess = tf.Session()
# Evaluate the tensor `a`.
>>> print(sess.run(a))
[[[ 1. 2. 3. 4.]
[ 5. 6. 7. 8.]
[ 8. 7. 6. 5.]
[ 4. 3. 2. 1.]]
[[ 4. 3. 2. 1.]
[ 8. 7. 6. 5.]
[ 1. 2. 3. 4.]
[ 5. 6. 7. 8.]]]
法二:
>>> import tensorflow as tf
>>> a=tf.constant([
... [[1.0,2.0,3.0,4.0],
... [5.0,6.0,7.0,8.0],
... [8.0,7.0,6.0,5.0],
... [4.0,3.0,2.0,1.0]],
... [[4.0,3.0,2.0,1.0],
... [8.0,7.0,6.0,5.0],
... [1.0,2.0,3.0,4.0],
... [5.0,6.0,7.0,8.0]]
... ])
# Launch the graph in a session.
>>> with tf.Session():
... print(a.eval())
...
[[[ 1. 2. 3. 4.]
[ 5. 6. 7. 8.]
[ 8. 7. 6. 5.]
[ 4. 3. 2. 1.]]
[[ 4. 3. 2. 1.]
[ 8. 7. 6. 5.]
[ 1. 2. 3. 4.]
[ 5. 6. 7. 8.]]]