Multiply module by a learnable constant #2392
-
Hi, TLDR; I want to multiply a LBANN module like lbann.Gaussian which has a 3 dimensional output e.g. (512, 8, 8) with a learnable scalar. I am trying to convert StyleGAN2 from pytorch to LBANN. One part of this is at each layer we create a feature map with random noise to add to the outputs of the convolution layer. For example, in pytorch it would be like so: def __init__():
# learnable noise constant (B box in Karras 2019 Figure 1)
self.noise_strength = torch.nn.Parameter(torch.zeros([]))
def forward(x):
...
x = self.conv(x, ...)
# create a
noise = (
self.noise_function(
[x.shape[0], 1, self.resolution, self.resolution],
device=x.device,
) * self.noise_strength
x += noise
... In LBANN, I have the following: def __init__():
self.noise_strength = lbann.Weights(lbann.ValueInitializer(values=0.0))
self.noise_strength_layer = lbann.WeightsLayer(
dims = [1],
name = f'{name}_noise_multiplier',
weights = self.noise_strength
)
...
def forward(x):
x = self.conv(x)
noise = lbann.Scale(
lbann.Gaussian(
mean=0,
stdev=1,
neuron_dims=[self.out_channels, self.resolution, self.resolution],
# hint_layer=x
),
constant=self.noise_strength_layer
)
x = lbann.Add(x, noise) But Similarly, I have tried Any tips on how to do this seemingly simple task in LBANN? Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
A potential solution, but not sure if it still gives > 1 learnable parameters per layer: def __init__():
...
self.expected_shape = [self.output_channels, self.resolution, self.resolution]
self.noise_strength = lbann.Weights(lbann.ValueInitializer(values=0.0))
self.noise_strength_layer_orig = lbann.WeightsLayer(
dims = [1,1,1],
name = f'{name}_noise_multiplier',
weights = self.noise_strength
)
...
def forward(x):
...
self.noise_strength_layer = lbann.Identity(self.noise_strength_layer_orig)
for _ in range(0, int(np.log2(self.expected_shape[0]))):
self.noise_strength_layer = lbann.Concatenation([self.noise_strength_layer], [lbann.Identity(self.noise_strength_layer)], axis=0)
for _ in range(0, int(np.log2(self.expected_shape[1]))):
self.noise_strength_layer = lbann.Concatenation([self.noise_strength_layer], [lbann.Identity(self.noise_strength_layer)], axis=1)
for _ in range(0, int(np.log2(self.expected_shape[2]))):
self.noise_strength_layer = lbann.Concatenation([self.noise_strength_layer], [lbann.Identity(self.noise_strength_layer)], axis=2)
gaussian_noise = lbann.Gaussian(
mean=0,
stdev=1,
neuron_dims=self.expected_shape,
)
noise = lbann.Multiply(
gaussian_noise,
self.noise_strength_layer
)
x = lbann.Add(x, noise)
... Is there a simpler solution? |
Beta Was this translation helpful? Give feedback.
-
Your Python solution is close to what I'd recommend doing. Instead of concatenating the values over three dimensions, you should use the Tessellate layer with the right dimensions. Both the Tessellate solution and yours will have only one learnable parameter per layer. |
Beta Was this translation helpful? Give feedback.
Your Python solution is close to what I'd recommend doing. Instead of concatenating the values over three dimensions, you should use the Tessellate layer with the right dimensions. Both the Tessellate solution and yours will have only one learnable parameter per layer.