Hi, I want to do a constrained optimization with PyTorch
. I want to find the minimum of a function $f(x_1, x_2, \dots, x_n)$, with \sum_{i=1}^n x_i=5 and x_i \geq 0. I think this could be done via Softmax
. So I follow the How to do constrained optimization in PyTorch
import torch
from torch import nn
x = torch.rand(2)
x.requires_grad = True
lin = nn.Linear(2, 1)
optimizer = torch.optim.Adam([x], lr=0.1)
for i in range(100):
optimizer.zero_grad()
y = lin(x)
y.backward()
optimizer.step()
with torch.no_grad():
x = nn.Softmax(dim=-1)(x) * 5
If print(y)
in each step,the output is:
tensor([-0.2826], grad_fn=<AddBackward0>)
tensor([-0.9759], grad_fn=<AddBackward0>)
tensor([-0.9794], grad_fn=<AddBackward0>)
tensor([-0.9880], grad_fn=<AddBackward0>)
tensor([-1.0062], grad_fn=<AddBackward0>)
tensor([-1.0284], grad_fn=<AddBackward0>)
tensor([-1.0360], grad_fn=<AddBackward0>)
tensor([-1.0368], grad_fn=<AddBackward0>)
tensor([-1.0368], grad_fn=<AddBackward0>)
tensor([-1.0368], grad_fn=<AddBackward0>)
tensor([-1.0368], grad_fn=<AddBackward0>)
tensor([-1.0368], grad_fn=<AddBackward0>)
...
The following value does not change.
for i in lin.parameters():
print(i)
The weight of the linear layer is
Parameter containing:
tensor([[-0.2240, -0.1983]], requires_grad=True)
Parameter containing:
tensor([0.0823], requires_grad=True)
So the formula is $y=-0.224x_1-0.1983x_2+0.0823=-0.0257*x_1-0.9092$,whose minimum should be -1.0377. Is the above code wrong? How to modify? Thank you very much.