用法
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.compat.v1.distribute.StrategyExtended.batch_reduce_to用法及代码示例
- Python tf.compat.v1.distribute.StrategyExtended.colocate_vars_with用法及代码示例
- Python tf.compat.v1.distribute.StrategyExtended.non_slot_devices用法及代码示例
- Python tf.compat.v1.distribute.StrategyExtended.update用法及代码示例
- Python tf.compat.v1.distribute.StrategyExtended.call_for_each_replica用法及代码示例
- 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.reduce_to。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
