1、使用Tensorflow输出Hello,World
#导入tensorflow
import tensorflow as tf
#创建一个常量张量
hello = tf.constant('Hello,World')
#Session封装了被执行操作和Tensor计算的环境
sess = tf.Session()
#Python3使用 print() Python2使用print
print(sess.run(hello))
#Session拥有像变量、队列和读取器等的资源,不用时需要释放
sess.close()
2、代码优化
使用With,实现session的自动关闭
#导入tensorflow
import tensorflow as tf
#创建一个常量张量
hello = tf.constant('Hello,World')
#使用with,实现session的自动关闭
with tf.Session() as sess:
print(sess.run(hello))
时间戳:2019.3.12 13:36:27
本文通过使用TensorFlow输出'Hello, World!'来介绍如何在Python环境中设置和运行基本的TensorFlow程序。首先,导入TensorFlow库,然后创建一个包含字符串'Hello, World!'的常量张量,接着使用Session执行张量并打印结果。为优化代码,引入with语句自动管理Session的生命周期。
4991

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



