用法
get_next_as_optional()
返回包含所有副本的下一個值的 tf.experimental.Optional
。
如果tf.distribute.DistributedIterator
已到達序列末尾,則返回的tf.experimental.Optional
將沒有值。
示例用法:
strategy = tf.distribute.MirroredStrategy(["GPU:0", "GPU:1"])
global_batch_size = 2
steps_per_loop = 2
dataset = tf.data.Dataset.range(10).batch(global_batch_size)
distributed_iterator = iter(
strategy.experimental_distribute_dataset(dataset))
def step_fn(x):
# train the model with inputs
return x
@tf.function
def train_fn(distributed_iterator):
for _ in tf.range(steps_per_loop):
optional_data = distributed_iterator.get_next_as_optional()
if not optional_data.has_value():
break
per_replica_results = strategy.run(step_fn, args=(optional_data.get_value(),))
tf.print(strategy.experimental_local_results(per_replica_results))
train_fn(distributed_iterator)
# ([0 1], [2 3])
# ([4], [])
相關用法
- Python tf.distribute.DistributedIterator.get_next用法及代碼示例
- Python tf.distribute.DistributedDataset.__iter__用法及代碼示例
- Python tf.distribute.DistributedValues用法及代碼示例
- 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用法及代碼示例
- Python tf.distribute.OneDeviceStrategy.experimental_distribute_dataset用法及代碼示例
- Python tf.distribute.experimental.rpc.Server.create用法及代碼示例
- Python tf.distribute.experimental.MultiWorkerMirroredStrategy.experimental_distribute_dataset用法及代碼示例
- Python tf.distribute.OneDeviceStrategy.gather用法及代碼示例
- Python tf.distribute.MirroredStrategy.reduce用法及代碼示例
- Python tf.distribute.Strategy.scope用法及代碼示例
- Python tf.distribute.TPUStrategy.reduce用法及代碼示例
注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.distribute.DistributedIterator.get_next_as_optional。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。