-
Notifications
You must be signed in to change notification settings - Fork 407
/
line.cpp
executable file
·465 lines (406 loc) · 13.6 KB
/
line.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
This is the tool ....
Contact Author: Jian Tang, Microsoft Research, [email protected], [email protected]
Publication: Jian Tang, Meng Qu, Mingzhe Wang, Ming Zhang, Jun Yan, Qiaozhu Mei. "LINE: Large-scale Information Network Embedding". In WWW 2015.
*/
// Format of the training file:
//
// The training file contains serveral lines, each line represents a DIRECTED edge in the network.
// More specifically, each line has the following format "<u> <v> <w>", meaning an edge from <u> to <v> with weight as <w>.
// <u> <v> and <w> are seperated by ' ' or '\t' (blank or tab)
// For UNDIRECTED edge, the user should use two DIRECTED edges to represent it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <pthread.h>
#include <gsl/gsl_rng.h>
#define MAX_STRING 100
#define SIGMOID_BOUND 6
#define NEG_SAMPLING_POWER 0.75
const int hash_table_size = 30000000;
const int neg_table_size = 1e8;
const int sigmoid_table_size = 1000;
typedef float real; // Precision of float numbers
struct ClassVertex {
double degree;
char *name;
};
char network_file[MAX_STRING], embedding_file[MAX_STRING];
struct ClassVertex *vertex;
int is_binary = 0, num_threads = 1, order = 2, dim = 100, num_negative = 5;
int *vertex_hash_table, *neg_table;
int max_num_vertices = 1000, num_vertices = 0;
long long total_samples = 1, current_sample_count = 0, num_edges = 0;
real init_rho = 0.025, rho;
real *emb_vertex, *emb_context, *sigmoid_table;
int *edge_source_id, *edge_target_id;
double *edge_weight;
// Parameters for edge sampling
long long *alias;
double *prob;
const gsl_rng_type * gsl_T;
gsl_rng * gsl_r;
/* Build a hash table, mapping each vertex name to a unique vertex id */
unsigned int Hash(char *key)
{
unsigned int seed = 131;
unsigned int hash = 0;
while (*key)
{
hash = hash * seed + (*key++);
}
return hash % hash_table_size;
}
void InitHashTable()
{
vertex_hash_table = (int *)malloc(hash_table_size * sizeof(int));
for (int k = 0; k != hash_table_size; k++) vertex_hash_table[k] = -1;
}
void InsertHashTable(char *key, int value)
{
int addr = Hash(key);
while (vertex_hash_table[addr] != -1) addr = (addr + 1) % hash_table_size;
vertex_hash_table[addr] = value;
}
int SearchHashTable(char *key)
{
int addr = Hash(key);
while (1)
{
if (vertex_hash_table[addr] == -1) return -1;
if (!strcmp(key, vertex[vertex_hash_table[addr]].name)) return vertex_hash_table[addr];
addr = (addr + 1) % hash_table_size;
}
return -1;
}
/* Add a vertex to the vertex set */
int AddVertex(char *name)
{
int length = strlen(name) + 1;
if (length > MAX_STRING) length = MAX_STRING;
vertex[num_vertices].name = (char *)calloc(length, sizeof(char));
strncpy(vertex[num_vertices].name, name, length-1);
vertex[num_vertices].degree = 0;
num_vertices++;
if (num_vertices + 2 >= max_num_vertices)
{
max_num_vertices += 1000;
vertex = (struct ClassVertex *)realloc(vertex, max_num_vertices * sizeof(struct ClassVertex));
}
InsertHashTable(name, num_vertices - 1);
return num_vertices - 1;
}
/* Read network from the training file */
void ReadData()
{
FILE *fin;
char name_v1[MAX_STRING], name_v2[MAX_STRING], str[2 * MAX_STRING + 10000];
int vid;
double weight;
fin = fopen(network_file, "rb");
if (fin == NULL)
{
printf("ERROR: network file not found!\n");
exit(1);
}
num_edges = 0;
while (fgets(str, sizeof(str), fin)) num_edges++;
fclose(fin);
printf("Number of edges: %lld \n", num_edges);
edge_source_id = (int *)malloc(num_edges*sizeof(int));
edge_target_id = (int *)malloc(num_edges*sizeof(int));
edge_weight = (double *)malloc(num_edges*sizeof(double));
if (edge_source_id == NULL || edge_target_id == NULL || edge_weight == NULL)
{
printf("Error: memory allocation failed!\n");
exit(1);
}
fin = fopen(network_file, "rb");
num_vertices = 0;
for (int k = 0; k != num_edges; k++)
{
fscanf(fin, "%s %s %lf", name_v1, name_v2, &weight);
if (k % 10000 == 0)
{
printf("Reading edges: %.3lf%%%c", k / (double)(num_edges + 1) * 100, 13);
fflush(stdout);
}
vid = SearchHashTable(name_v1);
if (vid == -1) vid = AddVertex(name_v1);
vertex[vid].degree += weight;
edge_source_id[k] = vid;
vid = SearchHashTable(name_v2);
if (vid == -1) vid = AddVertex(name_v2);
vertex[vid].degree += weight;
edge_target_id[k] = vid;
edge_weight[k] = weight;
}
fclose(fin);
printf("Number of vertices: %d \n", num_vertices);
}
/* The alias sampling algorithm, which is used to sample an edge in O(1) time. */
void InitAliasTable()
{
alias = (long long *)malloc(num_edges*sizeof(long long));
prob = (double *)malloc(num_edges*sizeof(double));
if (alias == NULL || prob == NULL)
{
printf("Error: memory allocation failed!\n");
exit(1);
}
double *norm_prob = (double*)malloc(num_edges*sizeof(double));
long long *large_block = (long long*)malloc(num_edges*sizeof(long long));
long long *small_block = (long long*)malloc(num_edges*sizeof(long long));
if (norm_prob == NULL || large_block == NULL || small_block == NULL)
{
printf("Error: memory allocation failed!\n");
exit(1);
}
double sum = 0;
long long cur_small_block, cur_large_block;
long long num_small_block = 0, num_large_block = 0;
for (long long k = 0; k != num_edges; k++) sum += edge_weight[k];
for (long long k = 0; k != num_edges; k++) norm_prob[k] = edge_weight[k] * num_edges / sum;
for (long long k = num_edges - 1; k >= 0; k--)
{
if (norm_prob[k]<1)
small_block[num_small_block++] = k;
else
large_block[num_large_block++] = k;
}
while (num_small_block && num_large_block)
{
cur_small_block = small_block[--num_small_block];
cur_large_block = large_block[--num_large_block];
prob[cur_small_block] = norm_prob[cur_small_block];
alias[cur_small_block] = cur_large_block;
norm_prob[cur_large_block] = norm_prob[cur_large_block] + norm_prob[cur_small_block] - 1;
if (norm_prob[cur_large_block] < 1)
small_block[num_small_block++] = cur_large_block;
else
large_block[num_large_block++] = cur_large_block;
}
while (num_large_block) prob[large_block[--num_large_block]] = 1;
while (num_small_block) prob[small_block[--num_small_block]] = 1;
free(norm_prob);
free(small_block);
free(large_block);
}
long long SampleAnEdge(double rand_value1, double rand_value2)
{
long long k = (long long)num_edges * rand_value1;
return rand_value2 < prob[k] ? k : alias[k];
}
/* Initialize the vertex embedding and the context embedding */
void InitVector()
{
long long a, b;
a = posix_memalign((void **)&emb_vertex, 128, (long long)num_vertices * dim * sizeof(real));
if (emb_vertex == NULL) { printf("Error: memory allocation failed\n"); exit(1); }
for (b = 0; b < dim; b++) for (a = 0; a < num_vertices; a++)
emb_vertex[a * dim + b] = (rand() / (real)RAND_MAX - 0.5) / dim;
a = posix_memalign((void **)&emb_context, 128, (long long)num_vertices * dim * sizeof(real));
if (emb_context == NULL) { printf("Error: memory allocation failed\n"); exit(1); }
for (b = 0; b < dim; b++) for (a = 0; a < num_vertices; a++)
emb_context[a * dim + b] = 0;
}
/* Sample negative vertex samples according to vertex degrees */
void InitNegTable()
{
double sum = 0, cur_sum = 0, por = 0;
int vid = 0;
neg_table = (int *)malloc(neg_table_size * sizeof(int));
for (int k = 0; k != num_vertices; k++) sum += pow(vertex[k].degree, NEG_SAMPLING_POWER);
for (int k = 0; k != neg_table_size; k++)
{
if ((double)(k + 1) / neg_table_size > por)
{
cur_sum += pow(vertex[vid].degree, NEG_SAMPLING_POWER);
por = cur_sum / sum;
vid++;
}
neg_table[k] = vid - 1;
}
}
/* Fastly compute sigmoid function */
void InitSigmoidTable()
{
real x;
sigmoid_table = (real *)malloc((sigmoid_table_size + 1) * sizeof(real));
for (int k = 0; k != sigmoid_table_size; k++)
{
x = 2.0 * SIGMOID_BOUND * k / sigmoid_table_size - SIGMOID_BOUND;
sigmoid_table[k] = 1 / (1 + exp(-x));
}
}
real FastSigmoid(real x)
{
if (x > SIGMOID_BOUND) return 1;
else if (x < -SIGMOID_BOUND) return 0;
int k = (x + SIGMOID_BOUND) * sigmoid_table_size / SIGMOID_BOUND / 2;
return sigmoid_table[k];
}
/* Fastly generate a random integer */
int Rand(unsigned long long &seed)
{
seed = seed * 25214903917 + 11;
return (seed >> 16) % neg_table_size;
}
/* Update embeddings */
void Update(real *vec_u, real *vec_v, real *vec_error, int label)
{
real x = 0, g;
for (int c = 0; c != dim; c++) x += vec_u[c] * vec_v[c];
g = (label - FastSigmoid(x)) * rho;
for (int c = 0; c != dim; c++) vec_error[c] += g * vec_v[c];
for (int c = 0; c != dim; c++) vec_v[c] += g * vec_u[c];
}
void *TrainLINEThread(void *id)
{
long long u, v, lu, lv, target, label;
long long count = 0, last_count = 0, curedge;
unsigned long long seed = (long long)id;
real *vec_error = (real *)calloc(dim, sizeof(real));
while (1)
{
//judge for exit
if (count > total_samples / num_threads + 2) break;
if (count - last_count > 10000)
{
current_sample_count += count - last_count;
last_count = count;
printf("%cRho: %f Progress: %.3lf%%", 13, rho, (real)current_sample_count / (real)(total_samples + 1) * 100);
fflush(stdout);
rho = init_rho * (1 - current_sample_count / (real)(total_samples + 1));
if (rho < init_rho * 0.0001) rho = init_rho * 0.0001;
}
curedge = SampleAnEdge(gsl_rng_uniform(gsl_r), gsl_rng_uniform(gsl_r));
u = edge_source_id[curedge];
v = edge_target_id[curedge];
lu = u * dim;
for (int c = 0; c != dim; c++) vec_error[c] = 0;
// NEGATIVE SAMPLING
for (int d = 0; d != num_negative + 1; d++)
{
if (d == 0)
{
target = v;
label = 1;
}
else
{
target = neg_table[Rand(seed)];
label = 0;
}
lv = target * dim;
if (order == 1) Update(&emb_vertex[lu], &emb_vertex[lv], vec_error, label);
if (order == 2) Update(&emb_vertex[lu], &emb_context[lv], vec_error, label);
}
for (int c = 0; c != dim; c++) emb_vertex[c + lu] += vec_error[c];
count++;
}
free(vec_error);
pthread_exit(NULL);
}
void Output()
{
FILE *fo = fopen(embedding_file, "wb");
fprintf(fo, "%d %d\n", num_vertices, dim);
for (int a = 0; a < num_vertices; a++)
{
fprintf(fo, "%s ", vertex[a].name);
if (is_binary) for (int b = 0; b < dim; b++) fwrite(&emb_vertex[a * dim + b], sizeof(real), 1, fo);
else for (int b = 0; b < dim; b++) fprintf(fo, "%lf ", emb_vertex[a * dim + b]);
fprintf(fo, "\n");
}
fclose(fo);
}
void TrainLINE() {
long a;
pthread_t *pt = (pthread_t *)malloc(num_threads * sizeof(pthread_t));
if (order != 1 && order != 2)
{
printf("Error: order should be either 1 or 2!\n");
exit(1);
}
printf("--------------------------------\n");
printf("Order: %d\n", order);
printf("Samples: %lldM\n", total_samples / 1000000);
printf("Negative: %d\n", num_negative);
printf("Dimension: %d\n", dim);
printf("Initial rho: %lf\n", init_rho);
printf("--------------------------------\n");
InitHashTable();
ReadData();
InitAliasTable();
InitVector();
InitNegTable();
InitSigmoidTable();
gsl_rng_env_setup();
gsl_T = gsl_rng_rand48;
gsl_r = gsl_rng_alloc(gsl_T);
gsl_rng_set(gsl_r, 314159265);
clock_t start = clock();
printf("--------------------------------\n");
for (a = 0; a < num_threads; a++) pthread_create(&pt[a], NULL, TrainLINEThread, (void *)a);
for (a = 0; a < num_threads; a++) pthread_join(pt[a], NULL);
printf("\n");
clock_t finish = clock();
printf("Total time: %lf\n", (double)(finish - start) / CLOCKS_PER_SEC);
Output();
}
int ArgPos(char *str, int argc, char **argv) {
int a;
for (a = 1; a < argc; a++) if (!strcmp(str, argv[a])) {
if (a == argc - 1) {
printf("Argument missing for %s\n", str);
exit(1);
}
return a;
}
return -1;
}
int main(int argc, char **argv) {
int i;
if (argc == 1) {
printf("LINE: Large Information Network Embedding\n\n");
printf("Options:\n");
printf("Parameters for training:\n");
printf("\t-train <file>\n");
printf("\t\tUse network data from <file> to train the model\n");
printf("\t-output <file>\n");
printf("\t\tUse <file> to save the learnt embeddings\n");
printf("\t-binary <int>\n");
printf("\t\tSave the learnt embeddings in binary moded; default is 0 (off)\n");
printf("\t-size <int>\n");
printf("\t\tSet dimension of vertex embeddings; default is 100\n");
printf("\t-order <int>\n");
printf("\t\tThe type of the model; 1 for first order, 2 for second order; default is 2\n");
printf("\t-negative <int>\n");
printf("\t\tNumber of negative examples; default is 5\n");
printf("\t-samples <int>\n");
printf("\t\tSet the number of training samples as <int>Million; default is 1\n");
printf("\t-threads <int>\n");
printf("\t\tUse <int> threads (default 1)\n");
printf("\t-rho <float>\n");
printf("\t\tSet the starting learning rate; default is 0.025\n");
printf("\nExamples:\n");
printf("./line -train net.txt -output vec.txt -binary 1 -size 200 -order 2 -negative 5 -samples 100 -rho 0.025 -threads 20\n\n");
return 0;
}
if ((i = ArgPos((char *)"-train", argc, argv)) > 0) strcpy(network_file, argv[i + 1]);
if ((i = ArgPos((char *)"-output", argc, argv)) > 0) strcpy(embedding_file, argv[i + 1]);
if ((i = ArgPos((char *)"-binary", argc, argv)) > 0) is_binary = atoi(argv[i + 1]);
if ((i = ArgPos((char *)"-size", argc, argv)) > 0) dim = atoi(argv[i + 1]);
if ((i = ArgPos((char *)"-order", argc, argv)) > 0) order = atoi(argv[i + 1]);
if ((i = ArgPos((char *)"-negative", argc, argv)) > 0) num_negative = atoi(argv[i + 1]);
if ((i = ArgPos((char *)"-samples", argc, argv)) > 0) total_samples = atoi(argv[i + 1]);
if ((i = ArgPos((char *)"-rho", argc, argv)) > 0) init_rho = atof(argv[i + 1]);
if ((i = ArgPos((char *)"-threads", argc, argv)) > 0) num_threads = atoi(argv[i + 1]);
total_samples *= 1000000;
rho = init_rho;
vertex = (struct ClassVertex *)calloc(max_num_vertices, sizeof(struct ClassVertex));
TrainLINE();
return 0;
}