tensorflow學(xué)習(xí)筆記

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 實(shí)例

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,從官網(wǎng)安裝

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不僅漂亮而且運(yùn)行流暢玫坛,吸引了大批linuxer肆氓,其中也不乏搞cuda的小伙伴优炬。但是有不少童鞋在deepin15.4下配置cuda遇到了困難官扣,所以抽空寫個(gè)博文說一下我配置的方法祝蝠。主要針對電腦是intel

核顯延塑,nvidia顯卡绣张,需要運(yùn)行cuda,并且有雙顯卡熱切換需求的小朋友。

我的環(huán)境

先說一下我電腦的配置吧页畦,大家的硬件環(huán)境不一樣胖替,我也沒法一一測試。

CPUintel core i5 4210u

顯卡nvidia gt840m

系統(tǒng)deepin 15.4 x64

目標(biāo)

安裝nvidia-bumblebee,實(shí)現(xiàn)雙顯卡切換

對于筆記本用戶來說独令,一直開著獨(dú)顯的話發(fā)熱量會明顯增大端朵,并且耗電也會變快,所以需要安裝bumblebee來切換顯卡燃箭,平時(shí)只用核顯就足夠了冲呢,需要運(yùn)行cuda或者玩游戲的話才開啟獨(dú)顯。

安裝cuda開發(fā)工具

cuda在linux下的開發(fā)工具基本上夠用了招狸,有基于eclipse 的nsight敬拓,有visual

profiler性能分析工具,還有pycuda庫實(shí)現(xiàn)對python運(yùn)算的加速裙戏。但是我以前在deepin上面嘗試安裝官方的.run包乘凸,均以失敗告終,很容易把電腦搞崩潰累榜。最近終于找到了從軟件源直接安裝cuda的方法营勤。

具體安裝方法

安裝nvidia-bumblebee

sudoapt updatesudoapt install bumblebee bumblebee-nvidia nvidia-smi

一行命令搞定nvidia驅(qū)動(dòng)、bumblebee切換程序壹罚、和顯卡狀態(tài)監(jiān)控程序葛作。

不用管nouveau驅(qū)動(dòng),系統(tǒng)會自己屏蔽掉猖凛。

然后重啟

sudoreboot

重啟之后測試

nvidia-smi

optirun nvidia-smi

如果出現(xiàn)如下界面赂蠢,說明驅(qū)動(dòng)安裝成功


安裝cuda開發(fā)工具

首先安裝配置g++,gcc

因?yàn)閏uda版本原因,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

然后下載開發(fā)工具

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 循環(huán)后的 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)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市漠吻,隨后出現(xiàn)的幾起案子量瓜,更是在濱河造成了極大的恐慌,老刑警劉巖途乃,帶你破解...
    沈念sama閱讀 222,000評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件绍傲,死亡現(xiàn)場離奇詭異,居然都是意外死亡耍共,警方通過查閱死者的電腦和手機(jī)烫饼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,745評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來试读,“玉大人杠纵,你說我怎么就攤上這事」澈В” “怎么了比藻?”我有些...
    開封第一講書人閱讀 168,561評論 0 360
  • 文/不壞的土叔 我叫張陵铝量,是天一觀的道長。 經(jīng)常有香客問我银亲,道長慢叨,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,782評論 1 298
  • 正文 為了忘掉前任务蝠,我火速辦了婚禮拍谐,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘馏段。我一直安慰自己轩拨,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,798評論 6 397
  • 文/花漫 我一把揭開白布院喜。 她就那樣靜靜地躺著亡蓉,像睡著了一般。 火紅的嫁衣襯著肌膚如雪够坐。 梳的紋絲不亂的頭發(fā)上寸宵,一...
    開封第一講書人閱讀 52,394評論 1 310
  • 那天,我揣著相機(jī)與錄音元咙,去河邊找鬼。 笑死巫员,一個(gè)胖子當(dāng)著我的面吹牛庶香,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播简识,決...
    沈念sama閱讀 40,952評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼赶掖,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了七扰?” 一聲冷哼從身側(cè)響起奢赂,我...
    開封第一講書人閱讀 39,852評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎颈走,沒想到半個(gè)月后膳灶,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,409評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡立由,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,483評論 3 341
  • 正文 我和宋清朗相戀三年轧钓,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片锐膜。...
    茶點(diǎn)故事閱讀 40,615評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡毕箍,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出道盏,到底是詐尸還是另有隱情而柑,我是刑警寧澤文捶,帶...
    沈念sama閱讀 36,303評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站媒咳,受9級特大地震影響拄轻,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜伟葫,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,979評論 3 334
  • 文/蒙蒙 一恨搓、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧筏养,春花似錦斧抱、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,470評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至茎辐,卻和暖如春宪郊,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背拖陆。 一陣腳步聲響...
    開封第一講書人閱讀 33,571評論 1 272
  • 我被黑心中介騙來泰國打工弛槐, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人依啰。 一個(gè)月前我還...
    沈念sama閱讀 49,041評論 3 377
  • 正文 我出身青樓乎串,卻偏偏與公主長得像,于是被迫代替她去往敵國和親速警。 傳聞我的和親對象是個(gè)殘疾皇子叹誉,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,630評論 2 359