Pytorch Assigning fixed parameter to the model
I am only interest in the feature map after 2 convolution layers with specific weights.
class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()

        self.layer1 = nn.Sequential(
            nn.Conv2d(in_channels=1, out_channels=1, kernel_size=(2, 2), stride=1, padding=1, bias=False),
            nn.AvgPool2d(kernel_size=(2, 2), stride=(2, 2))
        )
        with torch.no_grad():
            weights1 = torch.tensor([[0.2390, 0.1593], [0.5377, 0]])
            self.layer1.weight = nn.Parameter(weights1, requires_grad=False)

        self.layer2 = nn.Sequential(
            nn.Conv2d(in_channels=1, out_channels=1, kernel_size=(2, 2), stride=1, padding=1, bias=False),
            nn.AvgPool2d(kernel_size=(2, 2), stride=(2, 2))
        )
        with torch.no_grad():
            weights2 = torch.tensor([[-0.2390, -0.3585], [-0.5377, 0.2390]])
            self.layer2.weight = nn.Parameter(weights2, requires_grad=False)

    def forward(self, x):
        x = self.layer1(x)
        x = self.layer2(x)
        return x

Due to the limitation of real sensor implementation, I have to use fixed weights as above.
Problem is that the output is not constant.
&gt;&gt;&gt;list(model.parameters())
[Parameter containing:
tensor([[0.2390, 0.1593],
        [0.5377, 0.0000]]), Parameter containing:
tensor([[[[-0.2701,  0.1602],
          [-0.0056, -0.0924]]]], requires_grad=True), Parameter containing:
tensor([[-0.2390, -0.3585],
        [-0.5377,  0.2390]]), Parameter containing:
tensor([[[[-0.0287,  0.2864],
          [ 0.3319, -0.3913]]]], requires_grad=True)]

Above is the result of model's parameter and you can see there are other parameters.
Do you know how to fix the paramteres?

You are not accessing the weight property on the incorrect object: self.layer1 and self.layer2 are not nn.Conv2d instances, they are nn.Sequential layers. Doing so you are essentially registering two new tensors (the fixed ones) to your module, added the two tensor parameters instantiated by the nn.Conv2d layers.
You should assign the fixed parameters to self.layer1[0] and (self.layer2[0] respectively):
self.layer1[0].weight = nn.Parameter(weights1, requires_grad=False)
# and
self.layer2[0].weight = nn.Parameter(weights2, requires_grad=False)

Then .parameters() will generate two tensor parameters:
&gt;&gt;&gt; list
[Parameter containing:
 tensor([[0.2390, 0.1593],
         [0.5377, 0.0000]]), Parameter containing:
 tensor([[-0.2390, -0.3585],
         [-0.5377,  0.2390]])]

