TensorFlow 图的相关操作

本章节介绍图的相关操作,其他部分学习请前往:TensorFlow 学习目录

目录

一、默认图

二、新建图

三、tf.reset_default_graph()函数的使用

四、如何获得图中的tensor?

五、如何获得图中的OP?

六、获取列表元素

七、获取对象


我们知道,TensorFlow的运行机制,是根据一张图纸铺设“管道”,然后等“管道”架设完毕之后再通入水流,现在假设要对一个楼进行管道的铺设,那么这个楼即需要供应自来水的“管道”,又需要供暖的“管道”,而且这两个管道都是独立的“管道”,现在问题来了,如何在一个py文件中建设多个图呢?这就是本章节需要解决的问题。

一、默认图

就像此前的学习中我们发现,如果在一个py文件中进行TensorFlow的开发编程,我们发现没有涉及到任何有关图的建立之类的代码,这是因为,编译的时候自动生成了一个默认的图,我们编写的所有tensor、OP都是属于这个默认图的。

import tensorflow as tf

c = tf.constant("how are you???")
g1 = tf.get_default_graph()
print (g1)

输出,这里就是这个默认图的地址

<tensorflow.python.framework.ops.Graph object at 0x000002230FC018D0>

二、新建图

通过实例化tf.Graph()来新建一个新的图,并在其中定义一些tensor、OP在其中

import tensorflow as tf

g = tf.Graph()
with g.as_default():
    c1 = tf.constant("hello world!!!")
    print (c1.graph)
    print (g)

输出,通过途中张量也可以知道其所在的是哪个图,可以看到,此时的图的地址信息和默认图已经不一样了,说明,在这其中定义的tensor和OP是在新图下进行规划的。

<tensorflow.python.framework.ops.Graph object at 0x0000022329ECC080>
<tensorflow.python.framework.ops.Graph object at 0x0000022329ECC080>

三、tf.reset_default_graph()函数的使用

该函数是用来重置原先的默认图,也就是将原先的默认图“撕掉”不要了,重新规划一个新的默认图,但是要有一个前提,就是上一个默认图的所有操作必须释放掉,也就是说,如果上一个图有Session,那么一定要在Session被close之后,才可以使用该函数,不然是会报错误的。

import tensorflow as tf

c = tf.constant("how are you???")
g1 = tf.get_default_graph()
print (g1)

tf.reset_default_graph()
g2 = tf.get_default_graph()
print (g2)

a = tf.constant([[1, 2]])
print (a.graph)

输出,第一行是原先老图的地址,现在通过该函数reset之后,“图纸”更新了,并且可以看到之后定义的tensor其所在的默认图的地址是reset之后的默认图的(第二三两行),而不是之前的默认图的地址。

<tensorflow.python.framework.ops.Graph object at 0x00000254AE3D1898>
<tensorflow.python.framework.ops.Graph object at 0x00000254C76CFFD0>
<tensorflow.python.framework.ops.Graph object at 0x00000254C76CFFD0>

四、如何获得图中的tensor?

如果想要获得图中的tensor,可以通过函数g.get_tensor_by_name()函数来获取tensor。

import tensorflow as tf

a = tf.constant([[1, 2]])
b = tf.constant([[1], [3]])

t1 = tf.matmul(a, b, name='matmul')
print (t1.name)
print (t1)
test = tf.get_default_graph().get_tensor_by_name('matmul:0')
print (test)

输出,通过默认图中tensor t1 的名字:matmul:0,来获取tensor可以通过下面的第二三行结果发现,结果是一致的。

matmul:0
Tensor("matmul:0", shape=(1, 1), dtype=int32)
Tensor("matmul:0", shape=(1, 1), dtype=int32)

五、如何获得图中的OP?

如果想要获得图中的OP,可以通过函数g.get_operation_by_name()函数来获取tensor。

import tensorflow as tf

a = tf.constant([[1, 2]])
b = tf.constant([[1], [3]])

t1 = tf.matmul(a, b, name='matmul')
print(t1.op.name)
testop = tf.get_default_graph().get_operation_by_name('matmul')
print (testop)

输出,通过第一行,我们知道matmul操作在默认图中的名字是“matmul”,而剩下的所有操作都是通过该函数得到的输出,我们可以看到其中第一个说明,name:matmul,可以知道这就是我们所需要寻找到操作。

matmul
name: "matmul"
op: "MatMul"
input: "Const"
input: "Const_1"
attr {
  key: "T"
  value {
    type: DT_INT32
  }
}
attr {
  key: "transpose_a"
  value {
    b: false
  }
}
attr {
  key: "transpose_b"
  value {
    b: false
  }
}

六、获取列表元素

如果我们对一个图中的所有内容一无所知,可以通过函数get_operations来获取图中的全部元素信息

import tensorflow as tf

a = tf.constant([[1, 2]])
b = tf.constant([[1], [3]])
t1 = tf.matmul(a, b, name='matmul')

tt = tf.get_default_graph().get_operations()
    print (tt)

输出

[<tf.Operation 'Const' type=Const>, <tf.Operation 'Const_1' type=Const>, <tf.Operation 'matmul' type=MatMul>]

七、获取对象

前面的所有操作都是通过名字来获取元素,还可以通过对象来获取元素,使用as_graph_element(obj, allow_tensor=True, allow_operation=True)函数,也就是直接传入一个对象,返回一个张量或者一个OP,该函数具有验证和转换的功能,在多线程方面偶尔会用到。

import tensorflow as tf

a = tf.constant([[1, 2]])
b = tf.constant([[1], [3]])
t1 = tf.matmul(a, b, name='matmul')

print (tf.get_default_graph().as_graph_element(a))
print (tf.get_default_graph().as_graph_element(t1.op))

输出

Tensor("Const:0", shape=(1, 2), dtype=int32)
name: "matmul"
op: "MatMul"
input: "Const"
input: "Const_1"
attr {
  key: "T"
  value {
    type: DT_INT32
  }
}
attr {
  key: "transpose_a"
  value {
    b: false
  }
}
attr {
  key: "transpose_b"
  value {
    b: false
  }
}
发布了331 篇原创文章 · 获赞 135 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/Triple_WDF/article/details/103205777