这是我的 Matlab CVX
linkpair=[link_1 -link_2];
unlinkpair=[unlink_1 -unlink_2];
m=size(linkpair,2);
n=size(linkpair,1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%parametres%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
C=100;
B=10;
d=10;
W=[];
delta=[];
for i=1:size(linkpair,1)
delta(i)=norm((unlinkpair(i,(m/2+1):end))-(linkpair(i,(m/2+1):end)));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%CVX%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Wt=rand(m,d);
cvx_begin
variables W(m,d) xi(n)
minimize( 0.5*norm( W , 'fro' )+ C*sum(xi));
subject to
2*sum((unlinkpair*W).*(unlinkpair*Wt),2) - sum((linkpair*W).*(linkpair*W),2) >= (B*delta)'-xi+ sum((unlinkpair*Wt).*(unlinkpair*Wt),2);
xi>=0;
cvx_end
%%%%%%%%%%%%%%%%%%%%prediction%%%%%%%%%%%%%
[prediction_LUS,prediction_Original,count_LUS,count_noLUS]=pred(W, test1, test2)
我在 Python 环境中的 cvxpy 中尝试了这个:
def ulink_cccp():
link_1,link_2,unlink_1,unlink_2,test1,test2=data_generate()
linkpair=np.column_stack((link_1,link_2*-1))
unlinkpair=np.column_stack((unlink_1,unlink_2*-1))
m=linkpair.shape[1] ##col
n=linkpair.shape[0] ##row
#####parameters###############
C=100
B=10
d=10
delta=[]
for i in range(n):
delta.append(np.linalg.norm(unlinkpair[i,m/2:]-linkpair[i,m/2:]))
Wt=np.random.rand(m,d)
print Wt.size
#####CVX_Problems##################
xi=Variable(n)
W=Variable(m,d)
print W.size
objective=Minimize(0.5*norm(W,'fro')+C*sum(xi))
constraints=[2*sum_entries(np.multiply(np.dot(unlinkpair*W),np.dot(unlinkpair,Wt)),axis=1)-\
sum_entries(np.multiply(np.dot(linkpair,W),np.dot(linkpair,W)),axis=1)>=\
(np.dot(B,delta)).T-xi+\
sum_entries(np.multiply(np.dot(unlinkpair,Wt),np.dot(unlinkpair,Wt)),axis=1),\
xi>=0]
prob=Problem(objective,constraints)
result=prob.solve(verbose=True)
但是约束变量有问题。也许 numpy 函数不起作用。似乎只有 cvxpy 语法有效。但是,我在 cvxpy 语法中找不到矩阵乘法。我怎样才能做到这一点?