1. 程式人生 > >TensorFlow輸出列印張量操作

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