I'm trying to synthesize a 1x1 convolution layer with fully connected layers. This means a fully connected neural network deciding the parameters of a 1x1 convolution layer. Here is how I do.
class Network(nn.Module):
def __init__(self, len_input, num_kernels):
self.input_layers = nn.Sequential(
nn.Linear(len_input, num_kernels * 2),
nn.ReLU(),
nn.Linear(num_kernels * 2, num_kernels),
nn.ReLU()
)
self.synthesized_conv = nn.Conv2d(in_channels=3, out_channels=num_kernels, bias=False, kernel_size=1)
self.conv_layers = nn.Sequential(
nn.ReLU(),
nn.Conv2d(in_channels=num_kernels, out_channels=3, kernel_size=1)
)
def forward(self, x1, img):
x = self.input_layer(x1.float())
with torch.no_grad():
self.synthesized_conv.weight = nn.Parameter(x.reshape_as(self.synthesized_conv.weight))
generated = self.conv_layer(self.synthesized_conv(img))
return generated
There you can see that I'm initializing a 1x1 conv layer called "synthesized_conv" and trying to replace it's parameters with a fully connected network output called "self.input_layers" with call-by-reference. However, gradients doesn't seem like flowing through the fully connected network, but only flowing through convolutional layers. Here's how parameter histogram for fully connected layers looks like:
This histogram comes as a strong indicator of those fully connected part is not learning at all. It's most likely a malpractice of convolution parameter update by fully connected network output. Can someone help me how can I do it without breaking the autograd graph?
