TensorFlow输出打印张量操作

 1、简单地使用Session会话,计算完毕后,需要关闭会话

>>> hello = tf.constant('Hello TensorFlow')
>>> sess = tf.Session()
>>> print(sess.run(hello))
b'Hello TensorFlow'
>>> sess.run(hello)    # 不使用print()也能输出
b'Hello, TensorFlow!'
>>> a = tf.constant(1)
>>> b = tf.constant(2)
>>> print(sess.run(a+b))
3
>>> sess.close()

2、使用Session会话后自动关闭

>>> hello = tf.constant('Hello, TensorFlow!')
>>> hello = tf.constant('Hello TensorFlow')
>>> a = tf.constant(1)
>>> b = tf.constant(2)
>>> with tf.Session() as sess:
...     sess.run(hello)
...     sess.run(a+b)
...
b'Hello TensorFlow'
3

猜你喜欢

转载自blog.csdn.net/sdnuwjw/article/details/84988700