給定一個任意函數,將其包裝,以便進行變量共享。
用法
tf.compat.v1.make_template(
name_, func_, create_scope_now_=False, unique_name_=None, custom_getter_=None,
**kwargs
)
參數
-
name_
此模板創建的範圍的名稱。如有必要,將通過將_N
附加到名稱來使名稱唯一。 -
func_
要包裝的函數。 -
create_scope_now_
控製在構造模板時或調用模板時是否應創建範圍的布爾值。默認為 False,這意味著在調用模板時創建範圍。 -
unique_name_
使用時,它會覆蓋 name_ 並且不是唯一的。如果相同範圍/unique_name 的模板已經存在並且重用為 false,則會引發錯誤。默認為無。 -
custom_getter_
func_
中使用的變量的可選自定義 getter。有關詳細信息,請參閱tf.compat.v1.get_variable
custom_getter
文檔。 -
**kwargs
應用於func_
的關鍵字參數。
返回
-
封裝一組變量的函數,這些變量應該創建一次並重用。在調用
make_template
或調用結果時將創建封閉範圍,具體取決於create_scope_now_
的值。不管取值如何,第一次調用模板都會進入作用域,不重用,調用func_
創建變量,保證唯一。所有後續調用都將重新進入範圍並重用這些變量。
拋出
-
ValueError
如果name_
為無。
遷移到 TF2
警告:這個 API 是為 TensorFlow v1 設計的。繼續閱讀有關如何從該 API 遷移到本機 TensorFlow v2 等效項的詳細信息。見TensorFlow v1 到 TensorFlow v2 遷移指南有關如何遷移其餘代碼的說明。
tf.compat.v1.make_template
是一個遺留 API,僅與啟用的渴望執行和 tf.function
兼容,如果你將它與 tf.compat.v1.keras.utils.track_tf1_style_variables
結合使用。有關更多信息,請參閱 make_template
上的模型映射遷移指南部分:
即使您將舊版 API 用於基於 variable_scope
的變量重用,我們建議直接使用 tf.compat.v1.keras.utils.track_tf1_style_variables
而不是使用 tf.compat.v1.make_template
,因為它以比 make_template
更簡單和更可預測的方式與即刻執行互操作。
TF2 API 方法將使用 tf.Module
或 Keras 層和模型來跟蹤您的變量,而不是依賴 make_template
。
這將 func_
包裝在模板中並對其進行部分評估。模板是在第一次調用它們時創建變量並在之後重用它們的函數。為了使 func_
與 Template
兼容,它必須具有以下屬性:
- 該函數應創建所有可訓練變量和應通過調用
tf.compat.v1.get_variable
重用的任何變量。如果使用tf.Variable
創建可訓練變量,則會拋出 ValueError。可以通過指定tf.Variable(..., trainable=false)
來創建旨在成為局部變量的變量。 - 該函數可以在內部使用變量範圍和其他模板來創建和重用變量,但不應使用
tf.compat.v1.global_variables
來捕獲在函數範圍之外定義的變量。 - 內部範圍和變量名不應依賴於未提供給
make_template
的任何參數。一般來說,你會得到一個 ValueError ,告訴你如果你犯了錯誤,你正在嘗試重用一個不存在的變量。
在以下示例中,z
和 w
都將按相同的 y
進行縮放。重要的是要注意,如果我們沒有分配 scalar_name
並為 z 和 w 使用不同的名稱,則會拋出 ValueError
,因為它無法重用變量。
def my_op(x, scalar_name):
var1 = tf.compat.v1.get_variable(scalar_name,
shape=[],
initializer=tf.compat.v1.constant_initializer(1))
return x * var1
scale_by_y = tf.compat.v1.make_template('scale_by_y', my_op, scalar_name='y')
z = scale_by_y(input1)
w = scale_by_y(input2)
作為 safe-guard,如果通過調用 tf.Variable
創建可訓練變量,則返回的函數將在第一次調用後引發 ValueError
。
如果所有這些都是真的,那麽模板會強製執行 2 個屬性:
- 多次調用同一個模板將共享所有非局部變量。
- 兩個不同的模板保證是唯一的,除非您重新輸入與模板的初始定義相同的變量範圍並重新定義它。此異常的示例:
def my_op(x, scalar_name):
var1 = tf.compat.v1.get_variable(scalar_name,
shape=[],
initializer=tf.compat.v1.constant_initializer(1))
return x * var1
with tf.compat.v1.variable_scope('scope') as vs:
scale_by_y = tf.compat.v1.make_template('scale_by_y', my_op,
scalar_name='y')
z = scale_by_y(input1)
w = scale_by_y(input2)
# Creates a template that reuses the variables above.
with tf.compat.v1.variable_scope(vs, reuse=True):
scale_by_y2 = tf.compat.v1.make_template('scale_by_y', my_op,
scalar_name='y')
z2 = scale_by_y2(input1)
w2 = scale_by_y2(input2)
根據 create_scope_now_
的值,可以在第一次調用或構造時捕獲完整的變量範圍。如果此選項設置為 True,則通過重複調用模板創建的所有張量將在其名稱後附加一個 _N+1,因為第一次在模板構造函數中輸入範圍時,不會創建張量。
注意:name_
, func_
和 create_scope_now_
有一個尾隨下劃線,以減少與 kwargs 衝突的可能性。
相關用法
- Python tf.compat.v1.map_fn用法及代碼示例
- Python tf.compat.v1.math.log_softmax用法及代碼示例
- Python tf.compat.v1.math.softmax用法及代碼示例
- Python tf.compat.v1.mixed_precision.enable_mixed_precision_graph_rewrite用法及代碼示例
- Python tf.compat.v1.metrics.mean用法及代碼示例
- Python tf.compat.v1.multinomial用法及代碼示例
- Python tf.compat.v1.mixed_precision.MixedPrecisionLossScaleOptimizer用法及代碼示例
- Python tf.compat.v1.metrics.accuracy用法及代碼示例
- Python tf.compat.v1.distributions.Multinomial.stddev用法及代碼示例
- Python tf.compat.v1.distribute.MirroredStrategy.experimental_distribute_dataset用法及代碼示例
- Python tf.compat.v1.data.TFRecordDataset.interleave用法及代碼示例
- Python tf.compat.v1.distributions.Bernoulli.cross_entropy用法及代碼示例
- Python tf.compat.v1.Variable.eval用法及代碼示例
- Python tf.compat.v1.train.FtrlOptimizer.compute_gradients用法及代碼示例
- Python tf.compat.v1.layers.conv3d用法及代碼示例
- Python tf.compat.v1.strings.length用法及代碼示例
- Python tf.compat.v1.data.Dataset.snapshot用法及代碼示例
- Python tf.compat.v1.data.experimental.SqlDataset.reduce用法及代碼示例
- Python tf.compat.v1.feature_column.categorical_column_with_vocabulary_file用法及代碼示例
- Python tf.compat.v1.data.TextLineDataset.from_tensors用法及代碼示例
注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.compat.v1.make_template。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。