最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于tensorflow for循環(huán) while循環(huán)案例

 更新時(shí)間:2020年06月30日 10:53:54   作者:guotong1988  
這篇文章主要介紹了基于tensorflow for循環(huán) while循環(huán)案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

我就廢話不多說了,大家還是直接看代碼吧~

import tensorflow as tf

n1 = tf.constant(2)
n2 = tf.constant(3)
n3 = tf.constant(4)

def cond1(i, a, b):
 return i < n1

def cond2(i, a, b):
 return i < n2

def cond3(i, a, b):
 return i < n3

def body(i, a, b):
 return i + 1, b, a + b

i1, a1, b1 = tf.while_loop(cond1, body, (2, 1, 1))
i2, a2, b2 = tf.while_loop(cond2, body, (2, 1, 1))
i3, a3, b3 = tf.while_loop(cond3, body, (2, 1, 1))
sess = tf.Session()

print(sess.run(i1))
print(sess.run(a1))
print(sess.run(b1))
print("-")
print(sess.run(i2))
print(sess.run(a2))
print(sess.run(b2))
print("-")
print(sess.run(i3))
print(sess.run(a3))
print(sess.run(b3))

print結(jié)果:

2
1
1
-
3
1
2
-
4
2
3

可見body函數(shù)返回的三個(gè)變量又傳給了body

補(bǔ)充知識(shí):tensorflow在tf.while_loop循環(huán)(非一般循環(huán))中使用操縱變量該怎么做

代碼(操縱全局變量)

xiaojie=1
i=tf.constant(0,dtype=tf.int32)
batch_len=tf.constant(10,dtype=tf.int32)
loop_cond = lambda a,b: tf.less(a,batch_len)
#yy=tf.Print(batch_len,[batch_len],"batch_len:")
yy=tf.constant(0)
loop_vars=[i,yy]
def _recurrence(i,yy):
 c=tf.constant(2,dtype=tf.int32)
 x=tf.multiply(i,c)
 global xiaojie
 xiaojie=xiaojie+1
 print_info=tf.Print(x,[x],"x:")
 yy=yy+print_info
 i=tf.add(i,1)
# print (xiaojie)
 return i,yy
i,yy=tf.while_loop(loop_cond,_recurrence,loop_vars,parallel_iterations=1)#可以批處理
sess = tf.Session()
print (sess.run(i))
print (xiaojie)

輸出的是10和2。

也就是xiaojie只被修改了一次。

這個(gè)時(shí)候,在_recurrence循環(huán)體中添加語句

print (xiaojie)

會(huì)輸出2。而且只輸出一次。具體為什么,最后總結(jié)的時(shí)候再解釋。

代碼(操縱類成員變量)class RNN_Model():

def __init__(self):
  self.xiaojie=1
 def test_RNN(self):
  i=tf.constant(0,dtype=tf.int32)
  batch_len=tf.constant(10,dtype=tf.int32)
  loop_cond = lambda a,b: tf.less(a,batch_len)
  #yy=tf.Print(batch_len,[batch_len],"batch_len:")
  yy=tf.constant(0)
  loop_vars=[i,yy]
  def _recurrence(i,yy):
   c=tf.constant(2,dtype=tf.int32)
   x=tf.multiply(i,c)
   self.xiaojie=self.xiaojie+1
   print_info=tf.Print(x,[x],"x:")
   yy=yy+print_info
   i=tf.add(i,1)        print ("_recurrence:",self.xiaojie)
   return i,yy
  i,yy=tf.while_loop(loop_cond,_recurrence,loop_vars,parallel_iterations=1)#可以批處理
  sess = tf.Session()
  sess.run(yy)
  print (self.xiaojie)
if __name__ == "__main__":
 model = RNN_Model()#構(gòu)建樹,并且構(gòu)建詞典
 model.test_RNN()

輸出是:

_recurrence: 2
10
2

tf.while_loop操縱全局變量和類成員變量總結(jié)

為什么_recurrence中定義的print操作只執(zhí)行一次呢,這是因?yàn)開recurrence中的print相當(dāng)于一種對(duì)代碼的定義,直接在定義的過程中就執(zhí)行了。所以,可以看到輸出是在sess.run之前的。但是,定義的其它操作就是數(shù)據(jù)流圖中的操作,需要在sess.run中執(zhí)行。

就必須在sess.run中執(zhí)行。但是,全局變量xiaojie也好,還是類成員變量xiaojie也好。其都不是圖中的內(nèi)容。因此,tf.while_loop執(zhí)行的是tensorflow計(jì)算圖中的循環(huán),對(duì)于不是在計(jì)算圖中的,就不會(huì)參與循環(huán)。注意:而且必須是與loop_vars中指定的變量存在數(shù)據(jù)依賴關(guān)系的tensor才可以!此外,即使是依賴關(guān)系,也必須是_recurrence循環(huán)體中return出的變量,才會(huì)真正的變化。比如,見下面的self.L??傊?,想操縱變量,就要傳入loop_vars!

如果對(duì)一個(gè)變量沒有修改,就可以直接在循環(huán)中以操縱類成員變量或者全局變量的方式只讀。

self.L與loop_vars中變量有依賴關(guān)系,但是并沒有真正被修改。

#IIII通過計(jì)算將非葉子節(jié)點(diǎn)的詞向量也放入nodes_tensor中。
   iiii=tf.constant(0,dtype=tf.int32)
   loop____cond = lambda a,b,c,d,e: tf.less(a,self.sentence_length-1)#iiii的范圍是0到sl-2。注意,不包括sl-1。這是因?yàn)橹恍枰?jì)算sentence_length-1次,就能構(gòu)建出一顆樹
   loop____vars=[iiii,columnLinesOfL,node_tensors_cost_tensor,nodes_tensor,tfPrint]
   def ____recurrence(iiii,columnLinesOfL,node_tensors_cost_tensor,nodes_tensor,tfPrint):#循環(huán)的目的是實(shí)現(xiàn)Greedy算法
    ###
    #Greedy的主要目標(biāo)就是確立樹結(jié)構(gòu)。    
    ###  
    c1 = self.L[:,0:columnLinesOfL-1]#這段代碼是從RvNN的matlab的源碼中復(fù)制過來的,但是Matlab的下標(biāo)是從1開始,并且Matlab中1:2就是1和2,而python中1:2表示的是1,不包括2,所以,有很大的不同。
    c2 = self.L[:,1:columnLinesOfL]
    c=tf.concat([c1,c2],axis=0)
    p=tf.tanh(tf.matmul(self.W1,c)+tf.tile(self.b1,[1,columnLinesOfL-1]))
    p_normalization=self.normalization(p)
    y=tf.tanh(tf.matmul(self.U,p_normalization)+tf.tile(self.bs,[1,columnLinesOfL-1]))#根據(jù)Matlab中的源碼來的,即重構(gòu)后,也有一個(gè)激活的過程。
    #將Y矩陣拆分成上下部分之后,再分別進(jìn)行標(biāo)準(zhǔn)化。
    columnlines_y=columnLinesOfL-1
    (y1,y2)=self.split_by_row(y,columnlines_y)
    y1_normalization=self.normalization(y1)
    y2_normalization=self.normalization(y2)
    #論文中提出一種計(jì)算重構(gòu)誤差時(shí)要考慮的權(quán)重信息。具體見論文,這里暫時(shí)不實(shí)現(xiàn)。
    #這個(gè)權(quán)重是可以修改的。
    alpha_cat=1 
    bcat=1
    #計(jì)算重構(gòu)誤差矩陣
##    constant1=tf.constant([[1.0,2.0,3.0],[4.0,5.0,6.0],[7.0,8.0,9.0]])
##    constant2=tf.constant([[1.0,2.0,3.0],[1.0,4.0,2.0],[1.0,6.0,1.0]])
##    constructionErrorMatrix=self.constructionError(constant1,constant2,alpha_cat,bcat)
    y1c1=tf.subtract(y1_normalization,c1)
    y2c2=tf.subtract(y2_normalization,c2)    
    constructionErrorMatrix=self.constructionError(y1c1,y2c2,alpha_cat,bcat)
################################################################################
    print_info=tf.Print(iiii,[iiii],"\niiii:")#專門為了調(diào)試用,輸出相關(guān)信息。
    tfPrint=print_info+tfPrint
    print_info=tf.Print(columnLinesOfL,[columnLinesOfL],"\nbefore modify. columnLinesOfL:")#專門為了調(diào)試用,輸出相關(guān)信息。
    tfPrint=print_info+tfPrint
    print_info=tf.Print(constructionErrorMatrix,[constructionErrorMatrix],"\nbefore modify. constructionErrorMatrix:",summarize=100)#專門為了調(diào)試用,輸出相關(guān)信息。
    tfPrint=tf.to_int32(print_info[0])+tfPrint#一種不斷輸出tf.Print的方式,注意tf.Print的返回值。
################################################################################
    J_minpos=tf.to_int32(tf.argmin(constructionErrorMatrix))#如果不轉(zhuǎn)換的話,下面調(diào)用delete_one_column中,會(huì)調(diào)用tf.slice,之后tf.slice的參數(shù)中的類型必須是一樣的。
    J_min=constructionErrorMatrix[J_minpos]
    #一共要進(jìn)行sl-1次循環(huán)。因?yàn)槭菑膕l個(gè)葉子節(jié)點(diǎn),兩兩結(jié)合sl-1次,才能形成一顆完整的樹,而且是采用Greedy的方式。
    #所以,需要為下次循環(huán)做準(zhǔn)備。
    #第一步,從該sentence的詞向量矩陣中刪除第J_minpos+1列,因?yàn)榈贘_minpos和第J_minpos+1列對(duì)應(yīng)的單詞要合并為一個(gè)新的節(jié)點(diǎn),這里就是修改L
################################################################################
    print_info=tf.Print(self.L,[self.L[0]],"\nbefore modify. L row 0:",summarize=100)#專門為了調(diào)試用,輸出相關(guān)信息。
    tfPrint=tf.to_int32(print_info[0][0])+tfPrint
    print_info=tf.Print(self.L,[tf.shape(self.L)],"\nbefore modify. L shape:")#專門為了調(diào)試用,輸出相關(guān)信息。
    tfPrint=tf.to_int32(print_info[0][0])+tfPrint
################################################################################
    deleteColumnIndex=J_minpos+1
    self.L=self.delete_one_column(self.L,deleteColumnIndex,self.numlinesOfL,columnLinesOfL)
    columnLinesOfL=tf.subtract(columnLinesOfL,1) #列數(shù)減去1.
################################################################################
    print_info=tf.Print(deleteColumnIndex,[deleteColumnIndex],"\nbefore modify. deleteColumnIndex:")#專門為了調(diào)試用,輸出相關(guān)信息。
    tfPrint=print_info+tfPrint
    print_info=tf.Print(self.L,[self.L[0]],"\nafter modify. L row 0:",summarize=100)#專門為了調(diào)試用,輸出相關(guān)信息。
    tfPrint=tf.to_int32(print_info[0][0])+tfPrint
    
    print_info=tf.Print(self.L,[tf.shape(self.L)],"\nafter modify. L shape:")#專門為了調(diào)試用,輸出相關(guān)信息。
    tfPrint=tf.to_int32(print_info[0][0])+tfPrint
    print_info=tf.Print(columnLinesOfL,[columnLinesOfL],"\nafter modify. columnLinesOfL:")#專門為了調(diào)試用,輸出相關(guān)信息。
    tfPrint=print_info+tfPrint
################################################################################
    
    #第二步,將新的詞向量賦值給第J_minpos列
    columnTensor=p_normalization[:,J_minpos]
    new_column_tensor=tf.expand_dims(columnTensor,1)
    self.L=self.modify_one_column(self.L,new_column_tensor,J_minpos,self.numlinesOfL,columnLinesOfL)
    #第三步,同時(shí)將新的非葉子節(jié)點(diǎn)的詞向量存入nodes_tensor
    modified_index_tensor=tf.to_int32(tf.add(iiii,self.sentence_length))
    nodes_tensor=self.modify_one_column(nodes_tensor,new_column_tensor,modified_index_tensor,self.numlines_tensor,self.numcolunms_tensor)
    #第四步:記錄合并節(jié)點(diǎn)的最小損失,存入node_tensors_cost_tensor
    J_min_tensor=tf.expand_dims(tf.expand_dims(J_min,0),1)
    node_tensors_cost_tensor=self.modify_one_column(node_tensors_cost_tensor,J_min_tensor,iiii,self.numlines_tensor2,self.numcolunms_tensor2)
    ####進(jìn)入下一次循環(huán)
    iiii=tf.add(iiii,1)
    print_info=tf.Print(J_minpos,[J_minpos,J_minpos+1],"node:")#專門為了調(diào)試用,輸出相關(guān)信息。
    tfPrint=tfPrint+print_info
#    columnLinesOfL=tf.subtract(columnLinesOfL,1) #在上面的循環(huán)體中已經(jīng)執(zhí)行了,沒有必要再執(zhí)行。
    return iiii,columnLinesOfL,node_tensors_cost_tensor,nodes_tensor,tfPrint
   iiii,columnLinesOfL,node_tensors_cost_tensor,nodes_tensor,tfPrint=tf.while_loop(loop____cond,____recurrence,loop____vars,parallel_iterations=1)
   pass

上述代碼是Greedy算法,遞歸構(gòu)建神經(jīng)網(wǎng)絡(luò)樹結(jié)構(gòu)。

但是程序出錯(cuò)了,后來不斷的調(diào)試,才發(fā)現(xiàn)self.L雖然跟循環(huán)loop____vars中的變量有依賴關(guān)系,也就是在tf.while_loop進(jìn)行循環(huán)的時(shí)候,也可以輸出它的值。

但是,它每一次都無法真正意義上對(duì)self.L進(jìn)行修改。會(huì)發(fā)現(xiàn),每一次循環(huán)結(jié)束之后,進(jìn)入下一次循環(huán)時(shí),self.L仍然沒有變化。

執(zhí)行結(jié)果如下:

before modify. columnLinesOfL:[31]
iiii:[0]

after modify. columnLinesOfL:[30]

before modify. L shape:[300 31]

before modify. L row 0:[0.126693 -0.013654 -0.166731 -0.13703 -0.261395 0.11459 0.016001 0.016001 0.144603 0.05588 0.171787 0.016001 1.064545 0.144603 0.130615 -0.13703 -0.261395 1.064545 -0.261395 0.144603 0.036626 1.064545 0.188871 0.201198 0.05588 0.203795 0.201198 0.03536 0.089345 0.083778 0.103635]
node:[0][1]

before modify. constructionErrorMatrix:[3.0431733686706206 11.391056715427794 19.652819956115856 13.713453313903868 11.625973829805879 12.827533320819564 9.7513513723204746 13.009151292890811 13.896089243289065 10.649829109971648 9.45239374745086 15.704486086921641 18.274065790781862 12.447866299915024 15.302996103637689 13.713453313903868 14.295549844738751 13.779406175789358 11.625212314259059 16.340507223201449 19.095964364689717 15.10149194936319 11.989443162329437 13.436654650354058 11.120373311110505 12.39345317975002 13.568052800712424 10.998430341124633 8.3223909323599869 6.8896857405641851]

after modify. L shape:[300 30]

after modify. L row 0:[0.126693 -0.166731 -0.13703 -0.261395 0.11459 0.016001 0.016001 0.144603 0.05588 0.171787 0.016001 1.064545 0.144603 0.130615 -0.13703 -0.261395 1.064545 -0.261395 0.144603 0.036626 1.064545 0.188871 0.201198 0.05588 0.203795 0.201198 0.03536 0.089345 0.083778 0.103635]

before modify. deleteColumnIndex:[1]

before modify. columnLinesOfL:[30]

iiii:[1]

before modify. L shape:[300 31]

after modify. columnLinesOfL:[29]

before modify. L row 0:[0.126693 -0.013654 -0.166731 -0.13703 -0.261395 0.11459 0.016001 0.016001 0.144603 0.05588 0.171787 0.016001 1.064545 0.144603 0.130615 -0.13703 -0.261395 1.064545 -0.261395 0.144603 0.036626 1.064545 0.188871 0.201198 0.05588 0.203795 0.201198 0.03536 0.089345 0.083778 0.103635]

before modify. deleteColumnIndex:[1]
node:[0][1]

before modify. constructionErrorMatrix:[3.0431733686706206 11.391056715427794 19.652819956115856 13.713453313903868 11.625973829805879 12.827533320819564 9.7513513723204746 13.009151292890811 13.896089243289065 10.649829109971648 9.45239374745086 15.704486086921641 18.274065790781862 12.447866299915024 15.302996103637689 13.713453313903868 14.295549844738751 13.779406175789358 11.625212314259059 16.340507223201449 19.095964364689717 15.10149194936319 11.989443162329437 13.436654650354058 11.120373311110505 12.39345317975002 13.568052800712424 10.998430341124633 8.3223909323599869]

after modify. L shape:[300 29]

after modify. L row 0:[0.126693 -0.166731 -0.13703 -0.261395 0.11459 0.016001 0.016001 0.144603 0.05588 0.171787 0.016001 1.064545 0.144603 0.130615 -0.13703 -0.261395 1.064545 -0.261395 0.144603 0.036626 1.064545 0.188871 0.201198 0.05588 0.203795 0.201198 0.03536 0.089345 0.083778]

before modify. columnLinesOfL:[29]

iiii:[2]

后面那個(gè)after modify時(shí)L shape為[300 29]的原因是:執(zhí)行

self.L=self.modify_one_column(self.L,new_column_tensor,J_minpos,self.numlinesOfL,columnLinesOfL)

時(shí),columnLinesOfL是循環(huán)loop____vars中的變量,因此會(huì)隨著每次循環(huán)發(fā)生變化,我寫的modify_one_column見我的博文“修改tensor張量矩陣的某一列”。它決定了

修改后tensor的維度。

但是,無論如何,每一次循環(huán),都是

before modify. L shape:[300 31]

說明self.L在循環(huán)體中雖然被修改了。但是下次循環(huán)又會(huì)被重置為初始值。

以上這篇基于tensorflow for循環(huán) while循環(huán)案例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 淺析python連接數(shù)據(jù)庫的重要事項(xiàng)

    淺析python連接數(shù)據(jù)庫的重要事項(xiàng)

    這篇文章主要介紹了python連接數(shù)據(jù)庫的重要事項(xiàng),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • Python模塊域名dnspython解析

    Python模塊域名dnspython解析

    本文主要介紹了Python模塊域名dnspython解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • pandas.DataFrame 根據(jù)條件新建列并賦值的方法

    pandas.DataFrame 根據(jù)條件新建列并賦值的方法

    下面小編就為大家分享一篇pandas.DataFrame 根據(jù)條件新建列并賦值的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • python3中set(集合)的語法總結(jié)分享

    python3中set(集合)的語法總結(jié)分享

    這篇文章主要總結(jié)了關(guān)于python3中set(集合)的語法的相關(guān)資料,文中給出了詳細(xì)的示例代碼,對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。
    2017-03-03
  • Python數(shù)據(jù)分析之?Matplotlib?折線圖繪制

    Python數(shù)據(jù)分析之?Matplotlib?折線圖繪制

    這篇文章主要介紹了Python數(shù)據(jù)分析之?Matplotlib?折線圖繪制,在數(shù)據(jù)分析中,數(shù)據(jù)可視化也非常重要,下文通過數(shù)據(jù)分析展開對(duì)折線圖的繪制,需要的小伙伴可以參考一下
    2022-05-05
  • PyG搭建GCN模型實(shí)現(xiàn)節(jié)點(diǎn)分類GCNConv參數(shù)詳解

    PyG搭建GCN模型實(shí)現(xiàn)節(jié)點(diǎn)分類GCNConv參數(shù)詳解

    這篇文章主要為大家介紹了PyG搭建GCN模型實(shí)現(xiàn)節(jié)點(diǎn)分類GCNConv參數(shù)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • 使用Python實(shí)現(xiàn)圖像顏色量化的方法

    使用Python實(shí)現(xiàn)圖像顏色量化的方法

    這篇文章主要介紹了使用Python進(jìn)行圖像顏色量化,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • 如何把python項(xiàng)目部署到linux服務(wù)器

    如何把python項(xiàng)目部署到linux服務(wù)器

    這篇文章主要介紹了如何把python項(xiàng)目部署到linux服務(wù)器,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Python 虛擬環(huán)境工作原理解析

    Python 虛擬環(huán)境工作原理解析

    這篇文章主要介紹了Python 虛擬環(huán)境工作原理解析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • Python實(shí)現(xiàn)Tracert追蹤TTL值的方法詳解

    Python實(shí)現(xiàn)Tracert追蹤TTL值的方法詳解

    Tracert命令跟蹤路由原理是IP路由每經(jīng)過一個(gè)路由節(jié)點(diǎn)TTL值會(huì)減一。本文我們將通過scapy構(gòu)造一個(gè)路由追蹤工具并實(shí)現(xiàn)一次追蹤,感興趣的小伙伴可以了解一下
    2022-10-10

最新評(píng)論

察哈| 临沂市| 彩票| 行唐县| 山丹县| 丰宁| 海淀区| 汶上县| 小金县| 宁化县| 安顺市| 锡林浩特市| 黄平县| 阳原县| 永顺县| 犍为县| 遂平县| 交城县| 凤城市| 永春县| 西丰县| 宁德市| 滦南县| 文成县| 汪清县| 界首市| 三门县| 石阡县| 渭南市| 英超| 兰坪| 尼勒克县| 定州市| 健康| 普安县| 揭阳市| 长泰县| 房产| 界首市| 水富县| 双流县|