理解TensorFlow的Graph和Session

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_39198406/article/details/84863343
  1. 当我们import tensorflow的时候,就已经创建了一个默认的图了。
import tensorflow as tf
print(tf.get_default_graph())
$ result:
<tensorflow.python.framework.ops.Graph object at 0x7fe0ae6b2208>
  1. 有新的操作,默认添加到默认图中
import tensorflow as tf
c = tf.constant(value=1)
print(c.graph)
print(tf.get_default_graph())
$ result:
<tensorflow.python.framework.ops.Graph object at 0x7f39496a90f0>
<tensorflow.python.framework.ops.Graph object at 0x7f39496a90f0>
  1. 自定义一个新的图并在新的图中做操作
import tensorflow as tf
print(tf.get_default_graph())
g = tf.Graph()
print(g)
with g.as_default():
    d = tf.constant(value=2)
    print(d.graph)
f = tf.constant(value=2)
print(f.graph)
$ result:
<tensorflow.python.framework.ops.Graph object at 0x7f0debc51dd8>
<tensorflow.python.framework.ops.Graph object at 0x7f0debbad160>
<tensorflow.python.framework.ops.Graph object at 0x7f0debbad160>

你会发现,新建的图和默认图是不一样,在with外面做的操作又属于默认图了

  1. as_default前面必须使用with才有效
import tensorflow as tf
print(tf.get_default_graph())
g2 = tf.Graph()
print("g2:", g2)
g2.as_default()
e = tf.constant(value=15)
print(e.graph)
$ result:
<tensorflow.python.framework.ops.Graph object at 0x7f6a1cd14da0>
g2: <tensorflow.python.framework.ops.Graph object at 0x7f6a1cc70240>
<tensorflow.python.framework.ops.Graph object at 0x7f6a1cd14da0>
  1. session包含了操作对象执行的环境
import tensorflow as tf
# 向默认图中加入操作
a = tf.constant(2.)
b = tf.constant(5.)
c = a * b
# 创建会话
sess = tf.Session()
# 传入参数,即代表在计算流中计算这个参数
r = sess.run(c)
print(r)
# 关闭会话
sess.close()
  1. session上下文管理器
import tensorflow as tf
# 向默认图中加入操作
a = tf.constant(2.)
b = tf.constant(5.)
c = a * b
# 使用上下文管理器
with tf.Session() as sess:
    r = sess.run(c)
print(r)

参考:https://www.cnblogs.com/fydeblog/p/7429344.html
参考:https://www.tensorflow.org/api_docs/python/tf/Graph
参考:https://blog.csdn.net/xierhacker/article/details/53860379

猜你喜欢

转载自blog.csdn.net/weixin_39198406/article/details/84863343