Hello, I am trying to optimize the part of parameters in “one” nn.Parameter variable.
e.g. a simple CNN classifier
class Model(nn.Module):
    def __init__(self, device):
        super().__init__()
        self.resnet = ...
        
        feature_dim, n_cls = 64, 10
        self.classifier = torch.nn.Parameter(torch.Tensor(n_cls, feature_dim)).to(device)

    def forward(self, input):
        out = self.resnet(input)
        return out.matmul(self.classifier.t())

my_model = Model()

In some cases, I only want to optimize the certain rows in self.classifier.
e.g. [self.classifier[0], self.classifier[2], self.classifier[4], self.classifier[6], self.classifier[8]]
I figured out two possible ways to implement an optimizer for this situation, but both of them caused error.
1.
params = []
for i, para in enumerate(my_model.classifier):
    if i % 2 == 0:
        params.append(para)

optimizer = Adam(params, ...)

error msg: TypeError: optimizer can only optimize Tensors, but one of the params is Module.parameters

another similar way but different parameter group for the optimizer

params = []
for i, para in enumerate(my_model.classifier):
    if i % 2 == 0:
        params.append({"params": para})

optimizer = Adam(params, ...)

error msg: ValueError: can’t optimize a non-leaf Tensor
Does anyone have any ideas to optimize some entry in a nn.Parameter?