-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
136 lines (119 loc) · 3.9 KB
/
main.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
#include "cluster.hpp"
#include "util.hpp"
#include "string.h"
#include <time.h>
using namespace std;
/** Code to run the experiment and save the output in a nice readable format. */
void experiment(graphData* gd,
int K, // number of circles
int lambda, // regularization parameter
int reps, // number of iterations of training
int gradientReps, // number of iterations of gradient ascent
int improveReps, // number of iterations using by QPBO
char* resName // Where to save the results
)
{
long starttime = clock();
printf("Results will be saved to %s\n", resName);
Cluster c(gd);
Scalar bestll = 0;
int bestseed = 0;
vector<set<int> > bestClusters;
Scalar* bestTheta = new Scalar [K*c.gd->nEdgeFeatures];
Scalar* bestAlpha = new Scalar [K*c.gd->nEdgeFeatures];
// Number of random restarts
int nseeds = 5;
for (int seed = 0; seed < nseeds; seed ++)
{
printf("Random restart %d of %d\n", seed + 1, nseeds);
c.train(K, reps, gradientReps, improveReps, lambda, seed, SYMMETRICDIFF);
Scalar ll = c.loglikelihood(c.theta, c.alpha, c.chat);
if (ll > bestll or bestll == 0)
{
bestll = ll;
bestseed = seed;
bestClusters = c.chat;
memcpy(bestTheta, c.theta, K*c.gd->nEdgeFeatures*sizeof(Scalar));
memcpy(bestAlpha, c.alpha, K*sizeof(Scalar));
}
}
FILE* f = fopen(resName, "w");
long endtime = clock();
fprintf(f, "seed = %d\n", bestseed);
fprintf(f, "ll = %f\n", bestll);
fprintf(f, "loss_zeroone = %f\n", totalLoss(c.gd->clusters, bestClusters, c.gd->nNodes, ZEROONE));
fprintf(f, "loss_symmetric = %f\n", totalLoss(c.gd->clusters, bestClusters, c.gd->nNodes, SYMMETRICDIFF));
fprintf(f, "fscore = %f\n", 1 - totalLoss(c.gd->clusters, bestClusters, c.gd->nNodes, FSCORE));
fprintf(f, "clusters = [");
for (vector<set<int> >::iterator it = bestClusters.begin(); it != bestClusters.end(); it ++)
{
if (it != bestClusters.begin()) fprintf(f, ",");
fprintf(f, "[");
for (set<int>::iterator it2 = it->begin(); it2 != it->end(); it2 ++)
{
if (it2 != it->begin()) fprintf(f, ",");
fprintf(f, "%s", gd->indexNode[*it2].c_str());
}
fprintf(f, "]");
}
fprintf(f, "]\n");
fprintf(f, "theta = [");
for (int k = 0; k < K; k ++)
{
if (k != 0) fprintf(f, ",");
fprintf(f, "[");
for (int i = 0; i < c.gd->nEdgeFeatures; i ++)
{
if (i != 0) fprintf(f, ",");
fprintf(f, "%f", bestTheta[k*c.gd->nEdgeFeatures + i]);
}
fprintf(f, "]");
}
fprintf(f, "]\n");
fprintf(f, "alpha = [");
for (int k = 0; k < K; k ++)
{
if (k != 0) fprintf(f, ",");
fprintf(f, "%f", bestAlpha[k]);
}
fprintf(f, "]\n");
fprintf(f, "runtime = %f\n", ((float) (endtime - starttime)) / CLOCKS_PER_SEC);
fclose(f);
delete [] bestTheta;
delete [] bestAlpha;
}
int main(int argc, char** argv)
{
int K;
if (argc < 3)
{
printf("Expected atleast 2 arguments (userid, output path, <optional> No_of_circles), e.g.\n");
printf("%s facebook/698 results.out 3\n", argv[0]);
exit(1);
}
if (argc < 4){
K = 3;
}else{
K = atoi(argv[3]);
}
char* nodeFeatureFile = new char [1000];
char* selfFeatureFile = new char [1000];
char* clusterFile = new char [1000];
char* edgeFile = new char [1000];
sprintf(nodeFeatureFile, "%s.feat", argv[1]);
sprintf(selfFeatureFile, "%s.egofeat", argv[1]);
sprintf(clusterFile, "%s.circles", argv[1]);
sprintf(edgeFile, "%s.edges", argv[1]);
graphData gd(nodeFeatureFile, selfFeatureFile, clusterFile, edgeFile, FRIENDFEATURES, 0);
experiment(&gd,
K, // K
1, // lambda
25, // training iterations
50, // gradient reps
5, // QPBO reps
argv[2]);
delete [] nodeFeatureFile;
delete [] selfFeatureFile;
delete [] clusterFile;
delete [] edgeFile;
}