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

官方教程

class tf.Variable

一個變量通過調用run()來維護圖中的狀態(tài)媒殉,通過創(chuàng)建一個類實例添加一個變量到圖中。
Variable()構造函數接受一個變量的初始值摔敛,該初始值可以是任何類型廷蓉、任何shape的張量。初始值定義了變量的類型和shape马昙,變量的shape通常是固定的桃犬,初始值是可以通過assign methods來改變的。
如果你想稍后改變變量的shape行楞,需要把assign methods中的validate_shape指定為False攒暇,示例:

tf.assign(
    ref,
    value,
    validate_shape=None,
    use_locking=None,
    name=None
)
#參數
ref:一個可變張量,應該來自一個變量節(jié)點子房,可能還未初始化
---------------------------------------------------------------------------------------------
value:一個張量形用,必需和ref類型相同,要分配給變量的值
---------------------------------------------------------------------------------------------
validate_shape:可選bool证杭。默認為True田度。如果為true,則操作將驗證“值”的形狀與要分配的張量的形狀相匹配解愤。如果為false每币,'ref'將呈現(xiàn)'value'的形狀
---------------------------------------------------------------------------------------------
use_locking: An optional bool. Defaults to True. If True, the assignment will be protected by a lock; otherwise the behavior is undefined, but may exhibit less contention.
---------------------------------------------------------------------------------------------
name:操作的名稱(可選)
#return
張量ref,該張量具有一個新的值

就像任何其他Tensor變量一樣琢歇,創(chuàng)建的變量Variable()可以用作圖中其他Ops的輸入兰怠。此外,所有為Tensor該類重載的運算符都將轉化為變量李茫,因此您還可以通過對變量進行算術將節(jié)點添加到圖中

import tensorflow as tf

# Create a variable.
w = tf.Variable(<initial-value>, name=<optional-name>)

# Use the variable in the graph like any Tensor.
y = tf.matmul(w, ...another variable or tensor...)

# The overloaded operators are available too.
z = tf.sigmoid(w + b)

# Assign a new value to the variable with `assign()` or a related method.
w.assign(w + 1.0)
w.assign_add(1.0)

啟動計算圖時揭保,必須先明確初始化變量,然后才能運行使用其值的Ops魄宏。您可以通過運行初始化操作初始化變量秸侣,從保存文件中恢復變量,或者只需運行assign一個賦值給變量的操作宠互。實際上味榛,變量初始化器op只是一個assignOp,它將變量的初始值賦給變量本身

# Launch the graph in a session.
with tf.Session() as sess:
    # Run the variable initializer.
    sess.run(w.initializer)
    # ...you now can run ops that use the value of 'w'...

通用的初始化模式是用一個方便的函數initialize_all_variables()來初始化所有的變量予跌,在啟動計算圖后run這個op:

# Add an Op to initialize all variables.
init_op = tf.initialize_all_variables()

# Launch the graph in a session.
with tf.Session() as sess:
    # Run the Op that initializes all variables.
    sess.run(init_op)
    # ...you can now run any Op that uses variable values...

如果您需要創(chuàng)建一個初始值依賴于另一個變量的變量搏色,該初始值可通過initialized_value()函數獲得,這確保變量按正確的順序初始化
所有變量都會自動收集到創(chuàng)建它們的圖形中券册。 默認情況下频轿,構造函數將新變量添加到Graph集合GraphKeys.VARIABLES, 函數all_variables()返回該集合的內容
當我們建立模型的時候烁焙,我們需要參數變量進行更好的區(qū)分和收集航邢,比如可訓練參數weights和global step等超參數;變量構造函數提供了一個trainable=<bool>參數開關骄蝇,如果 True膳殷,新變量也被添加到計算圖集合 GraphKeys.TRAINABLE_VARIABLES中,trainable_variables()返回此集合的內容九火。各種Optimizer類將該集合收集的內容列表作為默認優(yōu)化變量

Creating a variable.
tf.Variable.__init__(initial_value, trainable=True, collections=None, validate_shape=True, name=None)

用initial_value初始化值來創(chuàng)建一個新變量赚窃,新變量被添加到集合中,默認添加到[GraphKeys.VARIABLES]中.
如果trainable參數為True吃既,變量也會添加到GraphKeys.TRAINABLE_VARIABLES中

#參數
initial_value:一個tensor(變量的初始值)或者一個可轉換為tensor的python對象考榨,除非validate_shape設置為False,否則必須指定形狀 
---------------------------------------------------------------------------------------------
trainable:如果True鹦倚,將變量添加到集GraphKeys.TRAINABLE_VARIABLES河质,各種Optimizer類將該集合收集的內容列表作為默認優(yōu)化變量
---------------------------------------------------------------------------------------------
collections: List of graph collections keys. The new variable is added to these collections. Defaults to [GraphKeys.VARIABLES].
---------------------------------------------------------------------------------------------
validate_shape:如果False,允許變量初始化為未知形狀的值震叙。如果True掀鹅,initial_value的shape必須是已知的
---------------------------------------------------------------------------------------------
name:可選的,變量的名稱媒楼;默認為'Variable'并且會自動擴展
#返回
A Variable

#Raises:
ValueError: If the initial value does not have a shape and `validate_shape` is `True`.

對于變量默認名稱自動擴展乐尊,在此舉個例子:
import tensorflow as tf
with tf.name_scope('zzh') as scope:
    a = tf.Variable(tf.random_normal([10], stddev=0.35))
    b = tf.Variable(tf.random_normal([10], stddev=0.35))
    print(a.op.name, b.op.name)
#輸出
zzh/Variable zzh/Variable_1        #自動擴展為獨一無二的名稱
tf.Variable.initialized_value()

返回一個已經初始化變量的值,在用已經初始化變量的值來初始化另一個變量時划址,可用到此操作
# Initialize 'v' with a random tensor.
v = tf.Variable(tf.truncated_normal([10, 40]))
# Use `initialized_value` to guarantee that `v` has been
# initialized before its value is used to initialize `w`.
# The random values are picked only once.
w = tf.Variable(v.initialized_value() * 2.0)
tf.Variable.assign(value, use_locking=False)
給一個變量賦一個新值扔嵌,和tf.assign()的作用一樣
#參數
value:一個tensor
use_locking: If True, use locking during the assignment.
tf.Variable.assign_add(delta, use_locking=False)
給一個變量加上一個值
#參數
delta:tensor限府,要加的值
use_locking: If True, use locking during the assignment.
tf.Variable.assign_sub(delta, use_locking=False)
從一個變量中減去一個值
#參數
delta:tensor,要減去的值
use_locking: If True, use locking during the assignment.
#返回
一個tensor痢缎,該值為操作完成后的新值
tf.Variable.scatter_sub(sparse_delta, use_locking=False)
Subtracts `IndexedSlices` from this variable.
This is essentially a shortcut for `scatter_sub(self, sparse_delta.indices, sparse_delta.values)`.

# Args:
sparse_delta: `IndexedSlices` to be subtracted from this variable.
use_locking: If `True`, use locking during the operation.

# Returns:
A `Tensor` that will hold the new value of this variable after the scattered subtraction has completed.

# Raises:
ValueError: if `sparse_delta` is not an `IndexedSlices`
tf.Variable.count_up_to(limit)
一個計數器胁勺,根據計數條件是否滿足控制流程;它有兩個主要參數独旷,ref,limit署穗,表示
每次都在 ref 的基礎上遞增,直到等于 limit嵌洼;超出限制的話就拋出異常 `OutOfRangeError`.

If no error is raised, the Op outputs the value of the variable before the increment.
This is essentially a shortcut for `count_up_to(self, limit)`.

# Args:
limit: value at which incrementing the variable raises an error.

# Returns:
A `Tensor` that will hold the variable value before the increment. If no other 
Op modifies this variable, the values produced will all be distinct.
-------------------------------------------------------------------------------------------------
例子:
x = tf.Variable(1, name='X', dtype=tf.int32)
init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    for i in range(4):
        print(sess.run(x))
        print(sess.run(x.count_up_to(5)))
#輸出
1
1
2
2
3
3
4
4

stack overflow的解釋

tf.Variable.eval(session=None)
在會話中案疲,計算并返回此變量的值,這不是一個計算圖構造方法麻养,它不會將操作添加到圖形中褐啡。
This convenience method requires a session where the graph containing this variable has been launched. If no session is passed, the default session is used.

v = tf.Variable([1, 2])
init = tf.initialize_all_variables()

with tf.Session() as sess:
    sess.run(init)
    # Usage passing the session explicitly.
    print v.eval(sess)
    # Usage with the default session.  The 'with' block
    # above makes 'sess' the default session.
    print v.eval()
#參數
session:用于評估此變量的會話回溺;如果沒有春贸,則使用默認會話
#返回:
該變量的一個副本————numpy ndarray
其他屬性
 `tf.Variable.name`
The name of this variable.
-------------------------------------------------------------------------
`tf.Variable.dtype`
The `DType` of this variable.
-------------------------------------------------------------------------
`tf.Variable.get_shape()`
The `TensorShape` of this variable.
# Returns:
A `TensorShape`.
-------------------------------------------------------------------------
 `tf.Variable.device`
The device of this variable.
-------------------------------------------------------------------------
 `tf.Variable.initializer`
The initializer operation for this variable.
-------------------------------------------------------------------------
 `tf.Variable.graph`
The `Graph` of this variable.
-------------------------------------------------------------------------
 `tf.Variable.op`
The `Operation` of this variable.

Variable helper functions

tensorflow也提供了一些列函數來管理計算圖中的變量集合

tf.all_variables()

Variable()構造函數會自動地將變量添加到計算圖集合中,默認是raphKeys.VARIABLES遗遵,而該函數可以非常方便的返回集合的內容
返回值:Variable對象列表

tf.trainable_variables()

返回所有可訓練的參數變量(也就是設置了trainable=True的變量)
此類變量會被添加到GraphKeys.TRAINABLE_VARIABLES集合中
返回值:Variable對象列表

tf.initialize_all_variables()

這個就不做介紹了
不知道為什么tf1.7提示我要用老版本萍恕。。车要。允粤。。
Returns an Op that initializes all variables.
This is just a shortcut for `initialize_variables(all_variables())`

#Returns:
An Op that initializes all variables in the graph.

tf.initialize_variables(var_list, name='init')

返回初始化變量列表的Op翼岁,該函數的功能是初始化var_list里面的變量并返回op类垫,
可以理解為初始化指定的部分變量;如果var_list為空琅坡,代碼不報錯悉患;
#參數
var_list:需要初始化的Variable對象列表
name:可選的,返回操作的名稱
#返回
An Op that run the initializers of all the specified variables.

tf.assert_variables_initialized(var_list=None)

功能:檢查變量是否被初始化
如果有任何一個變量未初始化榆俺,則返回的op會觸發(fā)異常
#參數
var_list:待檢查的變量對象列表售躁,默認是all_variables().
#返回
An Op, or None if there are no variables.

Saving and Restoring Variables

class tf.train.Saver

保存和重載變量,Saver類將ops保存到checkpoints中并從中重載茴晋,Checkpoints是一個二進制格式的文件陪捷,主要包含從變量名到tensor值的映射關系
當你創(chuàng)建一個Saver對象時,你可以選擇性地為檢查點文件中的變量挑選變量名诺擅。默認情況下市袖,將每個變量Variable.name屬性的值
保存的checkpoint文件可以是多個,并且文件名會自動擴充烁涌,so苍碟,我們可以在訓練的不同階段進行保存模型的狀態(tài)酒觅;為了避免保存無限個文件,可以設置global_step參數來進行控制微峰,該參數不是必需的

saver.save(sess, 'my-model', global_step=0) ==> filename: 'my-model-0'
...
saver.save(sess, 'my-model', global_step=1000) ==> filename: 'my-model-1000'
-----------------------------------------------------------------------------------------------
另外阐滩,還提供了一些可選參數來管理如何保存:
max_to_keep:保留的最近檢查點文件的最大數量。當新文件被創(chuàng)建時县忌,舊文件被
刪除;如果無或0继效,則保留所有檢查點文件症杏,默認為5
-----------------------------------------------------------------------------------------------
keep_checkpoint_every_n_hours:每N個小時保存一個文件,適用于長時間訓練
的實驗瑞信;默認值為10000厉颤,也就意味著默認禁止該功能
示例
# Create a saver.
saver = tf.train.Saver(...variables...)
# Launch the graph and train, saving the model every 1,000 steps.
sess = tf.Session()
for step in xrange(1000000):
    sess.run(..training_op..)
    if step % 1000 == 0:
        # Append the step number to the checkpoint name:
        saver.save(sess, 'my-model', global_step=step)

In addition to checkpoint files, savers keep a protocol buffer on disk with the list of recent checkpoints. This is used to manage numbered checkpoint files and by latest_checkpoint(), which makes it easy to discover the path to the most recent checkpoint. That protocol buffer is stored in a file named 'checkpoint' next to the checkpoint files.

If you create several savers, you can specify a different filename for the protocol buffer file in the call to save().

tf.train.Saver.__ init__(var_list=None, reshape=False, sharded=False, max_to_keep=5, keep_checkpoint_every_n_hours=10000.0, name=None, restore_sequentially=False, saver_def=None, builder=None)
構造函數
var_list參數指定要保存和重載的變量,它可以傳入一個字典或者列表凡简,來看例子吧:

v1 = tf.Variable(..., name='v1')
v2 = tf.Variable(..., name='v2')

# Pass the variables as a dict:
saver = tf.train.Saver({'v1': v1, 'v2': v2})

# Or pass them as a list.
saver = tf.train.Saver([v1, v2])
# Passing a list is equivalent to passing a dict with the variable op names
# as keys:
saver = tf.train.Saver({v.op.name: v for v in [v1, v2]})
參數
var_list:要保存和重載的變量逼友,它可以傳入一個字典或者列表
-----------------------------------------------------------------------------------------------------
reshape:允許變量以不同的shape保存和加載
-----------------------------------------------------------------------------------------------------
sharded:The optional sharded argument, if True, instructs the saver to 
shard checkpoints per device.
-----------------------------------------------------------------------------------------------------
max_to_keep:保留的最近檢查點文件的最大數量
-----------------------------------------------------------------------------------------------------
keep_checkpoint_every_n_hours:每N個小時保存一個文件,適用于長時間訓練
的實驗秤涩;默認值為10000帜乞,也就意味著默認禁止該功能
-----------------------------------------------------------------------------------------------------
name:syring類型,Optional name to use as a prefix when adding operations.
-----------------------------------------------------------------------------------------------------
restore_sequentially: A Bool, which if true, causes restore of different variables 
to happen sequentially within each device. This can lower memory usage when restoring very large models.
saver_def: Optional SaverDef proto to use instead of running the builder. This 
is only useful for specialty code that wants to recreate a Saver object for a previously built Graph that had a Saver. The saver_def proto should be the one returned by the as_saver_def() call of the Saver that was created for that Graph.
builder: Optional SaverBuilder to use if a saver_def was not provided. Defaults 
to BaseSaverBuilder().

Raises:
TypeError: If `var_list` is invalid.
ValueError: If any of the keys or values in `var_list` is not unique.

tf.train.Saver.save(sess, save_path, global_step=None, latest_filename=None)

#Args:
sess: A Session to use to save the variables.
--------------------------------------------------------------------------------------
save_path: string. Path to the checkpoint filename. If the saver is `sharded`, this 
is the prefix of the sharded checkpoint filename.
--------------------------------------------------------------------------------------
global_step: If provided the global step number is appended to `save_path` 
to create the checkpoint filename. The optional argument can be a Tensor, a 
Tensor name or an integer.
--------------------------------------------------------------------------------------
latest_filename: Optional name for the protocol buffer file that will contains the list
 of most recent checkpoint filenames. That file, kept in the same directory as 
the checkpoint files, is automatically managed by the saver to keep track of 
recent checkpoints. Defaults to 'checkpoint'.
#返回
checkpoint文件的路徑

tf.train.Saver.restore(sess, save_path)
加載之前保存的變量筐眷,這些變量不需要初始化黎烈,因為重載變量行為本身就是一種初始化
Other utility methods

Sharing Variables

重點:

  • tf.get_variable(name, shape=None, dtype=tf.float32, initializer=None, trainable=True, collections=None)
  • tf.variable_scope(name_or_scope, reuse=None, initializer=None)
    講的很詳細
    隨后介紹tf.name_scope(),比較一下兩者的區(qū)別·······························待續(xù)
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末匀谣,一起剝皮案震驚了整個濱河市照棋,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌武翎,老刑警劉巖烈炭,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異宝恶,居然都是意外死亡符隙,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進店門卑惜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來膏执,“玉大人,你說我怎么就攤上這事露久「祝” “怎么了?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵毫痕,是天一觀的道長征峦。 經常有香客問我迟几,道長,這世上最難降的妖魔是什么栏笆? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任类腮,我火速辦了婚禮,結果婚禮上蛉加,老公的妹妹穿的比我還像新娘蚜枢。我一直安慰自己,他們只是感情好针饥,可當我...
    茶點故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布厂抽。 她就那樣靜靜地躺著,像睡著了一般丁眼。 火紅的嫁衣襯著肌膚如雪筷凤。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天苞七,我揣著相機與錄音藐守,去河邊找鬼。 笑死蹂风,一個胖子當著我的面吹牛卢厂,可吹牛的內容都是我干的。 我是一名探鬼主播硫眨,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼足淆,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了礁阁?” 一聲冷哼從身側響起巧号,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎姥闭,沒想到半個月后丹鸿,有當地人在樹林里發(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡棚品,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年靠欢,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片铜跑。...
    茶點故事閱讀 39,926評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡门怪,死狀恐怖,靈堂內的尸體忽然破棺而出锅纺,到底是詐尸還是另有隱情掷空,我是刑警寧澤,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站坦弟,受9級特大地震影響护锤,放射性物質發(fā)生泄漏。R本人自食惡果不足惜酿傍,卻給世界環(huán)境...
    茶點故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一烙懦、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧赤炒,春花似錦氯析、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至癣朗,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間旺罢,已是汗流浹背旷余。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留扁达,地道東北人正卧。 一個月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像跪解,于是被迫代替她去往敵國和親炉旷。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,871評論 2 354

推薦閱讀更多精彩內容