1
$\begingroup$

The following is the error code generated while running lasso.py. Can anybody help in fixing the same.

Here is the code:

from cvxpy import *
import numpy as np
import cvxopt
from multiprocessing import Pool

# Problem data.
n = 10
m = 5
A = cvxopt.normal(n,m)
b = cvxopt.normal(n)
gamma = Parameter(nonneg=True)

# Construct the problem.
x = Variable(m)
objective = Minimize(sum_squares(A*x - b) + gamma*norm(x, 1))
p = Problem(objective)

# Assign a value to gamma and find the optimal x.
def get_x(gamma_value):
    gamma.value = gamma_value
    result = p.solve()
    return x.value

gammas = np.logspace(-1, 2, num=100)
# Serial computation.
x_values = [get_x(value) for value in gammas]

# Parallel computation.
pool = Pool(processes = 4)
par_x = pool.map(get_x, gammas)

for v1,v2 in zip(x_values, par_x):
    if np.linalg.norm(v1 - v2) > 1e-5:
        print("error")

Error: while running

Traceback (most recent call last):
  File "lasso.py", line 31, in <module>
    objective = Minimize(sum_squares(A*x - b) + gamma*norm(x, 1))
  File "/usr/local/lib/python3.5/dist-packages/cvxpy-1.0.11-py3.5-linux-x86_64.egg/cvxpy/expressions/expression.py", line 45, in cast_op
    return binary_op(self, other)
  File "/usr/local/lib/python3.5/dist-packages/cvxpy-1.0.11-py3.5-linux-x86_64.egg/cvxpy/expressions/expression.py", line 435, in __sub__
    return self + -other
  File "/usr/local/lib/python3.5/dist-packages/cvxpy-1.0.11-py3.5-linux-x86_64.egg/cvxpy/expressions/expression.py", line 45, in cast_op
    return binary_op(self, other)
  File "/usr/local/lib/python3.5/dist-packages/cvxpy-1.0.11-py3.5-linux-x86_64.egg/cvxpy/expressions/expression.py", line 423, in __add__
    return cvxtypes.add_expr()([self, other])
  File "/usr/local/lib/python3.5/dist-packages/cvxpy-1.0.11-py3.5-linux-x86_64.egg/cvxpy/atoms/affine/add_expr.py", line 33, in __init__
    super(AddExpression, self).__init__(*arg_groups)
  File "/usr/local/lib/python3.5/dist-packages/cvxpy-1.0.11-py3.5-linux-x86_64.egg/cvxpy/atoms/atom.py", line 41, in __init__
    self._shape = self.shape_from_args()
  File "/usr/local/lib/python3.5/dist-packages/cvxpy-1.0.11-py3.5-linux-x86_64.egg/cvxpy/atoms/affine/add_expr.py", line 41, in shape_from_args
    return u.shape.sum_shapes([arg.shape for arg in self.args])
  File "/usr/local/lib/python3.5/dist-packages/cvxpy-1.0.11-py3.5-linux-x86_64.egg/cvxpy/utilities/shape.py", line 49, in sum_shapes
    len(shapes)*" %s" % tuple(shapes))
ValueError: Cannot broadcast dimensions  (10,) (10, 1)

As suggested by multiple links, I tried with all possible python and library versions. But I am unable to fix this. Kindly throw some light.

$\endgroup$
0

2 Answers 2

0
$\begingroup$

(A*x) Expression has dimensionality of (10, ) and b has shape of ( 10, 1) - this is why you see this error.

My fix solves the error, but you should double check the results

objective = Minimize(sum_squares(A*x - np.array(b).reshape(10,)) + gamma*norm(x, 1))

Hope this helps!

$\endgroup$
0
$\begingroup$

The fix for your problem would be to change line

x = Variable(m)

into

x = Variable(shape=(m,1))
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.