CVXOPT Quadratic Optimization - python

When using CVXOPT to optimize my SVM problem, I receive the error given below.
Traceback (most recent call last):
File "C:\Users\mospic\AppData\Roaming\Python\Python310\site-packages\cvxopt\misc.py", line 1429, in factor
lapack.potrf(F['S'])
ArithmeticError: 25
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\mospic\AppData\Roaming\Python\Python310\site-packages\cvxopt\coneprog.py", line 2065, in coneqp
try: f = kktsolver(W)
File "C:\Users\mospic\AppData\Roaming\Python\Python310\site-packages\cvxopt\coneprog.py", line 1981, in kktsolver
return factor(W, P)
File "C:\Users\mospic\AppData\Roaming\Python\Python310\site-packages\cvxopt\misc.py", line 1444, in factor
lapack.potrf(F['S'])
ArithmeticError: 25
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\mospic\Desktop\EXP2_files\src1\main.py", line 28, in <module>
test_svm(1, 'Linear', 1e-4)
File "C:\Users\mospic\Desktop\EXP2_files\src1\main.py", line 21, in test_svm
model.fit(train_features, train_labels)
File "C:\Users\mospic\Desktop\EXP2_files\src1\SVM.py", line 66, in fit
sol = cvxopt.solvers.qp(P, q, G, h, A, b)
File "C:\Users\mospic\AppData\Roaming\Python\Python310\site-packages\cvxopt\coneprog.py", line 4485, in qp
return coneqp(P, q, G, h, None, A, b, initvals, kktsolver = kktsolver, options = options)
File "C:\Users\mospic\AppData\Roaming\Python\Python310\site-packages\cvxopt\coneprog.py", line 2067, in coneqp
raise ValueError("Rank(A) < p or Rank([P; A; G]) < n")
ValueError: Rank(A) < p or Rank([P; A; G]) < n
And the Code given below:
n = train_data.shape[0]
K = np.zeros((n, n))
for i in range(n):
for j in range(n):
K[i][j] = train_label[i] * train_label[j] * SupportVectorMachine().KERNEL(train_data[i], train_data[j])
#Calculate Kernal Matrix with label i and label j
# 1/2x^TPx + q^Tx
P = cvxopt.matrix(K)
q = cvxopt.matrix(-1 * np.ones(n))
# Gx <= h
temp_G1 = np.identity(n)
temp_G2 = -1 * (np.identity(n))
G = cvxopt.matrix(np.vstack((temp_G1, temp_G2)))
temp_h1 = self.C * np.ones(n)
temp_h2 = np.zeros(n)
h = cvxopt.matrix(np.hstack((temp_h1, temp_h2)).reshape(-1, 1))
# Ax = b
A = cvxopt.matrix(train_label.astype(np.double), (1, n))
b = cvxopt.matrix(0.0)
sol = cvxopt.solvers.qp(P, q, G, h, A, b)
alpha = np.array(sol['x'])
n is the length of training data, and sol is the optimal solution of the SVM question.But the Error ValueError: Rank(A) < p or Rank([P; A; G]) < n is confusing me.

Related

Implicit differentiation in sympy function

I'm trying to perform implicit differentiation to the function Lrdot which is -2*rdot/(1 - 1/r(t)), wrt an affine parameter s, where rdot is dr/ds. The error below pops up and i'm not sure how to debug.
from sympy import *
from sympy.physics.mechanics import *
r = dynamicsymbols('r')
rdot = dynamicsymbols('r', 1)
t = dynamicsymbols('t')
tdot = dynamicsymbols('t', 1)
phi = dynamicsymbols('phi')
phidot = dynamicsymbols('phi', 1)
s = symbols('s')
def F(x):
return 1-(1/x)
# Largangian
def L(a,b,c, adot, bdot, cdot, photon = true): #r,t,phi
return F(a)*(bdot)**2 - adot**2/F(a) - (a*cdot)**2
L = L(r, t, phi, rdot, tdot, phidot, photon = true)
Lr = diff(L, r)
Lrdot = diff(L, rdot)
diffLrdot = idiff(-2*rdot/(1-1/r), r, s)
print(diffLrdot)
Traceback (most recent call last):
File "/Users/myname/PycharmProjects/untitled/.idea/14.1.py", line 40, in <module>
diffLrdot = idiff(-2*rdot/(1-1/r), r, s)
File "/Users/myname/PycharmProjects/untitled/venv/lib/python3.6/site-packages/sympy/geometry/util.py", line 578, in idiff
f = {s: Function(s.name)(x) for s in eq.free_symbols
File "/Users/myname/PycharmProjects/untitled/venv/lib/python3.6/site-packages/sympy/geometry/util.py", line 579, in <dictcomp>
if s != x and s in dep}
NameError: free variable 'dep' referenced before assignment in enclosing scope

How to correctly initialize pool worker and run method

Background:
I am trying to get a pool of workers going to solve a task. My issue is that I am trying to pass it a shared variable, but I get an error. I have written an initializer method for the workers that expect my variables, but I can't seem to get it to work.
Here is my code:
from matplotlib import pyplot
import time
import multiprocessing
#initialize some multiprocessing stuff
num_processes = 8
y = multiprocessing.Array('d', 1000, lock=False)
new_y = multiprocessing.Array('d', 1000, lock=False)
dt = multiprocessing.Value('d',0, lock=False)
y_len = multiprocessing.Value('i',len(y), lock=False)
def init(y_to_share, new_y_to_share):
global y, new_y
y = y_to_share
new_y = new_y_to_share
y[480:520] = [1] * 40
dt.value = 0.01
# our rule for reaction-diffusion
def advance():
global y, new_y
n = len(y)
new_y = list(y)
for j in xrange(n):
new_y[j] += dt * (20 * (y[j - 1] - 2 * y[j] + y[(j + 1) % n])
- y[j] * (1 - y[j]) * (0.3 - y[j]))
y = new_y
return y
# advance through t (t = i * dt) is at least 100; plot
# every 20
chunks = len(y)/num_processes
y_range = range(len(y))
y_range = [y_range[i:i+chunks] for i in range(0, len(y_range), chunks)]
p = multiprocessing.Pool(num_processes, initializer=init, initargs=(y, new_y))
i = 0
start = time.time()
while i * dt.value <= 100:
if i * dt.value % 20 == 0:
pyplot.plot(y, label='t = %g' % (i * dt.value))
arr = p.map(advance, (y, new_y))#hand in an array of indices
i += 1
#print i * dt.value
end = time.time()
elapsed = end-start
print elapsed
pyplot.legend()
pyplot.show()
Edit: Post the actual error
The error:
runfile('/home/kevin/Downloads/cbb750_parallel_hw/propagating-signal-parallel.py', wdir='/home/kevin/Downloads/cbb750_parallel_hw')
Traceback (most recent call last):
File "<ipython-input-64-5ad3fdf93b59>", line 1, in <module>
runfile('/home/kevin/Downloads/cbb750_parallel_hw/propagating-signal-parallel.py', wdir='/home/kevin/Downloads/cbb750_parallel_hw')
File "/usr/local/lib/python2.7/dist-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "/usr/local/lib/python2.7/dist-packages/spyder/utils/site/sitecustomize.py", line 94, in execfile
builtins.execfile(filename, *where)
File "/home/kevin/Downloads/cbb750_parallel_hw/propagating-signal-parallel.py", line 45, in <module>
arr = p.map(advance, (y, new_y))#hand in an array of indices
File "/usr/lib/python2.7/multiprocessing/pool.py", line 251, in map
return self.map_async(func, iterable, chunksize).get()
File "/usr/lib/python2.7/multiprocessing/pool.py", line 567, in get
raise self._value
PicklingError: Can't pickle <class 'multiprocessing.sharedctypes.c_double_Array_1000'>: attribute lookup multiprocessing.sharedctypes.c_double_Array_1000 failed
Can anyone help me solve this? I am not sure what I am doing wrong, but I would like to use pool.map .

Cuda out of resources error when running python numbapro

I am trying to run a cuda kernel in numbapro python, but I keep getting an out of resources error.
I then tried to execute the kernel into a loop and send smaller arrays, but that still gave me the same error.
Here is my error message:
Traceback (most recent call last):
File "./predict.py", line 418, in <module>
predict[griddim, blockdim, stream](callResult_d, catCount, numWords, counts_d, indptr_d, indices_d, probtcArray_d, priorC_d)
File "/home/mhagen/Developer/anaconda/lib/python2.7/site-packages/numba/cuda/compiler.py", line 228, in __call__
sharedmem=self.sharedmem)
File "/home/mhagen/Developer/anaconda/lib/python2.7/site-packages/numba/cuda/compiler.py", line 268, in _kernel_call
cu_func(*args)
File "/home/mhagen/Developer/anaconda/lib/python2.7/site-packages/numba/cuda/cudadrv/driver.py", line 1044, in __call__
self.sharedmem, streamhandle, args)
File "/home/mhagen/Developer/anaconda/lib/python2.7/site-packages/numba/cuda/cudadrv/driver.py", line 1088, in launch_kernel
None)
File "/home/mhagen/Developer/anaconda/lib/python2.7/site-packages/numba/cuda/cudadrv/driver.py", line 215, in safe_cuda_api_call
self._check_error(fname, retcode)
File "/home/mhagen/Developer/anaconda/lib/python2.7/site-packages/numba/cuda/cudadrv/driver.py", line 245, in _check_error
raise CudaAPIError(retcode, msg)
numba.cuda.cudadrv.driver.CudaAPIError: Call to cuLaunchKernel results in CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES
Here is my source code:
from numbapro.cudalib import cusparse
from numba import *
from numbapro import cuda
#cuda.jit(argtypes=(double[:], int64, int64, double[:], int64[:], int64[:], double[:,:], double[:] ))
def predict( callResult, catCount, wordCount, counts, indptr, indices, probtcArray, priorC ):
i = cuda.threadIdx.x + cuda.blockIdx.x * cuda.blockDim.x
correct = 0
wrong = 0
lastDocIndex = -1
maxProb = -1e6
picked = -1
for cat in range(catCount):
probSum = 0.0
for j in range(indptr[i],indptr[i+1]):
wordIndex = indices[j]
probSum += (counts[j]*math.log(probtcArray[cat,wordIndex]))
probSum += math.log(priorC[cat])
if probSum > maxProb:
maxProb = probSum
picked = cat
callResult[i] = picked
predictions = []
counter = 1000
for i in range(int(math.ceil(numDocs/(counter*1.0)))):
docTestSliceList = docTestList[i*counter:(i+1)*counter]
numDocsSlice = len(docTestSliceList)
docTestArray = np.zeros((numDocsSlice,numWords))
for j,doc in enumerate(docTestSliceList):
for ind in doc:
docTestArray[j,ind['term']] = ind['count']
docTestArraySparse = cusparse.ss.csr_matrix(docTestArray)
start = time.time()
OPT_N = numDocsSlice
blockdim = 1024, 1
griddim = int(math.ceil(float(OPT_N)/blockdim[0])), 1
catCount = len(music_categories)
callResult = np.zeros(numDocsSlice)
stream = cuda.stream()
with stream.auto_synchronize():
probtcArray_d = cuda.to_device(numpy.asarray(probtcArray),stream)
priorC_d = cuda.to_device(numpy.asarray(priorC),stream)
callResult_d = cuda.to_device(callResult, stream)
counts_d = cuda.to_device(docTestArraySparse.data, stream)
indptr_d = cuda.to_device(docTestArraySparse.indptr, stream)
indices_d = cuda.to_device(docTestArraySparse.indices, stream)
predict[griddim, blockdim, stream](callResult_d, catCount, numWords, counts_d, indptr_d, indices_d, probtcArray_d, priorC_d)
callResult_d.to_host(stream)
#stream.synchronize()
predictions += list(callResult)
print "prediction %d: %f" % (i,time.time()-start)
I found out this was in the cuda procedure.
When you call predict the blockdim is set to 1024.
predict[griddim, blockdim, stream](callResult_d, catCount, numWords, counts_d, indptr_d, indices_d, probtcArray_d, priorC_d)
But the procedure is called iteratively with slice sizes of 1000 elements not 1024.
So, in the procedure it will attempt to write 24 elements that are out of bounds in the return array.
Sending a number of elements parameter (n_el) and placing an error checking call in the cuda procedure solves it.
#cuda.jit(argtypes=(double[:], int64, int64, int64, double[:], int64[:], int64[:], double[:,:], double[:] ))
def predict( callResult, n_el, catCount, wordCount, counts, indptr, indices, probtcArray, priorC ):
i = cuda.threadIdx.x + cuda.blockIdx.x * cuda.blockDim.x
if i < n_el:
....

Python Code for plot. Receiving a message that I have 1 less Y value

from scitools.std import *
t = []
v = []
infile = open('running.txt', 'r')
for line in infile:
tnext, vnext = line.strip().split(',')
t.append(float(tnext))
v.append(float(vnext))
infile.close()
a = []
for i in range(len(t)-1):
a.append((v[i+1] - v[i])/(t[i+1] - t[i]))
s = []
for i in range(len(t)-1):
s.append((v[i+1])*(t[i+1]-t[i]))
plot(t, a)
plot(t, s)
This is the outcome of the code(error):
Traceback (most recent call last):
File "******1c.py", line 20, in <module>
plot(t, a)
File "/usr/lib/python2.6/site-packages/scitools/easyviz/common.py", line 3046, in plot
format=''))
File "/usr/lib/python2.6/site-packages/scitools/easyviz/common.py", line 372, in __init__
self.setp(**kwargs)
File "/usr/lib/python2.6/site-packages/scitools/easyviz/common.py", line 445, in setp
'not %d.' % (size(x),size(x),size(y))
AssertionError: Line.setp: x has size 1219, expected y to have size 1219, not 1218.
The problem is on the last line. I have 1219 x points and 1218 y points. How can I fix this?

Scipy minimize erroring for large array of variables

Hi I am trying to use scipy for optimization. The minimize function with method as 'COBYLA' is working fine for small array size but errors out for larger sized arrays. I tried with 'COBYLA' and 'SLSQP' methods since I have a constrained optimization problem for non-linear functions.
Code snippet:
import scipy as sp
import random
def mytest7obj(x, x_d, y_d, z_d, power):
for x_i in x:
if x_i < 0:
return 0.
sum = 0.
for i in range(x_d):
for j in range(z_d):
term = 1.
for k in range(y_d):
term *= (x[i*(y_d*z_d)+j*(y_d)+k] ** power[k])
sum += term
return 0. - sum
def mytest7():
x_d = 30
y_d = 10
z_d = 100
goal = 1000000.
constraints = []
power = []
for i in range(y_d):
power.append(random.uniform(0.,0.3))
constraints.append({'type':'ineq', 'fun': lambda x: goal - sum(x)})
print 'power: %s\n' % (power,)
result = sp.optimize.minimize(fun = mytest7obj, x0 = [30.] * (x_d*y_d*z_d), method = 'COBYLA', args = (x_d, y_d, z_d, power), jac=False, constraints=constraints, options={'disp':True, 'rhobeg':3., 'maxiter': 10000})
print 'goal attained: %s'% (sum(result.x),)
if __name__ == “__main__”:
mytest7()
The traceback of the error with method 'COBYLA' is:
Traceback (most recent call last):
File "opt_test.py", line 584, in <module>
print 'mytest7'; mytest7()
File "opt_test.py", line 571, in mytest7
result = sp.optimize.minimize(fun = mytest7obj, x0 = [30.] * (x_d*y_d*z_d), method = 'COBYLA', args = (x_d, y_d, z_d, power), jac=False, constraints=constraints, options={'disp':True, 'rhobeg':3., 'maxiter': 10000})
File "/usr/lib64/python2.7/site-packages/scipy/optimize/_minimize.py", line 385, in minimize
return _minimize_cobyla(fun, x0, args, constraints, **options)
File "/usr/lib64/python2.7/site-packages/scipy/optimize/cobyla.py", line 238, in _minimize_cobyla
dinfo=info)
ValueError: failed to create intent(cache|hide)|optional array-- must have defined dimensions but got (-1594577286,)
With 'SLSQP', the error is:
File "opt_test.py", line 586, in <module>
print 'test'; test()
File "opt_test.py", line 454, in test
x = get_optimal(base, budget, initial_values, x_elas, y_elas, x_history, y_history, constraint_coeffs, opt_method = 'SLSQP')
File "opt_test.py", line 355, in get_optimal
constraints=constraints, options=opts)
File "/usr/lib64/python2.7/site-packages/scipy/optimize/_minimize.py", line 388, in minimize
constraints, **options)
File "/usr/lib64/python2.7/site-packages/scipy/optimize/slsqp.py", line 316, in _minimize_slsqp
w = zeros(len_w)
MemoryError
I am using python 2.7.5,
scipy version: 0.14.0rc1,
numpy version: 1.8.1

Categories