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. Spring Cloud 微服务开发系列整理

    Spring Boot 系列精选 Spring Boot 自定义 starter Spring Boot 整合 mybatis-plus Spring Boot 整合 spring cache Spr ...

  2. .net基础系列

    这里汇总了.net基础的相关文章,方便查阅! .net基础 委托(1)认识委托 委托(2).net 1.x中的委托 委托(3).net 2.0中的委托 委托(4).net 3.5中的委托 委托(5)委 ...

  3. js高德地图手机定位

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  4. Mac Java Idea 下面Git配置简要教程

    Mac Java Idea 配置简要教程(Sunplay) 1.找到git路径 终端输入:which git 2.在idea的设置git路径 3.测试Git的效果 测试Git成功的效果

  5. springboot集成elasticsearch

    在基础阶段学习ES一般是首先是 安装ES后借助 Kibana 来进行CURD 了解ES的使用: 在进阶阶段可以需要学习ES的底层原理,如何通过Version来实现乐观锁保证ES不出问题等核心原理: 第 ...

  6. thymeleaf th:href url传递多参数

    <a th:href="@{/teacherShowMember(class_id=${class.classId},class_name=${class.className})}&q ...

  7. MyDAL - .QueryListAsync() 使用

    索引: 目录索引 一.API 列表 .QueryListAsync() .QueryListAsync<M>() 如: .QueryListAsync<AgentInventoryR ...

  8. selenium-自动化用例(十一)

    思路 将页面操作与用例case分别封装,编写case时就可以用同一个操作方法对应多个case 如下图: PageGUI:存放页面操作方法,每个页面写一个文件,每个文件中写同一个页面不同的操作,例如检索 ...

  9. GlusterFS群集存储项目

    最小化安装的centos7.5 内存大于1GB 关闭selinux,防火墙端口放行(port:24007,111)(测试建议关闭firewalld) 一.环境部署 所有软件包离线安装,原因是yum安装 ...

  10. Windows服务的安装卸载及错误查找

    @echo off echo 清理原有服务项. . . %SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil /U D:\abc\te ...