1. tf.Graph().as_default() 的作用


首先看官网上的解释:

再看博主 Joanna-In-Hdu&Hust 对此比较通俗易懂的解释(https://www.cnblogs.com/studylyn/p/9105818.html

tf.Graph() 表示实例化一个类,用于 TensorFlow  计算和表示用的数据流图,通俗讲就是在代码中添加的操作(画中的结点)和数据(画中的线条)都是画在纸上的“画”,而图就是呈现这些画的纸,你可以利用很多线程生成多张图,但默认图只有一张。

tf.Graph().as_default()  表示将这个类实例,也就是新生成的图作为整个 tensorflow 运行环境的默认图,如果只有一个主线程不写也没有关系, TensorFlow 里面已经存好了一张默认图,可以使用 tf.get_default_graph() 来调用(显示这张默认纸),当你有多个线程就可以创造多个 tf.Graph() ,就是你可以有一个画图本,有很多张图纸,这时候就会有一个默认图的概念。

当只有一个线程时写不写都不重要,但写上确实一个良好的习惯,也是代码比较规范。正如下面这段话所解释的那样。

Since a default graph is always registered, every op and variable is placed into the default graph. The statement, however, creates a new graph and places everything (declared inside its scope) into this graph. If the graph is the only graph, it's useless. But it's a good practice because if you start to work with many graphs it's easier to understand where ops and vars are placed. Since this statement costs you nothing, it's better to write it anyway.

Just to be sure that if you refactor the code in the future, the operations defined belong to the graph you choose initially.

2.  tf.device() 的使用


在 TensorFlow 中,模型可以在本地 CPU 和 GPU 中运行,用户可以使用 tf.device() 指定模型运行的设备。

一般情况下,如果安装的 TensorFlow 是 GPU 版本,而且电脑配置中有符合条件的显卡,默认模型在显卡中运行,并会有相应提示。

如果要切换成 CPU 运算,可调用 tf.device(device_name) 函数,其中 device_name 格式如 /cpu:0 其中的0表示设别号,TF不区分 CPU 设备,通常设置为0即可;但是会区分 GPU 设备, /gpu:0 和 /gpu:1 表示两张不同的显卡。

有时候,我们即使在 GPU 下跑模型,也会将部分 Tesnor 储存在内存里,因为可能 Tensor 太大了,显存放不下,放到更大的内存中来,这是常常通过人为指定 CPU 设备。尽管这种方法会减少显存的负担,但从内存把数据传输到显存是很慢的,需要考虑速度的问题。

with tf.device('/cpu:0'):
build_CNN() # 这时CNN的Tensor储存在内存中,而非显存里

3.  sys.path.append('../') 


当我们 import module 时,默认情况下 python 解析器会搜索当前目录、已安装的内置模块和第三方模块,搜索路径存放在 sys 模块的 path 参数中, sys.path 返回的是一个 list

当我们要添加自己的搜索目录时,可以通过列表的 append() 方法 sys.path.append('../') 添加到列表末尾或者 insert() 方法 sys.path.insert(position, '../') 添加到指定位置

 >>> import sys
>>> sys.path
['', '/usr/local/lib/python2.7/dist-packages/apex-0.1-py2.7-linux-x86_64.egg', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
>>> sys.path.append('../')
>>> sys.path
['', '/usr/local/lib/python2.7/dist-packages/apex-0.1-py2.7-linux-x86_64.egg', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '../']

这种方法是在程序运行时对搜索路径进行临时修改,脚本运行结束后失效。

如果想要改路径一直有效,需要把路径添加到系统环境变量中。有以下几种方法:

1. 将写好的py文件放到已经添加到环境变量的目录下

2. 使用环境变量PYTHONPATH修改默认搜索路径 export PYTHONPATH=$PYTHONPATH:/home/liu/shell/config

3. /usr/lib/python2.7/site-packages下新建一个.pth 文件(以 pth 作为后缀) ,将需要导入的模块的路径分行书写,如:

 /home/cv/mvsnet/tools
/home/cv/mvsnet/cnn_warpper

4.  tf.app.flags.DEFINE_string() 


tf.app.flags.DEFINE_xxx() 的作用就是添加命令行的X tf.app.flags.DEFINE_xxx() ,而 tf.app.flags.FLAGS 可以从对应的命令行参数中取出参数。

 tf.app.flags.DEFINE_string('model_dir', './checkpoint', """Path to save the model.""")
tf.app.flags.DEFINE_boolean('use_pretrain', False, """Whether to train.""")
tf.app.flags.DEFINE_integer('ckpt_step', 0, """ckpt step.""")

上述代码的功能是创建几个命令行参数,其中第三个参数是对无输入时相应参数的提示信息。

当运行 tf.app.run() 时,可以利用 tf.app.flags 非常方便地将这些变量在线程之间传输

5.  GPU run out of memory 


-- ::59.515839: I tensorflow/core/kernels/cuda_solvers.cc:] Creating CudaSolver handles for stream 0x5a7a07e0

-- ::00.676787: W tensorflow/core/common_runtime/bfc_allocator.cc:] Allocator (GPU_0_bfc) ran out of memory trying to allocate .62MiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory is available.

-- ::00.682241: W tensorflow/core/common_runtime/bfc_allocator.cc:] Allocator (GPU_0_bfc) ran out of memory trying to allocate .50MiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory is available.

未完待续……

【学习笔记】TensorFlow的更多相关文章

  1. TensorFlow 深度学习笔记 TensorFlow实现与优化深度神经网络

    转载请注明作者:梦里风林 Github工程地址:https://github.com/ahangchen/GDLnotes 欢迎star,有问题可以到Issue区讨论 官方教程地址 视频/字幕下载 全 ...

  2. tensorflow学习笔记----tensorflow在windows的安装及TensorBoard中mnist样例

    前言:                                                                                                 ...

  3. [学习笔记] TensorFlow 入门之基本使用

    整体介绍 使用 TensorFlow, 你必须明白 TensorFlow: 使用图 (graph) 来表示计算任务. 在被称之为 会话 (Session) 的上下文 (context) 中执行图. 使 ...

  4. Tensorflow学习笔记2:About Session, Graph, Operation and Tensor

    简介 上一篇笔记:Tensorflow学习笔记1:Get Started 我们谈到Tensorflow是基于图(Graph)的计算系统.而图的节点则是由操作(Operation)来构成的,而图的各个节 ...

  5. Google TensorFlow深度学习笔记

    Google Deep Learning Notes Google 深度学习笔记 由于谷歌机器学习教程更新太慢,所以一边学习Deep Learning教程,经常总结是个好习惯,笔记目录奉上. Gith ...

  6. Tensorflow学习笔记2019.01.22

    tensorflow学习笔记2 edit by Strangewx 2019.01.04 4.1 机器学习基础 4.1.1 一般结构: 初始化模型参数:通常随机赋值,简单模型赋值0 训练数据:一般打乱 ...

  7. Tensorflow学习笔记2019.01.03

    tensorflow学习笔记: 3.2 Tensorflow中定义数据流图 张量知识矩阵的一个超集. 超集:如果一个集合S2中的每一个元素都在集合S1中,且集合S1中可能包含S2中没有的元素,则集合S ...

  8. 【转载】TensorFlow学习笔记:共享变量

    原文链接:http://jermmy.xyz/2017/08/25/2017-8-25-learn-tensorflow-shared-variables/ 本文是根据 TensorFlow 官方教程 ...

  9. TensorFlow学习笔记之--[compute_gradients和apply_gradients原理浅析]

    I optimizer.minimize(loss, var_list) 我们都知道,TensorFlow为我们提供了丰富的优化函数,例如GradientDescentOptimizer.这个方法会自 ...

  10. TensorFlow学习笔记:共享变量

    本文是根据 TensorFlow 官方教程翻译总结的学习笔记,主要介绍了在 TensorFlow 中如何共享参数变量. 教程中首先引入共享变量的应用场景,紧接着用一个例子介绍如何实现共享变量(主要涉及 ...

随机推荐

  1. while死循环导致的内存溢出

    场景:新开发的功能内测,新调用了其它模块的接口,一如既往的点鼠标,计费,但是许久都没有响应页面遮罩一直锁着,最后抛出了以下异常 咋一看这个异常信息,不就是锁表了吗?把锁表进程Kill掉,再来一遍,结果 ...

  2. Mybatis入门之动态sql

    Mybatis入门之动态sql 通过mybatis提供的各种标签方法实现动态拼接sql. 1.if.where.sql.include标签(条件.sql片段) <sql id="sel ...

  3. Jmeter简单回顾

    之前公众号推文一上手就分享如何测接口, 其实忽略了一些概念性的东西, 今天来给大家拾遗补缺, 做个回顾吧. 一. JMeter介绍 jmeter能做什么,来自官网的解释: Ability to loa ...

  4. redis -hash(哈希.对象)

    hash 用于储存对象,对象的结构为属性.值 值的类型string 增加.修改: 设置单个属性: hset 键 field 值 例如: 设置键 user 的属性name 为 python hset u ...

  5. React---入门(1)

    React是什么? React 是一个用于构建用户界面的 JAVASCRIPT 库. React 特点 1.声明式设计 −React采用声明范式,可以轻松描述应用. 2.高效 −React通过对DOM ...

  6. RuntimeException和Exception区别

    1.java将所有的错误封装为一个对象,其根本父类为Throwable, Throwable有两个子类:Error和Exception. 2.Error是Throwable 的子类,用于指示合理的应用 ...

  7. Windows下Oracle 11g的安装

    Windows下Oracle 11g的安装 Windows下Oracle 11g的安装: Windows:64位, Oracle 11g版本:win64_11gR2_database_1of2(安装包 ...

  8. 使用git提交代码到github,每次都要输入用户名和密码的解决方法

    自从使用git提交代码到github后,发现自己使用git的功力增长了不少,但也遇到不少问题.比如,使用git提交代码到github的时候,经常要求输入用户名和密码,类似这种: 网上有这么一种解决方法 ...

  9. 四。Hibernate 使用MAVEN工具

    maven工具的使用1.作用:打包项目以及jar包的版本管理2.使用步骤: a.下载maven工具,修改conf目录下的setting.xml文件 <mirror> <id>a ...

  10. commons-lang3之StringUtils

    字符串是一种在开发中经常使用到的数据类型,对字符串的处理也变得非常重要,字符串本身有一些方法,但都没有对null做处理,而且有时可能还需要做一些额外处理才能满足我们的需求,比如,要判断某个字符串中是否 ...