Hello,
I am trying to run the following code, which I took exactly from a website, where people confirmed it to be working. Could you please help with resolving this?
I am trying to run the following code, which I took exactly from a website, where people confirmed it to be working. Could you please help with resolving this?
import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt import scipy.optimize as spopt import scipy.fftpack as spfft import scipy.ndimage as spimg import imageio import cvxpy as cvx def dct2(x): return spfft.dct(spfft.dct(x.T, norm='ortho', axis=0).T, norm='ortho', axis=0) def idct2(x): return spfft.idct(spfft.idct(x.T, norm='ortho', axis=0).T, norm='ortho', axis=0) # read original image and downsize for speed Xorig = spimg.imread('escher_waterfall.jpg', flatten=True, mode='L') # read in grayscale X = spimg.zoom(Xorig, 0.04) ny,nx = X.shape # extract small sample of signal k = round(nx * ny * 0.5) # 50% sample ri = np.random.choice(nx * ny, k, replace=False) # random sample of indices b = X.T.flat[ri] b = np.expand_dims(b, axis=1) # create dct matrix operator using kron (memory errors for large ny*nx) A = np.kron( spfft.idct(np.identity(nx), norm='ortho', axis=0), spfft.idct(np.identity(ny), norm='ortho', axis=0) ) A = A[ri,:] # same as phi times kron vx = cvx.Variable(nx * ny) objective = cvx.Minimize(cvx.norm(vx, 1)) constraints = [A*vx == b] prob = cvx.Problem(objective, constraints) result = prob.solve(verbose=True) Xat2 = np.array(vx.value).squeeze()The error I get is
Error:---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-3-518d80ccde9e> in <module>()
14 vx = cvx.Variable(nx * ny)
15 objective = cvx.Minimize(cvx.norm(vx, 1))
---> 16 constraints = [A*vx == b]
17 prob = cvx.Problem(objective, constraints)
18 result = prob.solve(verbose=True)
/anaconda3/lib/python3.6/site-packages/cvxpy/expressions/expression.py in cast_op(self, other)
43 """
44 other = self.cast_to_const(other)
---> 45 return binary_op(self, other)
46 return cast_op
47
/anaconda3/lib/python3.6/site-packages/cvxpy/expressions/expression.py in __eq__(self, other)
544 """Equality : Creates a constraint ``self == other``.
545 """
--> 546 return Equality(self, other)
547
548 @_cast_other
/anaconda3/lib/python3.6/site-packages/cvxpy/constraints/zero.py in __init__(self, lhs, rhs, constr_id)
109 """
110 def __init__(self, lhs, rhs, constr_id=None):
--> 111 self._expr = lhs - rhs
112 super(Equality, self).__init__([lhs, rhs], constr_id)
113
/anaconda3/lib/python3.6/site-packages/cvxpy/expressions/expression.py in cast_op(self, other)
43 """
44 other = self.cast_to_const(other)
---> 45 return binary_op(self, other)
46 return cast_op
47
/anaconda3/lib/python3.6/site-packages/cvxpy/expressions/expression.py in __sub__(self, other)
433 """Expression : The difference of two expressions.
434 """
--> 435 return self + -other
436
437 @_cast_other
/anaconda3/lib/python3.6/site-packages/cvxpy/expressions/expression.py in cast_op(self, other)
43 """
44 other = self.cast_to_const(other)
---> 45 return binary_op(self, other)
46 return cast_op
47
/anaconda3/lib/python3.6/site-packages/cvxpy/expressions/expression.py in __add__(self, other)
421 """Expression : Sum two expressions.
422 """
--> 423 return cvxtypes.add_expr()([self, other])
424
425 @_cast_other
/anaconda3/lib/python3.6/site-packages/cvxpy/atoms/affine/add_expr.py in __init__(self, arg_groups)
31 # For efficiency group args as sums.
32 self._arg_groups = arg_groups
---> 33 super(AddExpression, self).__init__(*arg_groups)
34 self.args = []
35 for group in arg_groups:
/anaconda3/lib/python3.6/site-packages/cvxpy/atoms/atom.py in __init__(self, *args)
39 self.args = [Atom.cast_to_const(arg) for arg in args]
40 self.validate_arguments()
---> 41 self._shape = self.shape_from_args()
42 if len(self._shape) > 2:
43 raise ValueError("Atoms must be at most 2D.")
/anaconda3/lib/python3.6/site-packages/cvxpy/atoms/affine/add_expr.py in shape_from_args(self)
39 """Returns the (row, col) shape of the expression.
40 """
---> 41 return u.shape.sum_shapes([arg.shape for arg in self.args])
42
43 def expand_args(self, expr):
/anaconda3/lib/python3.6/site-packages/cvxpy/utilities/shape.py in sum_shapes(shapes)
47 raise ValueError(
48 "Cannot broadcast dimensions " +
---> 49 len(shapes)*" %s" % tuple(shapes))
50
51 longer = shape if len(shape) >= len(t) else t
ValueError: Cannot broadcast dimensions (50,) (50, 1)