tf.distribute.DistributedValues
Базовый класс для представления распределённых значений.
tf.distribute.DistributedValues(
values
)
Экземпляр подкласса tf.distribute.DistributedValues создаётся при создании переменных в стратегии распределения, итерации tf.distribute.DistributedDataset или через tf.distribute.Strategy.run. Этот базовый класс не следует создавать напрямую. tf.distribute.DistributedValues содержит значение для каждой реплики. В зависимости от подкласса, значения могут синхронизироваться при обновлении, по запросу или никогда не синхронизироваться.
tf.distribute.DistributedValues можно свести к единственному значению по всем репликам, как вход для tf.distribute.Strategy.run, или просмотреть значения по каждой реплике с помощью tf.distribute.Strategy.experimental_local_results.
Пример использования:
- Создано из
tf.distribute.DistributedDataset:
strategy = tf.distribute.MirroredStrategy(["GPU:0", "GPU:1"]) dataset = tf.data.Dataset.from_tensor_slices([5., 6., 7., 8.]).batch(2) dataset_iterator = iter(strategy.experimental_distribute_dataset(dataset)) distributed_values = next(dataset_iterator)
- Возвращено функцией
run:
strategy = tf.distribute.MirroredStrategy(["GPU:0", "GPU:1"]) @tf.function def run(): ctx = tf.distribute.get_replica_context() return ctx.replica_id_in_sync_group distributed_values = strategy.run(run)
- В качестве входных данных для
run:
strategy = tf.distribute.MirroredStrategy(["GPU:0", "GPU:1"]) dataset = tf.data.Dataset.from_tensor_slices([5., 6., 7., 8.]).batch(2) dataset_iterator = iter(strategy.experimental_distribute_dataset(dataset)) distributed_values = next(dataset_iterator) @tf.function def run(input): return input + 1.0 updated_value = strategy.run(run, args=(distributed_values,))
- Сведение значения:
strategy = tf.distribute.MirroredStrategy(["GPU:0", "GPU:1"])
dataset = tf.data.Dataset.from_tensor_slices([5., 6., 7., 8.]).batch(2)
dataset_iterator = iter(strategy.experimental_distribute_dataset(dataset))
distributed_values = next(dataset_iterator)
reduced_value = strategy.reduce(tf.distribute.ReduceOp.SUM,
distributed_values,
axis = 0)
- Просмотр значений локальной реплики:
strategy = tf.distribute.MirroredStrategy(["GPU:0", "GPU:1"]) dataset = tf.data.Dataset.from_tensor_slices([5., 6., 7., 8.]).batch(2) dataset_iterator = iter(strategy.experimental_distribute_dataset(dataset)) per_replica_values = strategy.experimental_local_results( distributed_values) per_replica_values (<tf.Tensor: shape=(1,), dtype=float32, numpy=array([5.], dtype=float32)>, <tf.Tensor: shape=(1,), dtype=float32, numpy=array([6.], dtype=float32)>)
© 2022 The TensorFlow Authors. All rights reserved.
Licensed under the Creative Commons Attribution License 4.0.
Code samples licensed under the Apache 2.0 License.
https://www.tensorflow.org/versions/r2.9/api_docs/python/tf/distribute/DistributedValues