天天看点

敲代码啦!——Tensorflow_01_Hello,World!

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

继续阅读