當(dāng)訓(xùn)練模型時(shí),用變量來(lái)存儲(chǔ)和更新參數(shù)。變量包含張量 (Tensor)存放于內(nèi)存的緩存區(qū)。建模時(shí)它們需要被明確地初始化,模型訓(xùn)練后它們必須被存儲(chǔ)到磁盤(pán)。這些變量的值可在之后模型訓(xùn)練和分析是被加載。
本文檔描述以下兩個(gè)TensorFlow類(lèi)。點(diǎn)擊以下鏈接可查看完整的API文檔:
tf.Variable 類(lèi)tf.train.Saver 類(lèi)當(dāng)創(chuàng)建一個(gè)變量時(shí),你將一個(gè)張量作為初始值傳入構(gòu)造函數(shù)Variable()。TensorFlow提供了一系列操作符來(lái)初始化張量,初始值是常量或是隨機(jī)值。
注意,所有這些操作符都需要你指定張量的shape。那個(gè)形狀自動(dòng)成為變量的shape。變量的shape通常是固定的,但TensorFlow提供了高級(jí)的機(jī)制來(lái)重新調(diào)整其行列數(shù)。
# Create two variables.
weights = tf.Variable(tf.random_normal([784, 200], stddev=0.35),
name="weights")
biases = tf.Variable(tf.zeros([200]), name="biases")
調(diào)用tf.Variable()添加一些操作(Op, operation)到graph:
Variable操作存放變量的值。tf.assign操作.biases變量的zeros操作也被加入了graph。tf.Variable的返回值是Python的tf.Variable類(lèi)的一個(gè)實(shí)例。
變量的初始化必須在模型的其它操作運(yùn)行之前先明確地完成。最簡(jiǎn)單的方法就是添加一個(gè)給所有變量初始化的操作,并在使用模型之前首先運(yùn)行那個(gè)操作。
你或者可以從檢查點(diǎn)文件中重新獲取變量值,詳見(jiàn)下文。
使用tf.initialize_all_variables()添加一個(gè)操作對(duì)變量做初始化。記得在完全構(gòu)建好模型并加載之后再運(yùn)行那個(gè)操作。
# Create two variables.
weights = tf.Variable(tf.random_normal([784, 200], stddev=0.35),
name="weights")
biases = tf.Variable(tf.zeros([200]), name="biases")
...
# Add an op to initialize the variables.
init_op = tf.initialize_all_variables()
# Later, when launching the model
with tf.Session() as sess:
# Run the init operation.
sess.run(init_op)
...
# Use the model
...
你有時(shí)候會(huì)需要用另一個(gè)變量的初始化值給當(dāng)前變量初始化。由于tf.initialize_all_variables()是并行地初始化所有變量,所以在有這種需求的情況下需要小心。
用其它變量的值初始化一個(gè)新的變量時(shí),使用其它變量的initialized_value()屬性。你可以直接把已初始化的值作為新變量的初始值,或者把它當(dāng)做tensor計(jì)算得到一個(gè)值賦予新變量。
# Create a variable with a random value.
weights = tf.Variable(tf.random_normal([784, 200], stddev=0.35),
name="weights")
# Create another variable with the same value as 'weights'.
w2 = tf.Variable(weights.initialized_value(), name="w2")
# Create another variable with twice the value of 'weights'
w_twice = tf.Variable(weights.initialized_value() * 0.2, name="w_twice")
tf.initialize_all_variables()函數(shù)便捷地添加一個(gè)op來(lái)初始化模型的所有變量。你也可以給它傳入一組變量進(jìn)行初始化。詳情請(qǐng)見(jiàn)Variables Documentation,包括檢查變量是否被初始化。
最簡(jiǎn)單的保存和恢復(fù)模型的方法是使用tf.train.Saver對(duì)象。構(gòu)造器給graph的所有變量,或是定義在列表里的變量,添加save和restoreops。saver對(duì)象提供了方法來(lái)運(yùn)行這些ops,定義檢查點(diǎn)文件的讀寫(xiě)路徑。
變量存儲(chǔ)在二進(jìn)制文件里,主要包含從變量名到tensor值的映射關(guān)系。
當(dāng)你創(chuàng)建一個(gè)Saver對(duì)象時(shí),你可以選擇性地為檢查點(diǎn)文件中的變量挑選變量名。默認(rèn)情況下,將每個(gè)變量Variable.name屬性的值。
用tf.train.Saver()創(chuàng)建一個(gè)Saver來(lái)管理模型中的所有變量。
# Create some variables.
v1 = tf.Variable(..., name="v1")
v2 = tf.Variable(..., name="v2")
...
# Add an op to initialize the variables.
init_op = tf.initialize_all_variables()
# Add ops to save and restore all the variables.
saver = tf.train.Saver()
# Later, launch the model, initialize the variables, do some work, save the
# variables to disk.
with tf.Session() as sess:
sess.run(init_op)
# Do some work with the model.
..
# Save the variables to disk.
save_path = saver.save(sess, "/tmp/model.ckpt")
print "Model saved in file: ", save_path
用同一個(gè)Saver對(duì)象來(lái)恢復(fù)變量。注意,當(dāng)你從文件中恢復(fù)變量時(shí),不需要事先對(duì)它們做初始化。
# Create some variables.
v1 = tf.Variable(..., name="v1")
v2 = tf.Variable(..., name="v2")
...
# Add ops to save and restore all the variables.
saver = tf.train.Saver()
# Later, launch the model, use the saver to restore variables from disk, and
# do some work with the model.
with tf.Session() as sess:
# Restore variables from disk.
saver.restore(sess, "/tmp/model.ckpt")
print "Model restored."
# Do some work with the model
...
如果你不給tf.train.Saver()傳入任何參數(shù),那么saver將處理graph中的所有變量。其中每一個(gè)變量都以變量創(chuàng)建時(shí)傳入的名稱(chēng)被保存。
有時(shí)候在檢查點(diǎn)文件中明確定義變量的名稱(chēng)很有用。舉個(gè)例子,你也許已經(jīng)訓(xùn)練得到了一個(gè)模型,其中有個(gè)變量命名為"weights",你想把它的值恢復(fù)到一個(gè)新的變量"params"中。
有時(shí)候僅保存和恢復(fù)模型的一部分變量很有用。再舉個(gè)例子,你也許訓(xùn)練得到了一個(gè)5層神經(jīng)網(wǎng)絡(luò),現(xiàn)在想訓(xùn)練一個(gè)6層的新模型,可以將之前5層模型的參數(shù)導(dǎo)入到新模型的前5層中。
你可以通過(guò)給tf.train.Saver()構(gòu)造函數(shù)傳入Python字典,很容易地定義需要保持的變量及對(duì)應(yīng)名稱(chēng):鍵對(duì)應(yīng)使用的名稱(chēng),值對(duì)應(yīng)被管理的變量。
注意:
restore()函數(shù)被運(yùn)行時(shí),它的值才會(huì)發(fā)生改變。tf.initialize_variables()。# Create some variables.
v1 = tf.Variable(..., name="v1")
v2 = tf.Variable(..., name="v2")
...
# Add ops to save and restore only 'v2' using the name "my_v2"
saver = tf.train.Saver({"my_v2": v2})
# Use the saver object normally after that.
...
原文鏈接: http://tensorflow.org/how_tos/variables/index.html 翻譯:趙屹華 校對(duì):Wiki