当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python tf.distribute.OneDeviceStrategy.experimental_distribute_values_from_function用法及代码示例


用法

experimental_distribute_values_from_function(
    value_fn
)

参数

  • value_fn 运行以生成值的函数。每个副本都会调用它,并以tf.distribute.ValueContext 作为唯一参数。它必须返回一个张量或可以转换为张量的类型。

返回

value_fn 生成 tf.distribute.DistributedValues

此函数用于生成 tf.distribute.DistributedValues 以传递给 run , reduce 或其他在不使用数据集时采用分布式值的方法。

示例用法:

  1. 每个副本返回常量值:
strategy = tf.distribute.MirroredStrategy(["GPU:0", "GPU:1"])
def value_fn(ctx):
  return tf.constant(1.)
distributed_values = (
     strategy.experimental_distribute_values_from_function(
       value_fn))
local_result = strategy.experimental_local_results(distributed_values)
local_result
(<tf.Tensor:shape=(), dtype=float32, numpy=1.0>,
 <tf.Tensor:shape=(), dtype=float32, numpy=1.0>)
  1. 根据replica_id在数组中分配值:
strategy = tf.distribute.MirroredStrategy(["GPU:0", "GPU:1"])
array_value = np.array([3., 2., 1.])
def value_fn(ctx):
  return array_value[ctx.replica_id_in_sync_group]
distributed_values = (
     strategy.experimental_distribute_values_from_function(
       value_fn))
local_result = strategy.experimental_local_results(distributed_values)
local_result
(3.0, 2.0)
  1. 使用 num_replicas_in_sync 指定值:
strategy = tf.distribute.MirroredStrategy(["GPU:0", "GPU:1"])
def value_fn(ctx):
  return ctx.num_replicas_in_sync
distributed_values = (
     strategy.experimental_distribute_values_from_function(
       value_fn))
local_result = strategy.experimental_local_results(distributed_values)
local_result
(2, 2)
  1. 在设备上放置值并分发:
strategy = tf.distribute.TPUStrategy()
worker_devices = strategy.extended.worker_devices
multiple_values = []
for i in range(strategy.num_replicas_in_sync):
  with tf.device(worker_devices[i]):
    multiple_values.append(tf.constant(1.0))

def value_fn(ctx):
  return multiple_values[ctx.replica_id_in_sync_group]

distributed_values = strategy.
  experimental_distribute_values_from_function(
  value_fn)

相关用法


注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.distribute.OneDeviceStrategy.experimental_distribute_values_from_function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。