-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest.cpp
169 lines (137 loc) · 5.25 KB
/
test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "test.h"
#include "typedef.h"
#include "util.h"
extern std::vector<hiddenNode> hiddenLayerParty1, hiddenLayerParty2;
extern std::vector<outputNode> outputLayerParty1, outputLayerParty2;
extern std::vector<inputNode> inputLayerParty1, inputLayerParty2;
extern vector<int> indexTrainSet;
void readModel(int flag) {
string file1,file2;
if (flag==0) {
file1="../cora/modelParty1.txt";
file2="../cora/modelParty2.txt";
}else if(flag==1){
file1="../citeseer/modelParty1.txt";
file2="../citeseer/modelParty2.txt";
}else{
file1="../pubmed/modelParty1.txt";
file2="../pubmed/modelParty2.txt";
}
ifstream infile(file1, ios::in);
if (!infile) { // 判断文件是否存在
cerr << "open error." << endl;
exit(1); // 退出程序
}
string str; // 定义字符数组用来接受读取一行的数据
int weight;
for (int i = 0; i < HIDDENNODE; ++i) {
getline(infile, str);
stringstream input(str);
hiddenNode newNode;
for (int j = 0; j < FEATURENUM; ++j) {
input >> weight;
newNode.weights.push_back(weight);
}
hiddenLayerParty1.push_back(newNode);
}
for (int i = 0; i < OUTNODE; ++i) {
getline(infile, str);
stringstream input(str);
outputNode newNode;
for (int j = 0; j < HIDDENNODE; ++j) {
input >> weight;
newNode.weights.push_back(weight);
}
outputLayerParty1.push_back(newNode);
}
infile.close();
ifstream infile2(file2, ios::in);
if (!infile) { // 判断文件是否存在
cerr << "open error." << endl;
exit(1); // 退出程序
}
for (int i = 0; i < HIDDENNODE; ++i) {
getline(infile2, str);
stringstream input(str);
hiddenNode newNode;
for (int j = 0; j < FEATURENUM; ++j) {
input >> weight;
newNode.weights.push_back(weight);
}
hiddenLayerParty2.push_back(newNode);
}
for (int i = 0; i < OUTNODE; ++i) {
getline(infile2, str);
stringstream input(str);
outputNode newNode;
for (int j = 0; j < HIDDENNODE; ++j) {
input >> weight;
newNode.weights.push_back(weight);
}
outputLayerParty2.push_back(newNode);
}
infile2.close();
}
bool test(int index) {
long long degree[2], res[2], degreeReci[2], degreeNeib[2];
mpcReci(inputLayerParty1[index].neibNum, inputLayerParty2[index].neibNum, degree);
for (int j = 0; j < HIDDENNODE; ++j) {
mpcMulti(degree[0], inputLayerParty1[index].embeddingFirstLayer[j],
degree[1], inputLayerParty2[index].embeddingFirstLayer[j], res);
inputLayerParty1[index].embeddingSecond[j] = res[0];
inputLayerParty2[index].embeddingSecond[j] = res[1];
}
mpcSquareRoot(inputLayerParty1[index].neibNum, inputLayerParty2[index].neibNum, degree);
for (int j = 0; j < inputLayerParty1[index].neibors.size(); ++j) {
int neibId = inputLayerParty1[index].neibors[j] + inputLayerParty2[index].neibors[j];
mpcSquareRoot(inputLayerParty1[neibId].neibNum,
inputLayerParty2[neibId].neibNum, degreeNeib);
mpcMulti(degree[0], degreeNeib[0], degree[1], degreeNeib[1], degreeReci);
for (int k = 0; k < HIDDENNODE; ++k) {
mpcMulti(degreeReci[0], inputLayerParty1[neibId].embeddingFirstLayer[k],
degreeReci[1], inputLayerParty2[neibId].embeddingFirstLayer[k], res);
mpcMulti(res[0], inputLayerParty1[index].edgeWeight[j],
res[1], inputLayerParty2[index].edgeWeight[j], res);
inputLayerParty1[index].embeddingSecond[k] += res[0];
inputLayerParty2[index].embeddingSecond[k] += res[1];
}
}
for (int j = 0; j < OUTNODE; ++j) {
long long res[2][HIDDENNODE];
mpcMulti(inputLayerParty1[index].embeddingSecond, outputLayerParty1[j].weights,
inputLayerParty2[index].embeddingSecond, outputLayerParty2[j].weights, *res);
inputLayerParty1[index].embeddingSecondLayer[j] = 0;
inputLayerParty2[index].embeddingSecondLayer[j] = 0;
for (int k = 0; k < HIDDENNODE; ++k) {
inputLayerParty1[index].embeddingSecondLayer[j] += res[0][k];
inputLayerParty2[index].embeddingSecondLayer[j] += res[1][k];
}
}
sofMax(index);
int loc = 0;
long long temp = 0;
for (int i = 0; i < OUTNODE; ++i) {
if (inputLayerParty1[index].embeddingSecondLayer[i] + inputLayerParty2[index].embeddingSecondLayer[i] > temp) {
loc = i;
temp = inputLayerParty1[index].embeddingSecondLayer[i] + inputLayerParty2[index].embeddingSecondLayer[i];
}
}
return inputLayerParty1[index].trueValue[loc] + inputLayerParty2[index].trueValue[loc] == 1;
}
double testData() {
forwardFirstLayer();
double rightNum = 0,num=0;
for (int i = 0; i < NODENUM; ++i) {
if (num>=1000){
break;
}
if (std::find(indexTrainSet.begin(), indexTrainSet.end(), i) != indexTrainSet.end()) {
continue;
}
if (test(i)) {
rightNum++;
}
num++;
}
return rightNum * 1.0 / 1000;
}