tensorflow学习笔记

tensorflow控制流之tf.case

tf.case

case(pred_fn_pairs,

         default,

         exclusive=False,

         strict=False,

          name='case')

控制语句还是非常好用的

Defined intensorflow/python/ops/control_flow_ops.py.

See the guide:Control Flow > Control Flow Operations

Create a case operation.

Thepred_fn_pairsparameter is a dict or list of pairs of size N.Each pair contains a boolean scalar tensor and a python callable thatcreates the tensors to be returned if the boolean evaluates to True.defaultis a callable generating a list of tensors. All the callablesinpred_fn_pairsas well asdefaultshould return the same numberand types of tensors.

If exclusive==True, all predicates are evaluated, and an exception isthrown if more than one of the predicates evaluates toTrue.Ifexclusive==False, execution stops are the first predicate whichevaluates to True, and the tensors generated by the corresponding functionare returned immediately. If none of the predicates evaluate to True, thisoperation returns the tensors generated bydefault.

tf.casesupports nested structures as implemented intensorflow.python.util.nest. All of the callables must return the same(possibly nested) value structure of lists, tuples, and/or named tuples.Singleton lists and tuples form the only exceptions to this: when returned bya callable, they are implicitly unpacked to single values. Thisbehavior is disabled by passingstrict=True.

If an unordered dictionary is used forpred_fn_pairs, the order of theconditional tests is not guaranteed. However, the order is guaranteed to bedeterministic, so that variables created in conditional branches are createdin fixed order across runs.

Example 1:

Pseudocode:

if(x

elsereturn23;

Expressions:

f1=lambda:tf.constant(17)

f2=lambda:tf.constant(23)

r=case([(tf.less(x,y),f1)],default=f2)

Example 2:

Pseudocode:

if(xz)raiseOpError("Only one predicate may evaluate true");

if(x

elseif(x>z)return23;

elsereturn-1;

Expressions:

deff1():returntf.constant(17)

deff2():returntf.constant(23)

deff3():returntf.constant(-1)

r=case({tf.less(x,y):f1,tf.greater(x,z):f2},       # case1, case2, case3, ...

default=f3,exclusive=True)

Args:

pred_fn_pairs: Dict or list of pairs of a boolean scalar tensor and a                callable which returns a list of tensors.

default: A callable that returns a list of tensors.

exclusive: True iff at most one predicate is allowed to evaluate toTrue.

strict: A boolean that enables/disables 'strict' mode; see above.

name: A name for this operation (optional).

Returns:

The tensors returned by the first pair whose predicate evaluated to True, or  those returned bydefaultif none does.

Raises:

TypeError: Ifpred_fn_pairsis not a list/dictionary.

TypeError: Ifpred_fn_pairsis a list but does not contain 2-tuples.

TypeError: Iffns[i]is not callable for any i, ordefaultis not   callable.


tensorflow tf.stack tf.unstack 实例

import tensorflow as tf

a = tf.constant([1,2,3])

b = tf.constant([4,5,6])

c = tf.stack([a,b],axis=1)

d = tf.unstack(c,axis=0)

e = tf.unstack(c,axis=1)

print(c.get_shape())

with tf.Session() as sess:   

     print(sess.run(c))  

    print(sess.run(d))

    print(sess.run(e))


centos6安装python3.4和pip3

在安装了epel源的情况下,直接yum就可以安装python3.4

yum install python34 -y

python3 --version

没有自带pip3,从官网安装

wget --no-check-certificate https://bootstrap.pypa.io/get-pip.py

python3 get-pip.py

pip3 -V


tensorflow GPU 安装

http://www.linuxidc.com/Linux/2016-11/137561.htm

http://blog.csdn.net/zhaoyu106/article/details/52793183/

http://blog.csdn.net/liaodong2010/article/details/71482304

Deepin15.4 下 CUDA 配置方法

deepin15.4不仅漂亮而且运行流畅,吸引了大批linuxer,其中也不乏搞cuda的小伙伴。但是有不少童鞋在deepin15.4下配置cuda遇到了困难,所以抽空写个博文说一下我配置的方法。主要针对电脑是intel

核显,nvidia显卡,需要运行cuda,并且有双显卡热切换需求的小朋友。

我的环境

先说一下我电脑的配置吧,大家的硬件环境不一样,我也没法一一测试。

CPUintel core i5 4210u

显卡nvidia gt840m

系统deepin 15.4 x64

目标

安装nvidia-bumblebee,实现双显卡切换

对于笔记本用户来说,一直开着独显的话发热量会明显增大,并且耗电也会变快,所以需要安装bumblebee来切换显卡,平时只用核显就足够了,需要运行cuda或者玩游戏的话才开启独显。

安装cuda开发工具

cuda在linux下的开发工具基本上够用了,有基于eclipse 的nsight,有visual

profiler性能分析工具,还有pycuda库实现对python运算的加速。但是我以前在deepin上面尝试安装官方的.run包,均以失败告终,很容易把电脑搞崩溃。最近终于找到了从软件源直接安装cuda的方法。

具体安装方法

安装nvidia-bumblebee

sudoapt updatesudoapt install bumblebee bumblebee-nvidia nvidia-smi

一行命令搞定nvidia驱动、bumblebee切换程序、和显卡状态监控程序。

不用管nouveau驱动,系统会自己屏蔽掉。

然后重启

sudoreboot

重启之后测试

nvidia-smi

optirun nvidia-smi

如果出现如下界面,说明驱动安装成功


安装cuda开发工具

首先安装配置g++,gcc

因为cuda版本原因,cuda8之前都只支持g++-4.8,gcc-4.8

所以gcc需要降级

sudoapt install g++-4.8gcc-4.8

然后更改软连接

cd/usr/binsudorm gcc g++sudoln-sg++-4.8g++sudoln-sgcc-4.8gcc

然后下载开发工具

sudo apt install nvidia-cuda-devnvidia-cuda-toolkitnvidia-nsightnvidia-visual-profiler

使用nsight的方法为:在终端下输入

optirun nsight


tf.while_loop()

tf.while_loop(cond, body, loop_vars, shape_invariants=None,

parallel_iterations=10, back_prop=True, swap_memory=False, name=None)

while_loop可以这么理解

loop_vars = [...]whilecond(*loop_vars):    loop_vars = body(*loop_vars)

1

2

3

示例:

importtensorflowastfa = tf.get_variable("a", dtype=tf.int32, shape=[], initializer=tf.ones_initializer())b = tf.constant(2)f = tf.constant(6)# Definition of condition and bodydefcond(a, b, f):returna <3defbody(a, b, f):# do some stuff with a, ba = a +1returna, b, f# Loop, 返回的tensor while 循环后的 a,b,fa, b, f = tf.while_loop(cond, body, [a, b, f])withtf.Session()assess:    tf.global_variables_initializer().run()    res = sess.run([a, b, f])    print(res)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,670评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,928评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,926评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,238评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,112评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,138评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,545评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,232评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,496评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,596评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,369评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,226评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,600评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,906评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,185评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,516评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,721评论 2 335