TensorFlow基础概念之模型Graph

Graph是TensorFlow的数据流动图,包含tf.Operation集合即计算单元,还包含tf.Tensor集合,即在Operation之间流动的张量数据。

平时使用的时候不特意声明,TensorFlow会创建默认图,找默认图可以使用函数tf.get_default_graph

import tensorflow as tf
a=tf.constant(1.0)
print(tf.get_default_graph())
assert a.graph==tf.get_default_graph()

输出:<tensorflow.python.framework.ops.Graph object at 0x0000010B0060B900>

还可以创建自己的图:

import tensorflow as tf
print(tf.get_default_graph())
g=tf.Graph()
with g.as_default():
    a=tf.constant(1.0)
    print(tf.get_default_graph())
    assert a.graph==g 

输出:<tensorflow.python.framework.ops.Graph object at 0x0000012B0063B320>
            <tensorflow.python.framework.ops.Graph object at 0x0000012B07F680F0>

还可以创建多个图
 

import tensorflow as tf
print(tf.get_default_graph())
g0=tf.Graph()
with g0.as_default():
    a=tf.constant(1.0)
    print(tf.get_default_graph())
    assert a.graph==g0

g1=tf.Graph()
with g1.as_default():
    h=tf.constant(1.0)
    print(tf.get_default_graph())
    assert h.graph==g1
print(g0==g1)

输出:<tensorflow.python.framework.ops.Graph object at 0x0000012B0063B320>
           <tensorflow.python.framework.ops.Graph object at 0x0000012B07F6E668>
           <tensorflow.python.framework.ops.Graph object at 0x0000012B07F6EEF0>
           False

猜你喜欢

转载自blog.csdn.net/shiheyingzhe/article/details/81839581