class Modulate(nn.Module):
"""Scale and shift each channel of h by a(c) and b(c), one row per class."""
def __init__(self, num_classes, channels):
super().__init__()
self.a = nn.Embedding(num_classes, channels)
self.b = nn.Embedding(num_classes, channels)
nn.init.ones_(self.a.weight) # a(c) = 1, b(c) = 0: the identity
nn.init.zeros_(self.b.weight)
def forward(self, h, c):
return self.a(c)[:, :, None, None] * h + self.b(c)[:, :, None, None]