thuml / Transfer-Learning-Library

Transfer Learning Library for Domain Adaptation, Task Adaptation, and Domain Generalization
http://transfer.thuml.ai
MIT License
3.39k stars 553 forks source link

reweight/iwan.py bug #201

Closed sunray42 closed 10 months ago

sunray42 commented 1 year ago

Describe the bug

def get_importance_weight(self, feature):
      """
      Get importance weights for each instance.
      Args:
          feature (tensor): feature from source domain, in shape :math:`(N, F)`
      Returns:
          instance weight in shape :math:`(N, 1)`
      """
      weight = 1. - self.discriminator(feature)
      weight = weight / weight.mean()
      weight = weight.detach()
      return weight

其中weight / weight.mean()可能会产生除0操作,导致nan。

增加条件判断 if weight.mean(): weight = weight / weight.mean()

thucbx99 commented 1 year ago

感谢指出这个bug,这里除的时候增加一个epsilon就可以了,我们会尽快修一下~