Skip to content

Commit

Permalink
Add lasso-addm example
Browse files Browse the repository at this point in the history
  • Loading branch information
quackzar committed May 14, 2024
1 parent 46d6010 commit db5908b
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
38 changes: 38 additions & 0 deletions pycare/examples/addm.py
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
30 changes: 30 additions & 0 deletions pycare/examples/lasso1.py
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()
30 changes: 30 additions & 0 deletions pycare/examples/lasso2.py
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()

0 comments on commit db5908b

Please sign in to comment.