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


Python tf.compat.v1.distribute.experimental.CentralStorageStrategy用法及代码示例


one-machine 策略将所有变量放在单个设备上。

继承自:Strategy

用法

tf.compat.v1.distribute.experimental.CentralStorageStrategy(
    compute_devices=None, parameter_device=None
)

属性

  • cluster_resolver 返回与此策略关联的集群解析器。

    一般来说,当使用multi-worker tf.distribute 策略如tf.distribute.experimental.MultiWorkerMirroredStrategytf.distribute.TPUStrategy() 时,有一个tf.distribute.cluster_resolver.ClusterResolver 与所使用的策略相关联,并且这样的实例由该属性返回。

    打算拥有关联tf.distribute.cluster_resolver.ClusterResolver 的策略必须设置相关属性,或覆盖此属性;否则,默认返回None。这些策略还应提供有关此属性返回的内容的信息。

    Single-worker 策略通常没有 tf.distribute.cluster_resolver.ClusterResolver ,在这些情况下,此属性将返回 None

    当用户需要访问集群规范、任务类型或任务 ID 等信息时,tf.distribute.cluster_resolver.ClusterResolver 可能很有用。例如,

    os.environ['TF_CONFIG'] = json.dumps({
      'cluster':{
          'worker':["localhost:12345", "localhost:23456"],
          'ps':["localhost:34567"]
      },
      'task':{'type':'worker', 'index':0}
    })
    
    # This implicitly uses TF_CONFIG for the cluster and current task info.
    strategy = tf.distribute.experimental.MultiWorkerMirroredStrategy()
    
    ...
    
    if strategy.cluster_resolver.task_type == 'worker':
      # Perform something that's only applicable on workers. Since we set this
      # as a worker above, this block will run on this particular instance.
    elif strategy.cluster_resolver.task_type == 'ps':
      # Perform something that's only applicable on parameter servers. Since we
      # set this as a worker above, this block will not run on this particular
      # instance.

    有关详细信息,请参阅 tf.distribute.cluster_resolver.ClusterResolver 的 API 文档字符串。

  • extended tf.distribute.StrategyExtended 与其他方法。
  • num_replicas_in_sync 返回聚合梯度的副本数。

变量分配给本地 CPU 或唯一的 GPU。如果有多个 GPU,计算操作(变量更新操作除外)将在所有 GPU 上复制。

例如:

strategy = tf.distribute.experimental.CentralStorageStrategy()
# Create a dataset
ds = tf.data.Dataset.range(5).batch(2)
# Distribute that dataset
dist_dataset = strategy.experimental_distribute_dataset(ds)

with strategy.scope():
  @tf.function
  def train_step(val):
    return val + 1

  # Iterate over the distributed dataset
  for x in dist_dataset:
    # process dataset elements
    strategy.run(train_step, args=(x,))

相关用法


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