程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

TensorFlow入门——默认图与自定义图

发布于2020-03-12 10:28     阅读(1101)     评论(0)     点赞(15)     收藏(4)


  • 前言

        TensorFlow程序通常被组织成一个构建图阶段一个执行图阶段

        在构件阶段,数据(张量Tensor)与操作(节点op)的执行步骤被描述成一个图;

        在执行阶段,使用会话(Session)执行构建好的图中的操作,图必须在会话里被启动;

一、默认图

TensorFlow会默认帮我们创建一张图

查看默认图的两种方法:

  1. 调用方法 tf.get_default_graph()
  2. op、sess中都含有graph属性,默认都在一张图中,可查看属性

代码演示:

  1. import tensorflow as tf
  2. def graph_demo():
  3.     """
  4.     图的演示
  5.     """
  6.     a_t = tf.constant(2)
  7.     b_t = tf.constant(3)
  8.     c_t = a_t + b_t
  9.     
  10.     # 查看默认图
  11.     #方法1:调用方法
  12.     default_g = tf.get_default_graph()
  13.     print("default_g:\n",default_g)
  14.     #方法2:查看属性
  15.     print("a_t的图属性:\n",a_t.graph)
  16.     print("c_t的图属性:\n",c_t.graph)
  17.     #打开会话
  18.     with tf.Session() as sess:
  19.         c_t_value = sess.run(c_t)
  20.         print("c_t_value:\n",c_t_value)
  21.         print("session的图属性:\n",sess.graph)
  22. return None
  23. if __name__ ==  "__main__":
  24.     graph_demo()

输出结果:

default_g:
 <tensorflow.python.framework.ops.Graph object at 0x000001AFB4903E48>
a_t的图属性:
 <tensorflow.python.framework.ops.Graph object at 0x000001AFB4903E48>
c_t的图属性:
 <tensorflow.python.framework.ops.Graph object at 0x000001AFB4903E48>
c_t_value:
 5
session的图属性:
 <tensorflow.python.framework.ops.Graph object at 0x000001AFB4903E48>

二、自定义图(创建图)

可以用tf.Graph()自定义创建图,如,new_g = tf.Graph(),

如果想在自定义的图里面定义数据和操作:

典型用法是使用tf.Graph.as_default()构成上下文管理器,在上下文管理器中去定义数据和操作

   演示代码如下:

  1. import tensorflow as tf
  2. def graph_demo():
  3.     """
  4.     图的演示
  5.     """
  6.     # 自定义图
  7.     new_g = tf.Graph()
  8.     # 在自己的图中定义数据和操作
  9.     with new_g.as_default():
  10.         a_new = tf.constant(20)
  11.         b_new = tf.constant(30)
  12.         c_new = a_new + b_new
  13.         print("c_new:\n",c_new)
  14.         print("a_new的图属性:\n",a_new.graph)
  15.         print("c_new的图属性:\n",c_new.graph)
  16. # 开启new_g的会话
  17. with tf.Session(graph = new_g) as new_sess:
  18. c_new_value = new_sess.run(c_new)
  19. print("c_new_value:\n",c_new_value)
  20. print("new_session的图属性:\n",new_sess.graph)
  21. return None
  22. if __name__ ==  "__main__":
  23.     graph_demo()

输出结果:

c_new:
 Tensor("add:0", shape=(), dtype=int32)
a_new的图属性:
 <tensorflow.python.framework.ops.Graph object at 0x0000029370046B70>
c_new的图属性:
 <tensorflow.python.framework.ops.Graph object at 0x0000029370046B70>
c_new_value:
 50
new_session的图属性:
 <tensorflow.python.framework.ops.Graph object at 0x0000029370046B70>

可以看到默认图与自定义图输出的图属性不同

注意:

  1. 在with tf.Session()中通过参数 graph = xxx指定当前会话所运行的计算图

  2. 如果没有显式指定张量和操作所属的计算图,则这些张量和操作属于默认计算图

  3. 一个图可以在多个sess中运行,一个sess也能运行多个图

对于注意的第一条,如果不指定graph参数会出现什么结果,对此进行测试:

测试代码如下:

  1. import tensorflow as tf
  2. def graph_demo():
  3. """
  4. 图的演示
  5. """
  6. #默认图
  7. a_t = tf.constant(2)
  8. b_t = tf.constant(3)
  9. c_t = a_t + b_t
  10. # 自定义图
  11. new_g = tf.Graph()
  12. # 在自己的图中定义数据和操作
  13. with new_g.as_default():
  14. a_new = tf.constant(20)
  15. b_new = tf.constant(30)
  16. c_new = a_new + b_new
  17. #打开会话
  18. with tf.Session() as sess:
  19. c_t_value = sess.run(c_t)
  20. #试图运行自定义图中的数据、操作
  21. c_new_value = sess.run(c_new)
  22. print("c_t_value:\n",c_t_value)
  23. print("c_new_value:\n",c_new_value)
  24. return None
  25. if __name__ == "__main__":
  26. graph_demo()

总共两个图,一个默认图,一个自定义图,在不指定graph参数时,运行结果如下:

ValueError: Fetch argument <tf.Tensor 'add:0' shape=() dtype=int32> cannot be interpreted as a Tensor. (Tensor Tensor("add:0", shape=(), dtype=int32) is not an element of this graph.)

报错了,为什么呢?

在创建一个新的会话Session时,要遵循如下规则:

class Session

def __init__(self,target='',graph=None,config=None)

其中graph=None,指的是运行默认图,在开启自定义图时,要设置graph = new_g

  1. import tensorflow as tf
  2. def graph_demo():
  3. """
  4. 图的演示
  5. """
  6. #默认图
  7. a_t = tf.constant(2)
  8. b_t = tf.constant(3)
  9. c_t = a_t + b_t
  10. # 自定义图
  11. new_g = tf.Graph()
  12. # 在自己的图中定义数据和操作
  13. with new_g.as_default():
  14. a_new = tf.constant(20)
  15. b_new = tf.constant(30)
  16. c_new = a_new + b_new
  17. #打开会话
  18. with tf.Session() as sess:
  19. c_t_value = sess.run(c_t)
  20. print("c_t_value:\n",c_t_value)
  21. with tf.Session(graph=new_g) as sess:
  22. #试图运行自定义图中的数据、操作
  23. c_new_value = sess.run(c_new)
  24. print("c_new_value:\n",c_new_value)
  25. return None
  26. if __name__ == "__main__":
  27. graph_demo()

这时运行结果如下:

c_t_value:
 5
c_new_value:
 50

 

———————————————————————————————————————————————————————

小Tips:

在运行TensorFlow程序时,程序可以运行,但是会出现一句警告

I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2

警告指出你的CPU支持AVX运算加速了线性代数计算,即点积,矩阵,乘法,卷积等。

简单来说,你的CPU支持加速计算,但你没有用,然后就出现警告,哈哈哈,是不是挺有意思,但看着感觉还是不太爽

我们在安装TensorFlow时大多都是通过pip install方式安装,因此这种加速操作用不了,想要用起来,可以通过源代码的方式安装TensorFlow来编译,安装教程百度一下还是挺多的。

当然也可以选择关闭这个提示,只需在程序开头加上两行代码,让其不显示相关日志

import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

———————————————————————————————————————————————————————

 

哈哈,第一次写博客,不太习惯,欢迎各位大佬指正

 

 

原文链接:https://blog.csdn.net/DAI_KAI_KAI/article/details/104788753



所属网站分类: 技术文章 > 博客

作者:四季度炒肉

链接:https://www.pythonheidong.com/blog/article/253762/139375413f58bdb5b148/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

15 0
收藏该文
已收藏

评论内容:(最多支持255个字符)