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
本文详细介绍了如何在TensorFlow中使用Session进行基本运算,包括创建常量、执行计算以及正确关闭会话的方法。通过实例展示了使用Session进行字符串和数值运算的过程。
1426

被折叠的 条评论
为什么被折叠?



