| 53 | |
| 54 | |
| 55 | class NoiseInjection(nn.Module): |
| 56 | def __init__(self): |
| 57 | super().__init__() |
| 58 | |
| 59 | self.weight = nn.Parameter(torch.zeros(1), requires_grad=True) |
| 60 | |
| 61 | def forward(self, feat, noise=None): |
| 62 | if noise is None: |
| 63 | batch, _, height, width = feat.shape |
| 64 | noise = torch.randn(batch, 1, height, width).to(feat.device) |
| 65 | |
| 66 | return feat + self.weight * noise |
| 67 | |
| 68 | |
| 69 | class Swish(nn.Module): |