Hi! I am trying to implement a pytorch-based Lasso regression but could not confirm the sparsity of the result weight matrix.
My codes:
class Lasso(nn.Module):
    "Lasso for compressing dictionary"
    def __init__(self, input_size):
        super(Lasso, self).__init__()
        self.linear = nn.Linear(input_size, 1, bias=False)
    def forward(self, x):
        out = self.linear(x)
        return out


def lasso(x, y, lr=0.005, max_iter=2000, tol=1e-4, opt='sgd'):
    # x = x.detach()
    # y = y.detach()
    lso = Lasso(x.shape[1])
    criterion = nn.MSELoss(reduction='sum')
    if opt == 'adam':
        optimizer = optim.Adam(lso.parameters(), lr=lr)
    elif opt == 'adagrad':
        optimizer = optim.Adagrad(lso.parameters(), lr=lr)
    else:
        optimizer = optim.SGD(lso.parameters(), lr=lr)
    w_prev = torch.tensor(0.)
    for it in range(max_iter):
        # lso.linear.zero_grad()
        optimizer.zero_grad()
        out = lso(x)
        loss = criterion(out, y)
        l1_norm = 0.1 * torch.norm(lso.linear.weight, p=1)
        loss += l1_norm
        loss.backward()
        optimizer.step()
        # pdb.set_trace()
        w = lso.linear.weight.detach()
        if bool(torch.norm(w_prev - w) < tol):
            break
        w_prev = w
        # if it % 100 == 0:
        # print(loss.item() - loss_prev)
    return lso.linear.weight.detach()


a = torch.randn(4, 60)
b = torch.randn(4, 1)

r = lasso(a, b, opt='adam')
print(r)
l = linear_model.Lasso(alpha=0.1, fit_intercept=False)
l.fit(a, b)
# l.path(a, b, verbose=True)
print(l.coef_)

And the printed results are:
tensor([[ 0.4202,  0.4335,  0.1444, -0.0708, -0.3143]])
[ 0.312021   -0.57850013 -0.         -0.          0.        ]

I am comparing my implementation with the sklearn Lasso implementation. I have found some answers on this discussion and Stack exchange as well but still having this problem. I wonder if there is anything wrong with my code?
Thank you very much!