[英]Python vs. C++. difference results
I have a python code from http://www.albertauyeung.com/mf.php , but I dont know anything about Python programming to improve and change this code. 我有一个来自http://www.albertauyeung.com/mf.php的python代码,但是我对Python编程没有任何了解,无法改进和更改此代码。 By anyway, I start write it again in C++ with Qt, but my result is so different.
无论如何,我开始用Qt用C ++再次编写它,但是结果却截然不同。 The Python code:
Python代码:
import numpy
def matrix_factorisation(R, P, Q, K, steps=5000, alpha=0.0002, beta=0.02):
Q = Q.T
for step in xrange(steps):
for i in xrange(len(R)):
for j in xrange(len(R[i])):
if R[i][j] > 0:
eij = R[i][j] - numpy.dot(P[i,:],Q[:,j])
for k in xrange(K):
P[i][k] = P[i][k] + alpha * (2 * eij * Q[k][j] - beta * P[i][k])
Q[k][j] = Q[k][j] + alpha * (2 * eij * P[i][k] - beta * Q[k][j])
eR = numpy.dot(P,Q)
e = 0
for i in xrange(len(R)):
for j in xrange(len(R[i])):
if R[i][j] > 0:
e = e + pow(R[i][j] - numpy.dot(P[i,:],Q[:,j]), 2)
for k in xrange(K):
e = e + (beta/2) * (pow(P[i][k],2) + pow(Q[k][j],2))
if e < 0.001:
break
return P, Q.T
if __name__ == "__main__":
R = [
[5,3,0,1],
[4,0,0,1],
[1,1,0,5],
[1,0,0,4],
[0,1,5,4],
]
R = numpy.array(R)
N = len(R)
M = len(R[0])
K = 2
P = numpy.random.rand(N,K)
Q = numpy.random.rand(M,K)
nP, nQ = matrix_factorisation(R, P, Q, K)
nR = numpy.dot(nP, nQ.T)
================================================================================= and C++ code: ================================================== ==============================和C ++代码:
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//=============preliminary=================
long double R[5][4]={5,3,0,1,
4,0,0,1,
1,1,0,5,
1,0,0,4,
0,1,5,4},newR[5][4]={0};
int n=5, m=4,k=2;
long double Q[2][4],P[5][2];
for (int i=0;i<k;i++){
for (int j=0;j<n;j++){
P[j][i]=random2(0,1);
}
for(int l=0;l<m;l++){
Q[i][l]=random2(0,1);
}
}
// ============= MatrixFactorization(R,P,Q,k)=======================
long double eij=0,sigmaPQ=0;
long double e;
long double alpha=0.0002, beta=0.02;
int t;
for(long step=0;step <5000;step++){
t=step;
for (int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(R[i][j]>0){
sigmaPQ=0;
for (int z=0;z<k;z++){
sigmaPQ += P[i][z]*Q[z][j];
}
eij=R[i][j]-sigmaPQ;
for (int z=0;z<k;z++){
P[i][z] += alpha*(2*eij*Q[z][j]-beta*P[i][z]);
Q[z][j] += alpha*(2*eij*P[i][z]-beta*Q[z][j]);
}
}
}
}
e=0;
for (int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(R[i][j]>0){
sigmaPQ=0;
for (int z=0;z<k;z++){
sigmaPQ += P[i][z]*Q[z][j];
}
for (int z=0;z<k;z++){
e+=(beta/2)*(qPow( P[i][z],2)+qPow(Q[z][j],2));
}
e=qSqrt(e);
}
}
}
if(e<0.001)
break;
}
//=========== calculate approximate R =============
long double temp;
for (int i=0;i<n;i++){
for(int j=0;j<m;j++){
temp=0;
for(int z=0;z<k;z++){
temp+=P[i][z]*Q[z][j];
}
newR[i][j]=temp;
}
}
}
the Python code is correct answer. Python代码是正确的答案。 what i should to do, to fix this C++ code?
我应该怎么做才能修复此C ++代码?
Break it down into steps and check outputs at each step. 将其分为多个步骤,并在每个步骤中检查输出。 print and printf are your friend.
print和printf是您的朋友。 :) Or perhaps learn enough of Python to create a pseudo code version which can be translated to C++.
:)或足够了解Python以创建可转换为C ++的伪代码版本。
Often times when debugging large code segments I just monitor the data at each logical step in the process. 通常,调试大型代码段时,我只是在过程中每个逻辑步骤都监视数据。 Python makes it easy by inserting print statements at key locations.
Python通过在关键位置插入打印语句来简化操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.