返回當前的 tf.distribute.ReplicaContext 或 None 。
用法
tf.distribute.get_replica_context()返回
- 
目前的tf.distribute.ReplicaContext在副本上下文範圍內時的對象,否則None.在一個特定的塊中,這兩種情況中的一種將是正確的: - get_replica_context()返回非- None,或
- tf.distribute.is_cross_replica_context()返回真。
 
如果在 cross-replica 上下文中,則返回 None。
注意執行:
- 在默認 (single-replica) 副本上下文中啟動(此函數將返回默認的 ReplicaContext對象);
- 進入 with tf.distribute.Strategy.scope():塊時切換到 cross-replica 上下文(在這種情況下,這將返回None);
- 切換到 strategy.run(fn, ...)內的(非默認)副本上下文;
- 如果 fn調用get_replica_context().merge_call(merge_fn, ...),那麽在merge_fn中,您將返回 cross-replica 上下文(此函數將再次返回None)。
大多數tf.distribute.Strategy 方法隻能在cross-replica 上下文中執行,在副本上下文中,您應該改用此方法返回的tf.distribute.ReplicaContext 對象的API。
assert tf.distribute.get_replica_context() is not None  # default
with strategy.scope():
  assert tf.distribute.get_replica_context() is None
  def f():
    replica_context = tf.distribute.get_replica_context()  # for strategy
    assert replica_context is not None
    tf.print("Replica id:", replica_context.replica_id_in_sync_group,
             " of ", replica_context.num_replicas_in_sync)
  strategy.run(f)相關用法
- Python tf.distribute.get_strategy用法及代碼示例
- Python tf.distribute.OneDeviceStrategy.experimental_distribute_values_from_function用法及代碼示例
- Python tf.distribute.TPUStrategy用法及代碼示例
- Python tf.distribute.experimental_set_strategy用法及代碼示例
- Python tf.distribute.experimental.MultiWorkerMirroredStrategy.gather用法及代碼示例
- Python tf.distribute.cluster_resolver.TFConfigClusterResolver用法及代碼示例
- Python tf.distribute.experimental.MultiWorkerMirroredStrategy用法及代碼示例
- Python tf.distribute.TPUStrategy.experimental_assign_to_logical_device用法及代碼示例
- Python tf.distribute.NcclAllReduce用法及代碼示例
- Python tf.distribute.OneDeviceStrategy.experimental_distribute_dataset用法及代碼示例
- Python tf.distribute.experimental.rpc.Server.create用法及代碼示例
- Python tf.distribute.experimental.MultiWorkerMirroredStrategy.experimental_distribute_dataset用法及代碼示例
- Python tf.distribute.OneDeviceStrategy.gather用法及代碼示例
- Python tf.distribute.MirroredStrategy.reduce用法及代碼示例
- Python tf.distribute.Strategy.scope用法及代碼示例
- Python tf.distribute.TPUStrategy.reduce用法及代碼示例
- Python tf.distribute.experimental.partitioners.Partitioner.__call__用法及代碼示例
- Python tf.distribute.DistributedIterator.get_next用法及代碼示例
- Python tf.distribute.Strategy.experimental_distribute_values_from_function用法及代碼示例
注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.distribute.get_replica_context。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
