Tensorflow結點打包和依賴控制

2021-09-07 19:44:26 字數 2916 閱讀 8112

深度學習庫能夠充分發揮gpu平行計算的能力,但是有時我們卻不得不需要序列。這時就需要用到依賴控制。

import tensorflow as tf

a = tf.variable(1)

b = tf.variable(2)

s = tf.add(a, b)

asiggn = tf.assign(a, 4)

with tf.session() as sess:

sess.run(tf.global_variables_initializer())

print(sess.run([a, b, s, asiggn]))

理論上,這段程式有時輸出6,有時輸出3。也就是說,求和操作和複製操作無法確定誰先執行。

如果想要先求和再賦值,那麼需要使用依賴控制指明依賴。

import tensorflow as tf

a = tf.variable(1)

b = tf.variable(2)

s = tf.add(a, b)

with tf.control_dependencies([s]):

asiggn = tf.assign(a, 4)

with tf.session() as sess:

sess.run(tf.global_variables_initializer())

print(sess.run([a, b, s, asiggn]))

每一次sess.run,每個結點只求解一次。

然而需要理解的乙個關鍵點是:

將多個結點打包之後,對這個包加上依賴控制,並不會影響被打包的各個結點的依賴。

# 將結點打包,同時執行多個結點

import tensorflow as tf

tf.reset_default_graph()

a = tf.variable(1, name="a")

b = tf.variable(2, name="b")

c = tf.variable(3, name="c")

with tf.control_dependencies([a, b, c]):

before_sum = tf.add_n([a, b, c])

a_add1 = tf.assign(a, tf.add(a, 1, name='a_add1'))

b_add2 = tf.assign(b, tf.add(b, 2, name='b_add2'))

c_add3 = tf.assign(c, tf.add(c, 3, name='c_add3'))

with tf.control_dependencies([before_sum]):

# group操作run之後返回值為none,它只負責同時執行,它並不負責控制依賴

op = tf.group(a_add1, b_add2, c_add3)

with tf.control_dependencies([op]): # 如果沒有這句話,則sum操作和op操作是並行的,導致出現奇怪的現象

after_sum = tf.add_n([a, b, c])

with tf.session() as sess:

sess.run(tf.global_variables_initializer())

print(sess.run([before_sum, op, a, b, c, after_sum]))

輸出為:[12, none, 2, 4, 6, 12]

表示beforesum並沒有在op之前執行。

要想在op之前求before_sum,那就需要為op中的每個結點新增依賴

# 將結點打包,同時執行多個結點

import tensorflow as tf

tf.reset_default_graph()

a = tf.variable(1, name="a")

b = tf.variable(2, name="b")

c = tf.variable(3, name="c")

with tf.control_dependencies([a, b, c]):

before_sum = tf.add_n([a, b, c])

with tf.control_dependencies([before_sum]):

a_add1 = tf.assign(a, tf.add(a, 1, name='a_add1'))

with tf.control_dependencies([before_sum]):

b_add2 = tf.assign(b, tf.add(b, 2, name='b_add2'))

with tf.control_dependencies([before_sum]):

c_add3 = tf.assign(c, tf.add(c, 3, name='c_add3'))

with tf.control_dependencies([before_sum]):

# group操作run之後返回值為none,它只負責同時執行,它並不負責控制依賴

op = tf.group(a_add1, b_add2, c_add3)

with tf.control_dependencies([op]): # 如果沒有這句話,則sum操作和op操作是並行的,導致出現奇怪的現象

after_sum = tf.add_n([a, b, c])

with tf.session() as sess:

sess.run(tf.global_variables_initializer())

print(sess.run([before_sum, op, a, b, c, after_sum]))

"""執行順序不同會出現什麼情況

"""

關於tensorflow和pytorch安裝

環境 windows 7,python 3.7 pycharm 2018.2 professional 因為是全部安裝完後整理的,所以只是以解決問題為主,很多東西都沒有深入去了解原理,僅僅記錄下踩坑的過程。1.把虛擬環境的pip公升級,之前不公升級,一直報要求檢測pip的版本,所以預設公升級到最新 ...

pytorch和tensorflow安裝記錄

友情提示 如果不是非要用某個環境裡的東西,重新建乙個虛擬環境能最快最有效的解決問題。記錄一下安裝pytorch和tensorflow的艱難歷程 親測新建乙個虛擬環境安裝正確的版本可行,如果版本不正確,也會報錯找不到指定模組,我電腦配置是cuda10.1,cudnn7.5,開始安裝了2.2.0報錯找不...

argparse和tensorflow命令列引數

1 tensorflow 定義命令列 獲取命令列引數的值 print flag.name 2 定義命令列 import argparse parser argparse.argumentparser description some description 定義乙個 parser 物件 定義引數 p...