用法
call_for_each_replica(
fn, args=(), kwargs=None
)參數
-
fn要運行的函數(每個副本將運行一次)。 -
args帶有fn的位置參數的元組或列表。 -
kwargs帶有fn的關鍵字參數的字典。
返回
-
合並所有副本的
fn的返回值。
每個副本運行一次fn。
fn 可以調用 tf.get_replica_context() 來訪問 replica_id_in_sync_group 和 merge_call() 等方法。
merge_call() 用於在副本之間進行通信並重新進入 cross-replica 上下文。所有副本都在遇到merge_call() 調用後暫停執行。之後執行 merge_fn -function。然後將其結果解包並返回給每個副本調用。之後繼續執行,直到 fn 完成或遇到另一個 merge_call() 。例子:
# Called once in "cross-replica" context.
def merge_fn(distribution, three_plus_replica_id):
# sum the values across replicas
return sum(distribution.experimental_local_results(three_plus_replica_id))
# Called once per replica in `distribution`, in a "replica" context.
def fn(three):
replica_ctx = tf.get_replica_context()
v = three + replica_ctx.replica_id_in_sync_group
# Computes the sum of the `v` values across all replicas.
s = replica_ctx.merge_call(merge_fn, args=(v,))
return s + v
with distribution.scope():
# in "cross-replica" context
...
merged_results = distribution.run(fn, args=[3])
# merged_results has the values from every replica execution of `fn`.
# This statement prints a list:
print(distribution.experimental_local_results(merged_results))
相關用法
- Python tf.compat.v1.distribute.StrategyExtended.colocate_vars_with用法及代碼示例
- Python tf.compat.v1.distribute.StrategyExtended.batch_reduce_to用法及代碼示例
- Python tf.compat.v1.distribute.StrategyExtended.non_slot_devices用法及代碼示例
- Python tf.compat.v1.distribute.StrategyExtended.update用法及代碼示例
- Python tf.compat.v1.distribute.StrategyExtended.reduce_to用法及代碼示例
- Python tf.compat.v1.distribute.StrategyExtended.variable_created_in_scope用法及代碼示例
- Python tf.compat.v1.distribute.Strategy.run用法及代碼示例
- Python tf.compat.v1.distribute.Strategy.experimental_make_numpy_dataset用法及代碼示例
- Python tf.compat.v1.distribute.Strategy.make_input_fn_iterator用法及代碼示例
- Python tf.compat.v1.distribute.Strategy用法及代碼示例
- Python tf.compat.v1.distribute.Strategy.scope用法及代碼示例
- Python tf.compat.v1.distribute.Strategy.experimental_distribute_dataset用法及代碼示例
- Python tf.compat.v1.distribute.Strategy.reduce用法及代碼示例
- Python tf.compat.v1.distribute.MirroredStrategy.experimental_distribute_dataset用法及代碼示例
- Python tf.compat.v1.distribute.OneDeviceStrategy用法及代碼示例
- Python tf.compat.v1.distribute.MirroredStrategy.experimental_make_numpy_dataset用法及代碼示例
- Python tf.compat.v1.distribute.experimental.TPUStrategy.experimental_distribute_dataset用法及代碼示例
- Python tf.compat.v1.distribute.OneDeviceStrategy.scope用法及代碼示例
- Python tf.compat.v1.distribute.experimental.TPUStrategy.experimental_make_numpy_dataset用法及代碼示例
- Python tf.compat.v1.distribute.OneDeviceStrategy.experimental_distribute_dataset用法及代碼示例
注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.compat.v1.distribute.StrategyExtended.call_for_each_replica。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
