Here is a toy example:
class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.p = nn.Parameter(torch.tensor(0.5))

    def forward(self, x):
        y = x * self.p
        return y

model = Model()
optimizer = torch.optim.Adam(model.parameters())
loss_func = torch.nn.MSELoss()
y = torch.tensor([0.1,0.2,0.3])
x = torch.tensor([1.,2.,3.])
model.train()
for i in range(3):
    pred = model(x)
    optimizer.zero_grad()
    loss = loss_func(pred, y)
    loss.backward()
    optimizer.step()

I want to optimize p in a certain range. For example, the value of p must between 0.2 to 0.6 in during training process.