tensorflow之图的一些基本操作

 1 import numpy as np
 2 import tensorflow as tf
 3 #图的操作
 4 
 5 #####建立图#####
 6 c = tf.constant(0.0)
 7 g = tf.Graph()
 8 with g.as_default():
 9     c1 = tf.constant(0.0)
10     print(c1.graph)
11     print(g)
12     print(c.graph)
13 
14 with tf.Graph().as_default():
15     g2 = tf.get_default_graph()#获得图
16     print(g2)#得到的是一个新图
17 tf.reset_default_graph()#重置图
18 g3 = tf.get_default_graph()
19 print(g3)
20 
21 ######获取张量#####
22 print(c1.name)
23 t = g.get_tensor_by_name(name = "Const:0")
24 print(t)
25 
26 #####获取操作符#####
27 a = tf.constant([[1.0, 2.0]])
28 b = tf.constant([[1.0], [3.0]])
29 
30 tensor1 = tf.matmul(a, b, name = 'exampleop')
31 print(tensor1.name, tensor1)
32 test = g3.get_tensor_by_name('exampleop:0')
33 print(test)
34 
35 print(tensor1.op.name)
36 testop = g3.get_operation_by_name("exampleop")
37 print(testop)
38 
39 with tf.Session() as sess:
40     test = sess.run(test)
41     print(test)
42     test = tf.get_default_graph().get_tensor_by_name("exampleop:0")
43     print(test)
44 ###tensor1是张量,op是描述张量中的运算关系,是通过访问张量的属性可以找到的
45 
46 #####获取对象#####多用于多线程方面
47 tt3 = g.as_graph_element(c1)
48 print(tt3)

猜你喜欢

转载自www.cnblogs.com/bigstrawberrywakaka/p/9343485.html
今日推荐