为什么多进程失败?

时间:2019-05-14 17:14:55

标签: python-2.7 multiprocessing multiprocess cvxpy

我通常在Windows10计算机上使用python2.7,并且最近更改了我的计算机(仍为Windows10),因此我必须在新计算机上安装python和所有模块。

我创建了一些代码来解决组套索惩罚线性模型。对于那些不习惯使用这些模型的人,基本思想是为数据集(x)和响应变量(y)以及参数值(lambda1)提供输入,从而改变此参数更改模型的解。此代码利用多处理库并求解不同的模型(并行关联到不同的参数值)。

为了可复制性,我在此处包括代码的简化版本:

# -*- 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使用多重处理并行化求解器函数。

为了运行此代码,我执行:

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_1 = t.solver(problem=r1['problem'], beta_var=r1['beta_var'], lambda_param=r1['lambda_param'], lambda_vector=r1['lambda_vector'])

s_parallel_1 = t.parallel_solver(problem=r1['problem'], beta_var=r1['beta_var'], lambda_param=r1['lambda_param'], lambda_vector=r1['lambda_vector'])

在我以前的计算机中,它像一种魅力一样工作,但是在新计算机中,我不断收到有关t.parallel求解器的错误消息,但我不知道该如何解决:

enter image description here

我试图在新计算机上重新安装python和模块,但没有结果。我最初以为这可能是库版本的问题,所以我在两台计算机上都安装了相同版本:

multiprocessing.__version__
'0.70a1'
import cvxpy; cvxpy.__version__
cvxpy.__version__
'0.4.8'

PS:我知道在此示例中,我正在使用并行编程来解决具有一个可能参数值的模型,但这只是一个小示例,旨在演示并行Runnig代码提供的错误。

0 个答案:

没有答案