【发布时间】:2019-01-23 12:45:57
【问题描述】:
我正在使用 GANEstimator 和 MirroredStrategy 在单个实例的多个 GPU 上工作。 input_fn 在我的例子中是 tf.data.Dataset 具有以下设置:
dataset = dataset.repeat()
dataset = dataset.shuffle(buffer_size=100)
dataset = dataset.batch(self.batch_size, drop_remainder=True)
dataset = dataset.prefetch(100)
我问这个问题的原因是我是否需要手动指定 dataset.shard() 之类的东西才能将不同的数据传递给工作人员?我正在研究Estimator 和MirroredStrategy 的代码,但我不清楚发生了什么。 description of distributed strategies:
MirroredStrategy: This does in-graph replication with synchronous
training on many GPUs on one machine. Essentially, we create copies of all
variables in the model's layers on each device. We then use all-reduce
to combine gradients across the devices before applying them
to the variables to keep them in sync.
CollectiveAllReduceStrategy: This is a version of MirroredStrategy
for multi-worker training.
那么 MirroredStratedy 是否只使用一名工人?我不明白。我需要指定批量大小等于一个塔的容量,否则我会得到 OOM。有人可以指点我的代码并解释这种简单的设置如何与批处理一起工作:
def create_dataset():
...
dataset = dataset.repeat()
dataset = dataset.shuffle(buffer_size=100)
dataset = dataset.batch(self.batch_size, drop_remainder=True)
dataset = dataset.prefetch(100)
return dataset
NUM_GPUS = 4
strategy = tf.contrib.distribute.MirroredStrategy(num_gpus=NUM_GPUS)
optimizer = tf.train.RMSPropOptimizer(learning_rate=0.01, use_locking=True)
optimizer_d = tf.train.RMSPropOptimizer(learning_rate=0.01, use_locking=True)
config = tf.estimator.RunConfig(save_checkpoints_steps=100,
save_summary_steps=1, keep_checkpoint_max=50,
train_distribute=strategy)
# I have more hooks here, just simplified to show
def get_hooks_fn(GANTrainOps):
disjoint_train_hook_func = tfgan.get_sequential_train_hooks(
train_steps=tfgan.GANTrainSteps(10, 1)
) # g steps, d steps
disjoint_train_hooks = disjoint_train_hook_func(GANTrainOps)
return [update_hook, summary_hook] + disjoint_train_hooks
# Create GAN estimator.
gan_estimator = tfgan.estimator.GANEstimator(
model_dir = '/data/checkpoints/estimator_model',
generator_fn = generator_fn,
discriminator_fn = discriminator_fn,
generator_loss_fn = generator_loss_fn,
discriminator_loss_fn = discriminator_loss_fn,
generator_optimizer = optimizer,
discriminator_optimizer = optimizer_d,
use_loss_summaries=True,
config=config,
get_hooks_fn=get_hooks_fn)
gan_estimator.train(input_fn=create_dataset, steps=10000)
谢谢!
MirroredStrategy的代码包含:
1) 奇怪的措辞:
这个类的多工作者版本将一个副本映射到一台设备上 工人。它反映了所有副本上的所有模型变量。例如,如果您 有两个
workers,每个worker有4个GPU,它将创建8个副本 这 8 个 GPU 上的模型变量。然后就像在 MirroredStrategy(???) 中一样,每个 副本使用自己的变量副本执行计算,除非在 发生变量或张量减少的跨副本模型。
2)
auto_shard_dataset:当有数据集时是否自动分片 多名工人。
此参数默认为False。
编辑:
到目前为止,我发现tf.estimator.train() 在一段时间后指向似乎是strategy.make_input_fn_iterator():
def _get_iterator_from_input_fn(self, input_fn, mode, distribution=None):
if distribution is not None:
iterator = distribution.make_input_fn_iterator(
lambda _: self._call_input_fn(input_fn, mode))
input_hooks = [
estimator_util.DistributedIteratorInitializerHook(iterator)]
else:
result = self._call_input_fn(input_fn, mode)
iterator = result.make_initializable_iterator()
input_hooks = [estimator_util._DatasetInitializerHook(iterator)]
return iterator, input_hooks
make_input_fn_iterator()
但它已从MirroredStrategy 的代码中删除,不再存在!我不明白它是如何工作的,以及数据集的实际拆分位置。
EDIT2:我在使用 grep 的 tensorflow 1.12.0 发行版中找不到行 make_input_fn_iterator。似乎它在代码中完全不存在。
【问题讨论】:
标签: tensorflow tensorflow-datasets tensorflow-estimator