TensorFlow:tf.get_variable()和tf.Variable()

  • TensorFlow:tf.get_variable()和tf.Variable()

1.tf.get_variable函數

函數:tf.get_variable,獲取具有這些參數的現有變量或創建一個新變量。

get_variable(name, shape=None, dtype=None, initializer=None, regularizer=None, trainable=True, collections=None, caching_device=None, partitioner=None, validate_shape=True, use_resource=None, custom_getter=None)
此函數將名稱與當前變量範圍進行前綴,並執行重用檢查。有關重用如何工作的詳細說明,請參見變量範圍。下面是一個基本示例:

with tf.variable_scope("foo"):
    v = tf.get_variable("v", [1])  # v.name == "foo/v:0"
    w = tf.get_variable("w", [1])  # w.name == "foo/w:0"
with tf.variable_scope("foo", reuse=True):
    v1 = tf.get_variable("v")  # The same as v above.

如果初始化器爲 None(默認),則將使用在變量範圍內傳遞的默認初始化器。如果另一個也是 None,那麼一個 glorot_uniform_initializer 將被使用。初始化器也可以是張量,在這種情況下,變量被初始化爲該值和形狀。
類似地,如果正則化器是 None(默認),則將使用在變量範圍內傳遞的默認正則符號(如果另一個也是 None,則默認情況下不執行正則化)。
如果提供了分區,則返回 PartitionedVariable。作爲張量訪問此對象將返回沿分區軸連接的碎片。
一些有用的分區可用。例如:variable_axis_size_partitioner 和 min_max_variable_partitioner。

參數:
name:新變量或現有變量的名稱。
shape:新變量或現有變量的形狀。
dtype:新變量或現有變量的類型(默認爲 DT_FLOAT)。
initializer:創建變量的初始化器。
regularizer:一個函數(張量 - >張量或無);將其應用於新創建的變量的結果將被添加到集合 tf.GraphKeys.REGULARIZATION_LOSSES 中,並可用於正則化。
trainable:如果爲 True,還將變量添加到圖形集合:GraphKeys.TRAINABLE_VARIABLES。
collections:要將變量添加到其中的圖形集合鍵的列表。默認爲 [GraphKeys.LOCAL_VARIABLES]。
caching_device:可選的設備字符串或函數,描述變量應該被緩存以讀取的位置。默認爲變量的設備,如果不是 None,則在其他設備上進行緩存。典型的用法的在使用該變量的操作所在的設備上進行緩存,通過 Switch 和其他條件語句來複制重複數據刪除。
partitioner:(可選)可調用性,它接受要創建的變量的完全定義的 TensorShape 和 dtype,並且返回每個座標軸的分區列表(當前只能對一個座標軸進行分區)。
validate_shape:如果爲假,則允許使用未知形狀的值初始化變量。如果爲真,則默認情況下,initial_value 的形狀必須是已知的。
use_resource:如果爲假,則創建一個常規變量。如果爲真,則創建一個實驗性的 ResourceVariable,而不是具有明確定義的語義。默認爲假(稍後將更改爲真)。
custom_getter:可調用的,將第一個參數作爲真正的 getter,並允許覆蓋內部的 get_variable 方法。custom_getter 的簽名應該符合這種方法,但最經得起未來考驗的版本將允許更改:def custom_getter(getter, *args, **kwargs)。還允許直接訪問所有 get_variable 參數:def custom_getter(getter, name, *args, **kwargs)。創建具有修改的名稱的變量的簡單標識自定義 getter 是:python def custom_getter(getter, name, *args, **kwargs): return getter(name + ‘_suffix’, *args, **kwargs)

返回值:
創建或存在Variable(或者PartitionedVariable,如果使用分區器)。

可能引發的異常:
ValueError:當創建新的變量和形狀時,在變量創建時違反重用,或當 initializer 的 dtype 和 dtype 不匹配時。在 variable_scope 中設置重用。

initializer爲變量初始化的方法,初始化有如下幾種:

tf.constant_initializer(value=0, dtype=tf.float32):常量初始化
tf.random_normal_initializer(mean=0.0, stddev=1.0, seed=None, dtype=tf.float32):正態分佈初始化
tf.truncated_normal_initializer(mean=0.0, stddev=1.0, seed=None, dtype=tf.float32):截取的正態分佈初始化
tf.random_uniform_initializer(minval=0, maxval=None, seed=None, dtype=tf.float32):均勻分佈初始化
tf.zeros_initializer(shape, dtype=tf.float32, partition_info=None):全0常量初始化
tf.ones_initializer(dtype=tf.float32, partition_info=None):全1常量初始化
tf.uniform_unit_scaling_initializer(factor=1.0, seed=None, dtype=tf.float32):均勻分佈(不指定最小、最大值)初始化
tf.variance_scaling_initializer(scale = 1.0, mode = "fan_in", distribution = "normal", seed = None, dtype = dtypes.float32):由mode確定數量的截取的正態分佈或均勻分
tf.orthogonal_initializer(gain=1.0, dtype=tf.float32, seed=None):正交矩陣初始化
tf.glorot_uniform_initializer(seed=None, dtype=tf.float32):由輸入單元節點數和輸出單元節點數確定的均勻分佈初始化
tf.glorot_normal_initializer(seed=None, dtype=tf.float32):由輸入單元節點數和輸出單元節點數確定的截取的正態分佈初始化
note: tf.get_variable中initializer的初始化不需要指定shape了,已在外面指定

2.tf.get_variable函數:

函數:tf.Variable,新建一個變量。
Variable(initial_value=None, trainable=True, collections=None, validate_shape=True, caching_device=None, name=None, variable_def=None, dtype=None, expected_shape=None, import_scope=None)

initial_value爲初始化變量的值,有以下幾種方法:

tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
tf.random_uniform(shape, minval=0, maxval=None, dtype=tf.float32, seed=None, name=None)
tf.random_shuffle(value, seed=None, name=None)
tf.random_crop(value, size, seed=None, name=None)
tf.multinomial(logits, num_samples, seed=None, name=None)
tf.random_gamma(shape, alpha, beta=None, dtype=tf.float32, seed=None, name=None)
tf.set_random_seed(seed) 設置產生隨機數的種子
tf.zeros(shape, dtype=tf.float32, name=None)
tf.zeros_like(tensor, dtype=None, name=None)
tf.ones(shape, dtype=tf.float32, name=None)
tf.ones_like(tensor, dtype=None, name=None)
tf.fill(dims, value, name=None)
tf.constant(value, dtype=None, shape=None, name='Const')

參考:https://www.w3cschool.cn/tensorflow_python/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章