我正在编写内核匹配追求的代码。在那,我已经习惯了 cvxpy 进行凸优化。我必须最小化基于这篇论文的以下目标:http: //ieeexplore.ieee.org/xpl/articleDetails.jsp? arnumber=6815769 ,代码如下:
import os
import cv2
import numpy as np
import cvxpy as cp
import cvxopt
from sklearn.datasets import make_sparse_coded_signal
from sklearn.linear_model import OrthogonalMatchingPursuit
rootdir = 'F:/face train image'
image=list()
#newimg=list()
for subdir, dirs, files in os.walk(rootdir):
for file in files:
img=cv2.imread(os.path.join(subdir, file),0)
img1=cv2.resize(img,(50,50))
img2=np.reshape(img1,(2500,1))
image.append(img2)
for i in range(1,len(image)):
if i == 1:
Y=image[0]
Y=np.append(Y,image[i],1)
[r,c]=Y.shape
for i in range(0,c):
a=np.linalg.norm(Y[:,i])
for j in range(0,r):
Y[j,i]=Y[j,i]/a
yt=cv2.imread( "F:/face test image/s5/8.pgm",0)
yt=cv2.resize(yt,(50,50))
yt=np.reshape(yt,(2500,1))
Ytr=np.transpose(Y)
print Ytr.shape
ytr=np.transpose(yt)
print ytr.shape
#Kernel functions using dot product.Here only linear kernel is used.
KYY=np.dot(Ytr,Y)
Kytyt=np.dot(ytr,yt)
KytY=np.dot(ytr,Y)
lam=0.2
xt=cp.Variable(25,1,name="xt")
xtr=xt.T
epirk=Kytyt+xt.T*KYY*xt-KytY*xt
objective= cp.Minimize(epirk+lam*cp.norm1(xt))
constraints=[]
prob=cp.Problem(objective,constraints)
result=prob.solve()
但是,代码不起作用并抛出错误:不能将两个非常数相乘。我认为这是在“epirk”中将三个项相乘的问题。但是,我对此没有任何解决方案。请帮忙。