用法
reduce_to(
    reduce_op, value, destinations, options=None
)參數
- 
reduce_op一個tf.distribute.ReduceOp值,指定如何組合值。允許使用枚舉的字符串表示,例如"SUM"、"MEAN"。
- 
valuetf.distribute.DistributedValues或tf.Tensor類似對象。
- 
destinationstf.distribute.DistributedValues、tf.Variable、tf.Tensor類似對象或設備字符串。它指定要減少到的設備。要執行 all-reduce,請將其傳遞給value和destinations。請注意,如果它是tf.Variable,則該值將減少到該變量的設備,並且此方法不會更新該變量。
- 
options一個tf.distribute.experimental.CommunicationOptions。執行集體操作的選項。如果tf.distribute.Strategy在構造函數中采用一個,這將覆蓋默認選項。有關選項的詳細信息,請參閱tf.distribute.experimental.CommunicationOptions。
返回
- 
減少到 destinations的張量或值。
跨副本組合(通過例如總和或平均值)值。
reduce_to 聚合 tf.distribute.DistributedValues 和分布式變量。它支持密集值和 tf.IndexedSlices 。
此 API 目前隻能在cross-replica 上下文中調用。其他減少副本值的變體是:
- tf.distribute.StrategyExtended.batch_reduce_to:此 API 的批處理版本。
- tf.distribute.ReplicaContext.all_reduce:此 API 在副本上下文中的對應項。它支持批處理和非批處理all-reduce。
- tf.distribute.Strategy.reduce:在cross-replica 上下文中減少到主機的更方便的方法。
destinations 指定將值減少到的位置,例如"GPU:0"。您也可以傳入 Tensor ,目標將是該張量的設備。對於 all-reduce,將其傳遞給 value 和 destinations 。
它可用於 tf.distribute.ReplicaContext.merge_call 編寫適用於所有 tf.distribute.Strategy 的代碼。
@tf.function
def step_fn(var):
  def merge_fn(strategy, value, var):
    # All-reduce the value. Note that `value` here is a
    # `tf.distribute.DistributedValues`.
    reduced = strategy.extended.reduce_to(tf.distribute.ReduceOp.SUM,
        value, destinations=var)
    strategy.extended.update(var, lambda var, value:var.assign(value),
        args=(reduced,))
  value = tf.identity(1.)
  tf.distribute.get_replica_context().merge_call(merge_fn,
    args=(value, var))
def run(strategy):
  with strategy.scope():
    v = tf.Variable(0.)
    strategy.run(step_fn, args=(v,))
    return v
run(tf.distribute.MirroredStrategy(["GPU:0", "GPU:1"]))
MirroredVariable:{
  0:<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=2.0>,
  1:<tf.Variable 'Variable/replica_1:0' shape=() dtype=float32, numpy=2.0>
}
run(tf.distribute.experimental.CentralStorageStrategy(
    compute_devices=["GPU:0", "GPU:1"], parameter_device="CPU:0"))
<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=2.0>
run(tf.distribute.OneDeviceStrategy("GPU:0"))
<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=1.0>相關用法
- Python tf.distribute.StrategyExtended.variable_created_in_scope用法及代碼示例
- Python tf.distribute.StrategyExtended.batch_reduce_to用法及代碼示例
- Python tf.distribute.StrategyExtended.update用法及代碼示例
- Python tf.distribute.StrategyExtended.colocate_vars_with用法及代碼示例
- Python tf.distribute.Strategy.scope用法及代碼示例
- Python tf.distribute.Strategy.experimental_distribute_values_from_function用法及代碼示例
- Python tf.distribute.Strategy用法及代碼示例
- Python tf.distribute.Strategy.reduce用法及代碼示例
- Python tf.distribute.Strategy.experimental_distribute_dataset用法及代碼示例
- Python tf.distribute.Strategy.run用法及代碼示例
- Python tf.distribute.Strategy.gather用法及代碼示例
- Python tf.distribute.Server用法及代碼示例
- 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用法及代碼示例
注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.distribute.StrategyExtended.reduce_to。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
