【学习笔记】TensorFlow
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的更多相关文章
- TensorFlow 深度学习笔记 TensorFlow实现与优化深度神经网络
转载请注明作者:梦里风林 Github工程地址:https://github.com/ahangchen/GDLnotes 欢迎star,有问题可以到Issue区讨论 官方教程地址 视频/字幕下载 全 ...
- tensorflow学习笔记----tensorflow在windows的安装及TensorBoard中mnist样例
前言: ...
- [学习笔记] TensorFlow 入门之基本使用
整体介绍 使用 TensorFlow, 你必须明白 TensorFlow: 使用图 (graph) 来表示计算任务. 在被称之为 会话 (Session) 的上下文 (context) 中执行图. 使 ...
- Tensorflow学习笔记2:About Session, Graph, Operation and Tensor
简介 上一篇笔记:Tensorflow学习笔记1:Get Started 我们谈到Tensorflow是基于图(Graph)的计算系统.而图的节点则是由操作(Operation)来构成的,而图的各个节 ...
- Google TensorFlow深度学习笔记
Google Deep Learning Notes Google 深度学习笔记 由于谷歌机器学习教程更新太慢,所以一边学习Deep Learning教程,经常总结是个好习惯,笔记目录奉上. Gith ...
- Tensorflow学习笔记2019.01.22
tensorflow学习笔记2 edit by Strangewx 2019.01.04 4.1 机器学习基础 4.1.1 一般结构: 初始化模型参数:通常随机赋值,简单模型赋值0 训练数据:一般打乱 ...
- Tensorflow学习笔记2019.01.03
tensorflow学习笔记: 3.2 Tensorflow中定义数据流图 张量知识矩阵的一个超集. 超集:如果一个集合S2中的每一个元素都在集合S1中,且集合S1中可能包含S2中没有的元素,则集合S ...
- 【转载】TensorFlow学习笔记:共享变量
原文链接:http://jermmy.xyz/2017/08/25/2017-8-25-learn-tensorflow-shared-variables/ 本文是根据 TensorFlow 官方教程 ...
- TensorFlow学习笔记之--[compute_gradients和apply_gradients原理浅析]
I optimizer.minimize(loss, var_list) 我们都知道,TensorFlow为我们提供了丰富的优化函数,例如GradientDescentOptimizer.这个方法会自 ...
- TensorFlow学习笔记:共享变量
本文是根据 TensorFlow 官方教程翻译总结的学习笔记,主要介绍了在 TensorFlow 中如何共享参数变量. 教程中首先引入共享变量的应用场景,紧接着用一个例子介绍如何实现共享变量(主要涉及 ...
随机推荐
- Mysql使用event,类似oracle job
MySQL从5.1开始支持event功能,类似oracle的job功能.有了这个功能之后我们就可以让MySQL自动的执行数据汇总等功能,不用像以前需要操作的支持了.如linux crontab功能. ...
- 小程序开发基础-view视图容器
view 视图容器. // wxml <view class="section"> <view class="section__title"& ...
- C#零基础入门-2-Visual Studio (VS)程序初始化及各组成部分
X:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe 可以使用桌面快捷方式启动,也可以从开始菜单启动,还 ...
- vue框架构建项目流程
构建项目流程: 1.全局查询:node -v 2.全局初始化:npm install --global vue-cli 3.模块化工程:vue init webpack myapp--->y,n ...
- WPF软件开发系统之四——医疗病人信息管理系统
仿360悬浮窗的方式,始终有个工具栏浮在桌面的最顶层,方便任何时候操作. 主要功能包括:病人信息的添加.修改.查询.包括别人基本信息.诊断结果.接待医生.手术多张图片等. 系统特点:简洁.易操作.美观 ...
- MySQL(InnoDB)是如何处理死锁的
MySQL(InnoDB)是如何处理死锁的 一.什么是死锁 官方定义如下:两个事务都持有对方需要的锁,并且在等待对方释放,并且双方都不会释放自己的锁. 这个就好比你有一个人质,对方有一个人质,你们俩去 ...
- 力扣算法题—093复原IP地址
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255.11.135", ...
- #032 有空就看PTA
我咋买书了? 上学期
- IDEA Can't Update No tracked branch configured for branch master or the branch doesn't exist.
IDEA Can't Update No tracked branch configured for branch master or the branch doesn't exist.To make ...
- url全部信息打印
public String findAllContract(HttpServletRequest request,String a){ String string = new StringBuilde ...