这是神经网络上反向传播算法代码的一部分。
在我们的例子中,我们想要并行化 for( pt=0; pt< N_PT_pair; pt++) 循环,for(epoch=0; epoch< MaxEpoch; epoch++) 不能并行化。
initialize W1[ ] [ ] and W2[ ][ ] with random values
for(epoch=0; epoch<MaxEpoch; epoch++)
dW1[ ][ ]=0.0; dW2[ ][ ]=0.0; //sum of weight corrections
sse = 0; // Sum of square of errors
for( pt=0; pt<N_PT_pair; pt++)
input = pattern[pt];
compute output // procedure as above
compare target[pt] and output and
compute dW2[ ][ ] += ... // procedure to be described
compute dW1[ ][ ] += ... // procedure to be described
for(k=1; k<=Noutput; k++)
sse+=pow((target[pt][k]-output[k]),2);
end pt for loop
cout << "mean square error" << sse/N_PT_pair;
W1[ ][ ] += rate*dW1[ ][ ]
W2[ ][ ] += rate*dW2[ ][ ]
end epoch for loop
这些是分配和释放数组的代码
double** allocate_matrix(int rows,int cols)
{
double **a;
a = new double*[rows];
if(a==NULL){cout<<"matrix allocation failed"<<endl;exit(-1);}
for (int j=0;j<rows;j++){
a[j] = new double[cols];
if(a[j]==NULL) {cout<<"matrix allocation failed"<<endl;exit(-1);}
}
return a;
}
int deallocate_matrix(double**a,int rows)
{
for(int i=0;i<rows;i++)
delete [] a[i];
delete [ ] a;
return 0;
}
你能帮我们并行化代码吗?