【Tensorflow】tf.Graph()函数

TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学习系统,其命名来源于本身的运行原理。Tensor(张量)意味着N维数组,Flow(流)意味着基于数据流图的计算,TensorFlow为张量从流图的一端流动到另一端计算过程。TensorFlow是将复杂的数据结构传输至人工智能神经网中进行分析和处理过程的系统。

TensorFlow可被用于语音识别图像识别等多项机器深度学习领域,对2011年开发的深度学习基础架构DistBelief进行了各方面的改进,它可在小到一部智能手机、大到数千台数据中心服务器的各种设备上运行。TensorFlow将完全开源,任何人都可以用。

原生接口文章

  1. 【Tensorflow】tf.placeholder函数
  2. 【TensorFlow】tf.nn.conv2d是怎样实现卷积的
  3. 【TensorFlow】tf.nn.max_pool实现池化操作
  4. 【Tensorflow】tf.nn.relu函数
  5. 【Tensorflow】tf.reshape 函数
  6. 【Tensorflow】tf.nn.dropout函数
  7. 【Tensorflow】tf.argmax函数
  8. 【Tensorflow】tf.cast 类型转换 函数
  9. 【Tensorflow】tf.train.AdamOptimizer函数
  10. 【Tensorflow】tf.Graph()函数
  11. 【TensorFlow】tf.nn.softmax_cross_entropy_with_logits的用法
  12. 【Tensorflow】tf.dynamic_partition 函数 分拆数组
     原生接口实例

  1. 【Tensorflow】实现简单的卷积神经网络CNN实际代码
  2. 【Tensorflow 实战】实现欧式距离
         slim接口文章

  1. 【Tensorflow】tensorflow.contrib.slim 包
  2. 【Tensorflow slim】 slim.arg_scope的用法
  3. 【Tensorflow slim】slim.data包
  4. 【Tensorflow slim】slim evaluation 函数
  5. 【Tensorflow slim】slim layers包
  6. 【Tensorflow slim】slim learning包
  7. 【Tensorflow slim】slim losses包
  8. 【Tensorflow slim】slim nets包
  9. 【Tensorflow slim】slim variables包
  10. 【Tensorflow slim】slim metrics包
        slim 实例

  1. 【Tensorflow slim 实战】写MobileNet
  2. 【Tensorflow slim 实战】写Inception-V4 Inception-ResNet-v2结构
         kera 接口文章

  1. 【Tensorflow keras】Keras:基于Theano和TensorFlow的深度学习库
  2. 【Tensorflow keras】轻量级深度学习框架 Keras简介
         tensorflow使用过程中的辅助接口或通过tensorflow实现的批量操作接口

  1. 将非RGB图片转换为RGB图片
  2. 【opencv】python3 将图片生成视频文件
  3. 【opencv】selective_search函数

=========================================================================



tf.Graph() 函数非常重要,注意提现在两个方面

1. 它可以通过tensorboard用图形化界面展示出来流程结构

2. 它可以整合一段代码为一个整体存在于一个图中


声明情况大体有三种

1. tensor:通过张量本身直接出graph

[python]  view plain  copy
  1. # -*- coding: utf-8 -*-    
  2. import tensorflow as tf  
  3.   
  4. c = tf.constant(4.0)  
  5.   
  6. sess = tf.Session()  
  7. sess.run(tf.global_variables_initializer())  
  8. c_out = sess.run(c)  
  9. print(c_out)  
  10. print(c.graph == tf.get_default_graph())  
  11. print(c.graph)  
  12. print(tf.get_default_graph())  
输出

[python]  view plain  copy
  1. 4.0  
  2. True  
  3. <tensorflow.python.framework.ops.Graph object at 0x7f382f9ef110>  
  4. <tensorflow.python.framework.ops.Graph object at 0x7f382f9ef110>  

2.通过声明一个默认的,然后定义张量内容,在后面可以调用或保存

[python]  view plain  copy
  1. # -*- coding: utf-8 -*-    
  2. import tensorflow as tf  
  3.   
  4. g = tf.Graph()  
  5. with g.as_default():  
  6.     c = tf.constant(4.0)  
  7.   
  8. sess = tf.Session(graph=g)  
  9. c_out = sess.run(c)  
  10. print(c_out)  
  11. print(g)  
  12. print(tf.get_default_graph())  

输出

[python]  view plain  copy
  1. 4.0  
  2. <tensorflow.python.framework.ops.Graph object at 0x7f65f1cb2fd0>  
  3. <tensorflow.python.framework.ops.Graph object at 0x7f65de447c90>  

3.通过多个声明,在后面通过变量名来分别调用

[python]  view plain  copy
  1. # -*- coding: utf-8 -*-    
  2. import tensorflow as tf  
  3.   
  4. g1 = tf.Graph()  
  5. with g1.as_default():  
  6.     c1 = tf.constant(4.0)  
  7.   
  8. g2 = tf.Graph()  
  9. with g2.as_default():  
  10.     c2 = tf.constant(20.0)  
  11.   
  12. with tf.Session(graph=g1) as sess1:  
  13.     print(sess1.run(c1))  
  14. with tf.Session(graph=g2) as sess2:  
  15.     print(sess2.run(c2))  

输出

[python]  view plain  copy
  1. 4.0  
  2. 20.0  

对graph的操作大体有三种

1.保存

[python]  view plain  copy
  1. # -*- coding: utf-8 -*-    
  2. import tensorflow as tf  
  3.   
  4. g1 = tf.Graph()  
  5. with g1.as_default():  
  6.     # 需要加上名称,在读取pb文件的时候,是通过name和下标来取得对应的tensor的  
  7.     c1 = tf.constant(4.0, name='c1')  
  8.   
  9. g2 = tf.Graph()  
  10. with g2.as_default():  
  11.     c2 = tf.constant(20.0)  
  12.   
  13. with tf.Session(graph=g1) as sess1:  
  14.     print(sess1.run(c1))  
  15. with tf.Session(graph=g2) as sess2:  
  16.     print(sess2.run(c2))  
  17.   
  18. # g1的图定义,包含pb的path, pb文件名,是否是文本默认False  
  19. tf.train.write_graph(g1.as_graph_def(),'.','graph.pb',False)  

输出

[python]  view plain  copy
  1. 4.0  
  2. 20.0  
并且在当前文件夹下面生成graph.pb文件

2.从pb文件中调用

[python]  view plain  copy
  1. # -*- coding: utf-8 -*-    
  2. import tensorflow as tf  
  3. from tensorflow.python.platform import gfile  
  4.   
  5. #load graph  
  6. with gfile.FastGFile("./graph.pb",'rb') as f:  
  7.     graph_def = tf.GraphDef()  
  8.     graph_def.ParseFromString(f.read())  
  9.     tf.import_graph_def(graph_def, name='')  
  10.   
  11. sess = tf.Session()  
  12. c1_tensor = sess.graph.get_tensor_by_name("c1:0")  
  13. c1 = sess.run(c1_tensor)  
  14. print(c1)  

输出

[python]  view plain  copy
  1. 4.0  

3.穿插调用

[python]  view plain  copy
  1. # -*- coding: utf-8 -*-    
  2. import tensorflow as tf  
  3.   
  4. g1 = tf.Graph()  
  5. with g1.as_default():  
  6.     # 声明的变量有名称是一个好的习惯,方便以后使用  
  7.     c1 = tf.constant(4.0, name="c1")  
  8.   
  9. g2 = tf.Graph()  
  10. with g2.as_default():  
  11.     c2 = tf.constant(20.0, name="c2")  
  12.   
  13. with tf.Session(graph=g2) as sess1:  
  14.     # 通过名称和下标来得到相应的值  
  15.     c1_list = tf.import_graph_def(g1.as_graph_def(), return_elements = ["c1:0"], name = '')  
  16.     print(sess1.run(c1_list[0]+c2))  

输出

[python]  view plain  copy
  1. 24.0  

猜你喜欢

转载自blog.csdn.net/qq_30868235/article/details/80502077
今日推荐