【发布时间】:2021-02-06 11:26:47
【问题描述】:
我正在使用 PyTorch Lightning 训练图像分类模型,并在具有多个 GPU 的机器上运行,因此我使用推荐的分布式后端以获得最佳性能 ddp (DataDistributedParallel)。这自然会拆分数据集,因此每个 GPU 只会看到数据的一部分。
但是,对于验证,我想计算整个验证集的准确度等指标,而不仅仅是部分。我该怎么做?我找到了some hints in the official documentation,但它们没有按预期工作或让我感到困惑。发生的事情是validation_epoch_end 被称为num_gpus 次,每个验证数据为1/num_gpus。我想汇总所有结果,只运行一次validation_epoch_end。
在this section 中,他们声明当使用 dp/ddp2 时,您可以添加一个像这样调用的附加函数
def validation_step(self, batch, batch_idx):
loss, x, y, y_hat = self.step(batch)
return {"val_loss": loss, 'y': y, 'y_hat': y_hat}
def validation_step_end(self, self, *args, **kwargs):
# do something here, I'm not sure what,
# as it gets called in ddp directly after validation_step with the exact same values
return args[0]
但是,结果并未汇总,validation_epoch_end 仍被称为num_gpu 次。这种行为是不是ddp不可用?还有其他方法可以实现这种聚合行为吗?
【问题讨论】:
标签: python parallel-processing pytorch distributed-computing pytorch-lightning