之前理解ema权重是在train阶段使用,提升网络性能,在https://github.com/openai/improved-diffusion中一直没有找到train是ema权重的使用,变化的使用是model权重(如果使用的话还需要将ema权重数值传给model权重,然后得设置为有梯度),后来查找ema权重只在train中根据model权重更新,在测试时使用。
PyTorch: Exponential Moving Average (EMA) Example
def forward(self, inputs: Tensor, return_feature: bool = False) -> Tensor:
if self.training:
return self.model(inputs, return_feature)
else:
return self.shadow(inputs, return_feature)
另外,查看self.opt也就是优化器时,保护params_groups和state,里面包含了两个相同的model权重,所以保存文件时opt文件大小也是model文件大小的2倍。不是特别明白。