cvxpy dimensions matching but i get value error

895 views
Skip to first unread message

Ali Mousavi

unread,
Jun 28, 2016, 8:48:43 PM6/28/16
to cvxpy

I'm new to cvxpy, my variable have the same dimensions but I get a valueerror. here is my code:

from cvxpy import *

import numpy as np
import cPickle as pickle
import h5py
import pdb



with open('test pickle', "rb") as f:
    cluster, example =pickle.load(f)

path = '/path/to/file.mat'
f = h5py.File(path)
feat_set = np.array(f['feat_set'])

feat_square = np.square(feat_set)
feat_norm = np.sqrt(np.sum(feat_square, axis = 0))
normalized_feat_set = feat_set/feat_norm

Y = normalized_feat_set[np.where(cluster.labels_==1)]
Y = np.transpose(Y)
print Y.shape
m,n = Y.shape 
w = Variable(m)
objective = Minimize(sum_entries(np.square(sum_entries(Y, 1) - w))

the error i get is:

/usr/local/lib/python2.7/dist-packages/cvxpy/expressions/expression.py:286: UserWarning: Forming a nonconvex expression (affine)*(affine).
  warnings.warn("Forming a nonconvex expression (affine)*(affine).")
Traceback (most recent call last):
  File "cvx_test.py", line 30, in <module>
    objective = Minimize(sum_entries(np.square(sum_entries(Y, 1) - w)))# + lamda_*norm(w,1))
  File "/usr/local/lib/python2.7/dist-packages/cvxpy/expressions/expression.py", line 43, in cast_op
    return binary_op(self, other)
  File "/usr/local/lib/python2.7/dist-packages/cvxpy/expressions/expression.py", line 287, in __mul__
    return cvxtypes.affine_prod_expr()(self, other)
  File "/usr/local/lib/python2.7/dist-packages/cvxpy/atoms/affine_prod.py", line 30, in __init__
    super(affine_prod, self).__init__(x, y)
  File "/usr/local/lib/python2.7/dist-packages/cvxpy/atoms/atom.py", line 47, in __init__
    self.validate_arguments()
  File "/usr/local/lib/python2.7/dist-packages/cvxpy/atoms/affine_prod.py", line 73, in validate_arguments
    u.shape.mul_shapes(self.args[0].size, self.args[1].size)
  File "/usr/local/lib/python2.7/dist-packages/cvxpy/utilities/shape.py", line 55, in mul_shapes
    lh_shape, rh_shape))
ValueError: Incompatible dimensions (512, 1) (512, 1)

I'm not sure what is wrong as both my variable in the minimize function have the same dimensions. can it be that because i'm using np.square it creates an error?


Steven Diamond

unread,
Jun 28, 2016, 10:08:17 PM6/28/16
to cvxpy
Yes, you can never use numpy functions on cvxpy objects. Use the cvxpy square function instead of np.square.

Ali Mousavi

unread,
Jun 29, 2016, 5:14:39 AM6/29/16
to cvxpy
that seem to have done the trick
Reply all
Reply to author
Forward
0 new messages