在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ 人工智能/ 變量:創(chuàng)建、初始化、保存和加載
BibTex 引用<a class="md-anchor" id="AUTOGENERATED-bibtex-citation"
術(shù)語(yǔ)表
自定義數(shù)據(jù)讀取 <a class="md-anchor" id="AUTOGENERATED-custom-data-reade
使用 GPUs <a class="md-anchor" id="AUTOGENERATED-using-gpus"></a>
Vector Representations of Words <a class="md-anchor" id="AUTOGEN
TensorFlow 個(gè)人學(xué)習(xí)心得
共享變量<a class="md-anchor" id="AUTOGENERATED-sharing-variables"></
應(yīng)用實(shí)例 <a class="md-anchor" id="AUTOGENERATED-example-uses"></a>
其他資源 <a class="md-anchor" id="AUTOGENERATED-additional-resources
偏微分方程 <a class="md-anchor" id="AUTOGENERATED-partial-differentia
TensorBoard:可視化學(xué)習(xí) <a class="md-anchor" id="AUTOGENERATED-tensorb
TensorFlow運(yùn)作方式入門(mén) <a class="md-anchor" id="AUTOGENERATED-tensorfl
常見(jiàn)問(wèn)題 <a class="md-anchor" id="AUTOGENERATED-frequently-asked-que
MNIST機(jī)器學(xué)習(xí)入門(mén) <a class="md-anchor" id="AUTOGENERATED-mnist-for-ml-
曼德布洛特(Mandelbrot)集合 <a class="md-anchor" id="AUTOGENERATED-mande
變量:創(chuàng)建、初始化、保存和加載
TensorBoard: 圖表可視化 <a class="md-anchor" id="AUTOGENERATED-tensor
簡(jiǎn)介 <a class="md-anchor" id="AUTOGENERATED-introduction"></a>
張量的階、形狀、數(shù)據(jù)類(lèi)型<a class="md-anchor" id="AUTOGENERATED-tensor-ranks-
線程和隊(duì)列 <a class="md-anchor" id="AUTOGENERATED-threading-and-queue
下載與安裝 <a class="md-anchor" id="AUTOGENERATED-download-and-setup"
常見(jiàn)問(wèn)題匯總
綜述
綜述 Overview
TensorFlow 相關(guān)資源
數(shù)據(jù)讀取 <a class="md-anchor" id="AUTOGENERATED-reading-data"></a>
遞歸神經(jīng)網(wǎng)絡(luò) <a class="md-anchor" id="AUTOGENERATED-recurrent-neural-n
深入MNIST <a class="md-anchor" id="AUTOGENERATED-deep-mnist-for-ex
增加一個(gè)新 Op <a class="md-anchor" id="AUTOGENERATED-adding-a-new-op"
卷積神經(jīng)網(wǎng)絡(luò) <a class="md-anchor" id="AUTOGENERATED-convolutional-neur
基本使用 <a class="md-anchor" id="AUTOGENERATED-basic-usage"></a>
MNIST 數(shù)據(jù)下載 <a class="md-anchor" id="AUTOGENERATED-mnist-data-dow

變量:創(chuàng)建、初始化、保存和加載

當(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文檔:

創(chuàng)建

當(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:

  • 一個(gè)Variable操作存放變量的值。
  • 一個(gè)初始化op將變量設(shè)置為初始值。這事實(shí)上是一個(gè)tf.assign操作.
  • 初始值的操作,例如示例中對(duì)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
  ...

由另一個(gè)變量初始化

你有時(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的所有變量,或是定義在列表里的變量,添加saverestoreops。saver對(duì)象提供了方法來(lái)運(yùn)行這些ops,定義檢查點(diǎn)文件的讀寫(xiě)路徑。

檢查點(diǎn)文件

變量存儲(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

恢復(fù)變量

用同一個(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
  ...

選擇存儲(chǔ)和恢復(fù)哪些變量

如果你不給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)被管理的變量。

注意:

  • 如果需要保存和恢復(fù)模型變量的不同子集,可以創(chuàng)建任意多個(gè)saver對(duì)象。同一個(gè)變量可被列入多個(gè)saver對(duì)象中,只有當(dāng)saver的restore()函數(shù)被運(yùn)行時(shí),它的值才會(huì)發(fā)生改變。
  • 如果你僅在session開(kāi)始時(shí)恢復(fù)模型變量的一個(gè)子集,你需要對(duì)剩下的變量執(zhí)行初始化op。詳情請(qǐng)見(jiàn)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