顺序编程和并行编程之间的解决方案差异



我创建了一个python代码,可以解决一组套索惩罚线性模型。对于那些不习惯使用这些模型的人来说,基本思想是你给出一个数据集(x(和一个响应变量(y(作为输入,以及一个参数的值(lambda1(,改变这个参数的值会改变模型的解决方案。所以我决定使用多处理库并求解不同的模型(与不同的参数值相关联(。我创建了一个名为"model.py"的python文件,其中包含以下功能:

# -*- coding: utf-8 -*-
from __future__ import division
import functools
import multiprocessing as mp
import numpy as np
from cvxpy import *
def lm_gl_preprocessing(x, y, index, lambda1=None):
lambda_vector = [lambda1]
m = x.shape[1]
n = x.shape[0]
lambda_param = Parameter(sign="positive")
m = m+1
index = np.append(0, index)
x = np.c_[np.ones(n), x]
group_sizes = []
beta_var = []
unique_index = np.unique(index)
for idx in unique_index:
group_sizes.append(len(np.where(index == idx)[0]))
beta_var.append(Variable(len(np.where(index == idx)[0])))
num_groups = len(group_sizes)
group_lasso_penalization = 0
model_prediction = x[:, np.where(index == unique_index[0])[0]] * beta_var[0]
for i in range(1, num_groups):
model_prediction += x[:, np.where(index == unique_index[i])[0]] * beta_var[i]
group_lasso_penalization += sqrt(group_sizes[i]) * norm(beta_var[i], 2)
lm_penalization = (1.0/n) * sum_squares(y - model_prediction)
objective = Minimize(lm_penalization + (lambda_param * group_lasso_penalization))
problem = Problem(objective)
response = {'problem': problem, 'beta_var': beta_var, 'lambda_param': lambda_param, 'lambda_vector': lambda_vector}
return response
def solver(problem, beta_var, lambda_param, lambda_vector):
beta_sol_list = []
for i in range(len(lambda_vector)):
lambda_param.value = lambda_vector[i]
problem.solve(solver=ECOS)
beta_sol = np.asarray(np.row_stack([b.value for b in beta_var])).flatten()
beta_sol_list.append(beta_sol)
return beta_sol_list
def parallel_solver(problem, beta_var, lambda_param, lambda_vector):
# Divide parameter vector into chunks to be executed in parallel
num_chunks = mp.cpu_count()
chunks = np.array_split(lambda_vector, num_chunks)
# Solve problem in parallel
pool = mp.Pool(num_chunks)
global_results = pool.map(functools.partial(solver, problem, beta_var, lambda_param), chunks)
pool.close()
pool.join()
return global_results
  • 函数lm_gl_preprocessing基本上定义了使用 cvxpy 模块求解的模型。
  • 函数求解
  • 器从前一个函数中获取模型详细信息,并求解导致模型最终解的优化问题。
  • 该函数parallel_solver使用多处理并行化求解器函数。

如果在 python 控制台中,我开始运行并行求解器,它会给出一个解决方案。此解决方案与顺序求解器提供的解决方案不同。 如果我重新启动 python 控制台并启动顺序求解器,然后运行并行求解器,则并行求解器给出的解决方案与顺序求解器相同。我将展示:

from __future__ import division
from sklearn.datasets import load_boston
import numpy as np
import model as t
boston = load_boston()
x = boston.data
y = boston.target
index = np.array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5])
lambda1 = 1e-3
r1 = t.lm_gl_preprocessing(x=x, y=y, index=index, lambda1=lambda1)
s_parallel_1 = t.parallel_solver(problem=r1['problem'], beta_var=r1['beta_var'], lambda_param=r1['lambda_param'], lambda_vector=r1['lambda_vector'])
print(s_parallel_1)
[[array([  4.61648376e+01,  -1.22394832e-04,   0.00000000e+00,
0.00000000e+00,   1.37065733e-04,   1.51910696e-03,
0.00000000e+00,   1.51910696e-03,   0.00000000e+00,
7.00079603e-03,   1.52776114e-03,  -8.67357376e-01,
7.16429750e-03,  -8.67357376e-01])], [], [], []]
s_1 = t.solver(problem=r1['problem'], beta_var=r1['beta_var'], lambda_param=r1['lambda_param'], lambda_vector=r1['lambda_vector'])
print(s_1)
[array([  3.62813738e+01,  -1.06995338e-01,   4.64210526e-02,
1.97112192e-02,   2.68475527e+00,  -1.75142155e+01,
3.80741843e+00,   5.14842823e-04,  -1.47105323e+00,
3.04949407e-01,  -1.23508259e-02,  -9.50143293e-01,
9.40708993e-03,  -5.25758097e-01])]
#####################################################
r1 = t.lm_gl_preprocessing(x=x, y=y, index=index, lambda1=lambda1)
s_1 = t.solver(problem=r1['problem'], beta_var=r1['beta_var'], lambda_param=r1['lambda_param'], lambda_vector=r1['lambda_vector'])
print(s_1)
[array([  3.62813738e+01,  -1.06995338e-01,   4.64210526e-02,
1.97112192e-02,   2.68475527e+00,  -1.75142155e+01,
3.80741843e+00,   5.14842823e-04,  -1.47105323e+00,
3.04949407e-01,  -1.23508259e-02,  -9.50143293e-01,
9.40708993e-03,  -5.25758097e-01])]
s_parallel_1 = t.parallel_solver(problem=r1['problem'], beta_var=r1['beta_var'], lambda_param=r1['lambda_param'], lambda_vector=r1['lambda_vector'])
print(s_parallel_1)
[[array([  3.62813738e+01,  -1.06995338e-01,   4.64210526e-02,
1.97112192e-02,   2.68475527e+00,  -1.75142155e+01,
3.80741843e+00,   5.14842823e-04,  -1.47105323e+00,
3.04949407e-01,  -1.23508259e-02,  -9.50143293e-01,
9.40708993e-03,  -5.25758097e-01])], [], [], []]

PS:我知道在这个例子中,我使用并行编程只是为了解决一个具有一个可能参数值的模型,但这只是一个小例子,旨在展示顺序和并行编程提供的解决方案的差异。我会感谢任何提示,因为我完全迷失在这里。

如果我执行您的代码,则在所有情况下都会得到相同的结果。这是我正在运行的代码(我合并了 2 个文件(:

from __future__ import division
import functools
import multiprocessing as mp
import numpy as np
from cvxpy import *
from sklearn.datasets import load_boston
def lm_gl_preprocessing(x, y, index, lambda1=None):
lambda_vector = [lambda1]
m = x.shape[1]
n = x.shape[0]
lambda_param = Parameter(sign="positive")
m = m+1
index = np.append(0, index)
x = np.c_[np.ones(n), x]
group_sizes = []
beta_var = []
unique_index = np.unique(index)
for idx in unique_index:
group_sizes.append(len(np.where(index == idx)[0]))
beta_var.append(Variable(len(np.where(index == idx)[0])))
num_groups = len(group_sizes)
group_lasso_penalization = 0
model_prediction = x[:, np.where(index == unique_index[0])[0]] * beta_var[0]
for i in range(1, num_groups):
model_prediction += x[:, np.where(index == unique_index[i])[0]] * beta_var[i]
group_lasso_penalization += sqrt(group_sizes[i]) * norm(beta_var[i], 2)
lm_penalization = (1.0/n) * sum_squares(y - model_prediction)
objective = Minimize(lm_penalization + (lambda_param * group_lasso_penalization))
problem = Problem(objective)
response = {'problem': problem, 'beta_var': beta_var, 'lambda_param': lambda_param, 'lambda_vector': lambda_vector}
return response
def solver(problem, beta_var, lambda_param, lambda_vector):
beta_sol_list = []
for i in range(len(lambda_vector)):
lambda_param.value = lambda_vector[i]
problem.solve(solver=ECOS)
beta_sol = np.asarray(np.row_stack([b.value for b in beta_var])).flatten()
beta_sol_list.append(beta_sol)
return beta_sol_list
def parallel_solver(problem, beta_var, lambda_param, lambda_vector):
# Divide parameter vector into chunks to be executed in parallel
num_chunks = mp.cpu_count()
chunks = np.array_split(lambda_vector, num_chunks)
# Solve problem in parallel
pool = mp.Pool(num_chunks)
global_results = pool.map(functools.partial(solver, problem, beta_var, lambda_param), chunks)
pool.close()
pool.join()
return global_results
if __name__ == "__main__":
boston = load_boston()
x = boston.data
y = boston.target
index = np.array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5])
lambda1 = 1e-3
r1 = lm_gl_preprocessing(x=x, y=y, index=index, lambda1=lambda1)
s_parallel_1 = parallel_solver(problem=r1['problem'], beta_var=r1['beta_var'], lambda_param=r1['lambda_param'], lambda_vector=r1['lambda_vector'])
print(s_parallel_1)
r1 = lm_gl_preprocessing(x=x, y=y, index=index, lambda1=lambda1)
s_1 = solver(problem=r1['problem'], beta_var=r1['beta_var'], lambda_param=r1['lambda_param'], lambda_vector=r1['lambda_vector'])
print(s_1)
print ("#####################################################")
r1 = lm_gl_preprocessing(x=x, y=y, index=index, lambda1=lambda1)
s_1 = solver(problem=r1['problem'], beta_var=r1['beta_var'], lambda_param=r1['lambda_param'], lambda_vector=r1['lambda_vector'])
print(s_1)
r1 = lm_gl_preprocessing(x=x, y=y, index=index, lambda1=lambda1)
s_parallel_1 = parallel_solver(problem=r1['problem'], beta_var=r1['beta_var'], lambda_param=r1['lambda_param'], lambda_vector=r1['lambda_vector'])
print(s_parallel_1)

和输出:

[[array([ 3.62813738e+01, -1.06995338e-01,  4.64210526e-02,  1.97112192e-02,
2.68475527e+00, -1.75142155e+01,  3.80741843e+00,  5.14842823e-04,
-1.47105323e+00,  3.04949407e-01, -1.23508259e-02, -9.50143293e-01,
9.40708993e-03, -5.25758097e-01])], [], [], []]
[array([ 3.62813738e+01, -1.06995338e-01,  4.64210526e-02,  1.97112192e-02,
2.68475527e+00, -1.75142155e+01,  3.80741843e+00,  5.14842823e-04,
-1.47105323e+00,  3.04949407e-01, -1.23508259e-02, -9.50143293e-01,
9.40708993e-03, -5.25758097e-01])]
#####################################################
[array([ 3.62813738e+01, -1.06995338e-01,  4.64210526e-02,  1.97112192e-02,
2.68475527e+00, -1.75142155e+01,  3.80741843e+00,  5.14842823e-04,
-1.47105323e+00,  3.04949407e-01, -1.23508259e-02, -9.50143293e-01,
9.40708993e-03, -5.25758097e-01])]
[[array([ 3.62813738e+01, -1.06995338e-01,  4.64210526e-02,  1.97112192e-02,
2.68475527e+00, -1.75142155e+01,  3.80741843e+00,  5.14842823e-04,
-1.47105323e+00,  3.04949407e-01, -1.23508259e-02, -9.50143293e-01,
9.40708993e-03, -5.25758097e-01])], [], [], []]

如您所见,我有相同数量的 CPU(4(。

我的环境是Linux上的Python2.7,这些是相关软件包的版本:

>>> import sklearn
>>> sklearn.__version__
'0.19.2'
>>> import scipy
>>> scipy.__version__
'1.1.0'
>>> import numpy 
>>> numpy.__version__
'1.15.2'
>>> import cvxpy
>>> cvxpy.__version__
'0.4.0'
>>> import multiprocessing
>>> multiprocessing.__version__
'0.70a1'

相关内容

  • 没有找到相关文章

最新更新