-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import numpy as np | ||
import caring | ||
|
||
def obj_fun(theta, A, b, lamb, n_samples): | ||
# .. the lasso objective function .. | ||
loss = (0.5 / n_samples) * np.linalg.norm(A.dot(theta) - b)**2 | ||
return loss + lamb * np.sum(np.abs(theta)) | ||
|
||
|
||
|
||
def lasso_ADMM(engine: caring.Engine, A, b, max_iter=100, lam=1.): | ||
# .. initialize variables .. | ||
tau = 1. | ||
n_samples, n_features = A.shape | ||
rho = np.zeros(n_features) | ||
u = np.zeros(n_features) | ||
|
||
# .. to keep track of progress .. | ||
obj_fun_history = [] | ||
|
||
# .. cache inverse matrix .. | ||
AtA_inv = np.linalg.pinv(A.T.dot(A) / n_samples + tau * np.eye(n_features)) | ||
|
||
for i in range(max_iter): | ||
theta = AtA_inv.dot(A.T.dot(b) / n_samples + tau * (rho - u)) | ||
|
||
u0 : list[float] = engine.sum_many(u.tolist()) | ||
u = np.array(u0) / 2. | ||
theta0 = engine.sum_many(theta.tolist()) | ||
theta = np.array(theta0) / 2. | ||
print(f"u = {u}") | ||
print(f"theta = {theta}") | ||
|
||
rho = np.fmax(theta + u - lam /tau, 0) - np.fmax(-lam/tau - theta - u, 0) | ||
u = u + theta - rho | ||
obj_fun_history.append(obj_fun(theta, A, b, lam, n_samples)) | ||
|
||
return theta, obj_fun_history |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from addm import lasso_ADMM | ||
import caring | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
np.random.seed(0) | ||
|
||
n_samples, n_features = 100, 100 | ||
A = np.random.randn(n_samples, n_features) | ||
w = np.random.randn(n_features) | ||
b = A.dot(w) + np.random.randn(n_samples) | ||
|
||
# .. make b be {-1, 1} since its a classification problem .. | ||
b = np.sign(A.dot(w) + np.random.randn(n_samples)) | ||
|
||
|
||
A_1, A_2 = np.array_split(A, 2) | ||
b_1, b_2 = np.array_split(b, 2) | ||
|
||
|
||
engine = caring.setup("127.0.0.1:1234", "127.0.0.1:1235") | ||
theta_1, func_vals = lasso_ADMM(engine, A_1, b_1) | ||
# lets plot the objective values of the function | ||
# to make sure it has converged | ||
plt.plot(func_vals) | ||
plt.ylabel('function values') | ||
plt.xlabel('iterations') | ||
|
||
plt.grid() | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from addm import lasso_ADMM | ||
import caring | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
np.random.seed(0) | ||
|
||
n_samples, n_features = 100, 100 | ||
A = np.random.randn(n_samples, n_features) | ||
w = np.random.randn(n_features) | ||
b = A.dot(w) + np.random.randn(n_samples) | ||
|
||
# .. make b be {-1, 1} since its a classification problem .. | ||
b = np.sign(A.dot(w) + np.random.randn(n_samples)) | ||
|
||
|
||
A_1, A_2 = np.array_split(A, 2) | ||
b_1, b_2 = np.array_split(b, 2) | ||
|
||
|
||
engine = caring.setup("127.0.0.1:1235", "127.0.0.1:1234") | ||
theta_1, func_vals = lasso_ADMM(engine, A_2, b_2) | ||
# lets plot the objective values of the function | ||
# to make sure it has converged | ||
plt.plot(func_vals) | ||
plt.ylabel('function values') | ||
plt.xlabel('iterations') | ||
|
||
plt.grid() | ||
plt.show() |