-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtimlinucb.py
1321 lines (1157 loc) · 48.5 KB
/
timlinucb.py
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/local/bin/python3
import logging
import pandas as pd
import numpy as np
import sys
from tqdm import tqdm
from subprocess import Popen, PIPE
import os
import matplotlib.pyplot as plt
import random
import shutil
import stat
from functools import partial
from helpers import run_ic_eff, tim, tim_parallel, tqdm_joblib, _run_timlinucb_parallel
import joblib
# --------------------------------------------------------------------------------------
# %% ------------------------------ Initial setup --------------------------------------
# --------------------------------------------------------------------------------------
# Fancy plots in matplotlib and pandas
random.seed(42)
plt.style.use("ggplot")
# Setting up logging
VERBOSE = False
LOGGING_FMT = (
"%(levelname)s | %(asctime)s | line %(lineno)s | %(funcName)s | %(message)s"
)
LOGGING_DATEFMT = "%H:%M:%S"
logger_tlu = logging.getLogger("logger_tlu")
syslog = logging.StreamHandler()
formatter = logging.Formatter(fmt=LOGGING_FMT, datefmt=LOGGING_DATEFMT)
syslog.setFormatter(formatter)
logger_tlu.addHandler(syslog)
if VERBOSE:
logger_tlu.setLevel(logging.DEBUG)
else:
logger_tlu.setLevel(logging.WARNING)
# --------------------------------------------------------------------------------------
# %% -------------------- Generating edge features (preprocessing) ---------------------
# --------------------------------------------------------------------------------------
def get_features_nodes(
df_graph,
dims=20,
epochs=1,
node2vec_path="node2vec",
tempdir_name="temp_dir",
dataset_name="facebook",
check_existing=True,
):
""" Generate node2vec features for the nodes in a graph
Parameters
----------
df_graph : pandas.DataFrame
The graph we run node2vec on, in the form of a DataFrame. A row represents
one edge in the graph, with columns containing the source and target nodes for
the edge.
dims : int, optional
Specifies the number of dimensions to pass to node2vec[1]. Default: 20
epochs : int, optional
Specifies the number of epochs to run the node2vec for. Default: 1
node2vec_path : str, optional
Path to the node2vec executable. Default: "node2vec", which means that it is
in one folder with this file
tempdir_name : str, optional
The name of a temporary directory to use for running node2vec.
Default: "temp_dir"
dataset_name : str, optional
The name of the dataset, used when naming the files in the temporary directory.
Default: "facebook"
check_existing : boolean, optional
Determines if we are checking for an alredy existing algorithm output in the
temporary directory.
Returns
-------
df_emb : pd.DataFrame
A dataframe with the contents of embeddings generated by node2vec. It is also
available as <dataset_name>-d<dims>.emb in the temporary directory.
References
----------
.. [1] Grover, Aditya, and Jure Leskovec. "node2vec: Scalable feature learning for networks."
Proceedings of the 22nd ACM SIGKDD international conference on
Knowledge discovery and data mining. 2016.
"""
if not os.path.exists(tempdir_name):
os.makedirs(tempdir_name)
FNAME_IN = os.path.join(tempdir_name, f"{dataset_name}.edgelist")
FNAME_OUT = os.path.join(tempdir_name, f"{dataset_name}-d{dims}.emb")
# Checking if we already ran the function before
if check_existing and os.path.exists(FNAME_OUT):
return pd.read_csv(
FNAME_OUT,
sep=" ",
names=(["node"] + [f"feat_{i}" for i in range(dims)]),
skiprows=1,
)
# Saving the edgelist to run node2vec on
df_graph.to_csv(
FNAME_IN, index=False, sep=" ", header=False, columns=["source", "target"]
)
# Preparing to run node2vec
process = Popen(
[
os.path.join(".", node2vec_path, "node2vec"),
f"-i:{FNAME_IN}",
f"-o:{FNAME_OUT}",
f"-d:{dims}",
f"-e:{epochs}",
"-v",
],
stdout=PIPE,
stderr=PIPE,
)
(output, err) = process.communicate()
_ = process.wait() # Returns exit code
out = output.decode("utf-8")
logger_tlu.debug(f"Running node2vec, {out}")
return pd.read_csv(
FNAME_OUT,
sep=" ",
names=(["node"] + [f"feat_{i}" for i in range(dims)]),
skiprows=1,
)
def generate_node2vec_fetures(
df,
node2vec_path="node2vec",
tempdir_name="temp_dir",
dataset_name="facebook",
num_features=20,
check_existing=True,
):
""" Generate node2vec features for the edges in a graph
Runs get_features_nodes to get the node2vec[1] features for the nodes and then
multiplies them to get the edge features.
Edge feature vector Fe = source_node_feats * target_node_feats.
Parameters
----------
df : pandas.DataFrame
The graph we run node2vec on, in the form of a DataFrame. A row represents one
edge in the graph, with columns containing the source and target nodes for
the edge.
node2vec_path : str, optional
Path to the node2vec executable. Default: "os.getcwd()", which means that it is
in one folder with this
tempdir_name : str, optional
The name of a temporary directory to use for running node2vec.
Default: "temp_dir"
dataset_name : str, optional
The name of the dataset, used when naming the files in the temporary directory.
Default: "facebook"
dims : int, optional
Specifies the number of dimensions to pass to node2vec[1]. Default: 20
check_existing : boolean, optional
Determines if we are checking for an alredy existing algorithm output in the
temporary directory.
Returns
-------
df_feats : pd.DataFrame
A dataframe with edge embeddings, where one row represents one edge.
References
----------
.. [1] Grover, Aditya, and Jure Leskovec.
"node2vec: Scalable feature learning for networks."
Proceedings of the 22nd ACM SIGKDD international conference on
Knowledge discovery and data mining. 2016.
"""
# Checking if we did this before
FNAME_SAVE = os.path.join(tempdir_name, f"{dataset_name}-d{num_features}-edges.emb")
if check_existing and os.path.exists(FNAME_SAVE):
df_ret = pd.read_csv(FNAME_SAVE)
df_ret[df_ret.columns[0]] = df_ret[df_ret.columns[0]].astype(int)
return df_ret.set_index(df_ret.columns[0])
# Getting node embeddings
logger_tlu.debug("Getting node embeddings...")
df_emb = get_features_nodes(
df,
dims=num_features,
node2vec_path=node2vec_path,
tempdir_name=tempdir_name,
dataset_name=dataset_name,
check_existing=check_existing,
)
df_emb = df_emb.set_index("node").sort_values(by="node")
# Generating edge features
logger_tlu.debug(f"Generating {num_features} edge features...")
df_feats = []
for row in tqdm(df.itertuples(), total=df.shape[0]):
df_feats.append(df_emb.loc[row.source].values * df_emb.loc[row.target].values)
df_feats = pd.DataFrame(df_feats)
# Saving the results
logger_tlu.debug(f"Saving the edge features to {FNAME_SAVE}")
df_feats.to_csv(FNAME_SAVE)
return df_feats
# --------------------------------------------------------------------------------------
# %% --------------------------------- Online IM --------------------------------------
# --------------------------------------------------------------------------------------
def _oim_node2vec_test(
df,
df_feats,
num_inf=10,
sigma=4,
c=0.1,
epsilon=0.4,
num_repeats=30,
num_repeats_regret_algo=20,
num_repeats_regret_true=20,
num_nodes_tim=-1,
oracle=tim,
):
""" Test of the Online IM with Node2Vec features
Online Influence Maximization using IMLinUCB [1] with node2vec [3] features. The
"test" version also compares the result obtained to the "true" seed set, generated
by an Offline IM algorithm with the actual activation probabilities. This uses the
node2vec node embeddings to speed up the processing.
Parameters
----------
df : pandas.DataFrame
The graph we run the OIM on, in the form of a DataFrame. A row represents one
edge in the graph, with columns being named "source", "target", and "probab".
"probab" column is the "true" activation probability.
df_feats : pandas.DataFrame
A dataframe with node embeddings generated by node2vec [3]. Should have n rows,
where n is the number of nodes in the graph. The number of columns is specified
when running node2vec on the graph. The smaller the number of columns is the
more uncertain the embedding, but it also speeds up processing.
num_inf : int, optional
Number of seed nodes to find. Default: 10.
sigma : float, optional
A parameter used by the IMLinUCB algorithm. Refer to the IMLinUCB paper for
more details. [1] Default: 4
c: float, optional
A parameter used by the IMLinUCB algorithm. Refer to the IMLinUCB paper for
more details. [1] Default: 0.1
epsilon : float, optional
A parameter used by the TIM algorithm. Refer to the TIM paper for
more details. [2] Default: 0.4
num_repeats : int, optional
Number of iterations of the IMLinUCB algorithm. The more, the better the
results. Default: 30
num_repeats_regret_algo : int, optional
Number of times to run IC at every IMLinUCB step. A larger number might
improve the results. Default: 20
num_repeats_regret_true : int, optional
Number of times to run the IC for the "true" seed set. A larger number will
provide a more accurate picture of the comparison. Default: 20
num_nodes_tim : int, optional
Number of nodes to use for the offline IM algorithm, TIM[2]. Default: -1
oracle : function, optional
A function to use as the Offline IM algorithm. Default: tim [2]
Returns
-------
return_dict : dict
A dictionary consisting of following keys:
- rewards, a list of all rewards (number of inf nodes) obtained by the algorithm
- rewards_edges, a list of edge rewards (number of inf edges) obtained
- s_best, the list of the selected seed nodes
- u_e_best, the approximated probabilities
- reward_best, the reward obtained by running IC with s_best
References
----------
.. [1] Wen, Zheng, Branislav Kveton, and Michal Valko.
"Influence maximization with semi-bandit feedback." CoRR, abs/1605.06593 (2016).
.. [2] Tang, Youze, Xiaokui Xiao, and Yanchen Shi.
"Influence maximization: Near-optimal time complexity meets practical efficiency."
Proceedings of the 2014 ACM SIGMOD international conference on Management of data. 2014.
.. [3] Grover, Aditya, and Jure Leskovec.
"node2vec: Scalable feature learning for networks."
Proceedings of the 22nd ACM SIGKDD international conference on
Knowledge discovery and data mining. 2016.
"""
logger_tlu.debug("Started Online Influence Maximization...")
logger_tlu.debug("Setting parameters")
num_feats = df_feats.shape[1]
num_edges_t = df.shape[0]
# "True" probabilities - effectively our test set
true_weights = df["probab"].copy()
# Using nodes_t[-1] because TIM wants the max node id
s_true = sorted(
oracle(
df[["source", "target", "probab"]],
num_nodes_tim,
num_edges_t,
num_inf,
epsilon,
)
)
# Gathering the stats for the "true" seed set
all_true_nodes = []
all_true_edges = []
all_true_obs = []
for k in range(num_repeats_regret_true):
true_act_nodes, true_act_edges, true_obs_edges = run_ic_eff(df, s_true)
all_true_nodes.append(true_act_nodes)
all_true_edges.append(true_act_edges)
all_true_obs.append(true_obs_edges)
# Means for nodes and activated edges
mean_true_nodes = np.mean([len(i) for i in all_true_nodes])
mean_true_edges = np.mean([len(i) for i in all_true_edges])
mean_true_obs = np.mean([len(i) for i in all_true_obs])
# b, M_inv - used by IMLinUCB
b = np.zeros((num_feats, 1))
m_inv = np.eye(num_feats, num_feats)
# Returning these
s_best = []
reward_best = 0
u_e_best = []
regrets = []
regrets_edges = []
for iter_oim in tqdm(
range(num_repeats),
desc=f"OIM iters {num_edges_t} edges",
leave=False,
file=sys.stderr,
):
# ---- Step 1 - Calculating the u_e ----
theta = (m_inv @ b) / (sigma * sigma)
# xMx = (df_feats.values @ m_inv @ df_feats.T.values).clip(min=0)
u_e = []
for i in range(num_edges_t):
x_e = df_feats.loc[i].values
xMx = x_e @ m_inv @ x_e.T # .clip(min=0)
u_e.append(np.clip(x_e @ theta + c * np.sqrt(xMx), 0, 1))
# u_e.append(expit(x_e @ theta + c * np.sqrt(xMx)))
u_e = np.array(u_e)
# ---- Step 2 - Evaluating the performance ----
# Loss function
df["probab"] = u_e
s_oracle = sorted(
oracle(
df[["source", "target", "probab"]],
num_nodes_tim,
num_edges_t,
num_inf,
epsilon,
)
)
# Observing edge-level feedback
df["probab"] = true_weights
all_algo_nodes = []
all_algo_edges = []
all_algo_obs = []
for k in range(num_repeats_regret_algo):
algo_act_nodes, algo_act_edges, algo_obs_edges = run_ic_eff(df, s_oracle)
all_algo_nodes.append(algo_act_nodes)
all_algo_edges.append(algo_act_edges)
all_algo_obs.append(algo_obs_edges)
# Mean node counts
mean_algo_nodes = np.mean([len(i) for i in all_algo_nodes])
# Mean activated edge counts
mean_algo_edges = np.mean([len(i) for i in all_algo_edges])
mean_algo_obs = np.mean([len(i) for i in all_algo_obs])
# Used for updating M and b later
all_algo_edges = np.unique(np.concatenate(all_algo_edges))
all_algo_obs = np.unique(np.concatenate(all_algo_obs))
regrets.append(mean_true_nodes - mean_algo_nodes)
regrets_edges.append(mean_true_edges - mean_algo_edges)
logger_tlu.debug(f"True seeds: {s_true}")
logger_tlu.debug(f"Algo seeds: {s_oracle}")
logger_tlu.debug(
"Diff between true and algo seeds: "
f"{len(np.setdiff1d(s_true, s_oracle))}"
)
logger_tlu.debug(f"True reward: {mean_true_nodes}")
logger_tlu.debug(f"Algo reward: {mean_algo_nodes}")
logger_tlu.debug(f"Best algo reward: {reward_best}")
logger_tlu.debug(f"Regrets: {regrets}")
logger_tlu.debug(f"Edge regrets: {regrets_edges}")
logger_tlu.debug(f"Observed diff: {mean_true_obs - mean_algo_obs}")
logger_tlu.debug(f"Algo weights {u_e[80:90]}".replace("\n", ""))
logger_tlu.debug(f"Real weights {true_weights[80:90]}".replace("\n", ""))
if mean_algo_nodes > reward_best:
reward_best = mean_algo_nodes
s_best = s_oracle
u_e_best = u_e
if mean_algo_nodes > mean_true_nodes:
logger_tlu.debug(
"The algorithm has achieved better reward than the true seed node set."
)
logger_tlu.debug("Stopping learning.")
logger_tlu.debug(f"Best algo seed node set: {s_best}")
return_dict = {
"regrets": regrets,
"regrets_edges": regrets_edges,
"s_true": s_true,
"s_best": s_best,
"u_e_best": u_e_best,
"reward_best": reward_best,
}
logger_tlu.debug(f"Returning {return_dict}")
return return_dict
# ---- Step 3 - Calculating updates ----
for i in all_algo_obs:
x_e = np.array([df_feats.loc[i].values])
m_inv -= (m_inv @ x_e.T @ x_e @ m_inv) / (
x_e @ m_inv @ x_e.T + sigma * sigma
)
b += x_e.T * int(i in all_algo_edges)
return_dict = {
"regrets": regrets,
"regrets_edges": regrets_edges,
"s_true": s_true,
"s_best": s_best,
"u_e_best": u_e_best,
"reward_best": reward_best,
}
logger_tlu.debug("The algorithm has finished running.")
logger_tlu.debug(f"Returning: {return_dict}")
return return_dict
def oim_node2vec_simple(
df,
df_feats,
num_inf=10,
sigma=4,
c=0.1,
epsilon=0.4,
num_repeats=15,
num_nodes_tim=-1,
oracle=tim,
):
""" Online IM with Node2Vec features (Simple version)
Online Influence Maximization using IMLinUCB[1] with node2vec [3] features. Simple
version doesn't simulate the propagation multiple times at every time step, which
makes the algorithm faster but less accurate.
Parameters
----------
df : pandas.DataFrame
The graph we run the OIM on, in the form of a DataFrame. A row represents one
edge in the graph, with columns being named "source", "target", and "probab".
"probab" column is the "true" activation probability.
df_feats : pandas.DataFrame
A dataframe with node embeddings generated by node2vec [3]. Should have n rows,
where n is the number of nodes in the graph. The number of columns is specified
when running node2vec on the graph. The smaller the number of columns is the
more uncertain the embedding, but it also speeds up processing.
num_inf : int, optional
Number of seed nodes to find. Default: 10.
sigma : float, optional
A parameter used by the IMLinUCB algorithm. Refer to the IMLinUCB paper for
more details. [1] Default: 4
c: float, optional
A parameter used by the IMLinUCB algorithm. Refer to the IMLinUCB paper for
more details. [1] Default: 0.1
epsilon : float, optional
A parameter used by the TIM algorithm. Refer to the TIM paper for
more details. [2] Default: 0.4
num_repeats : int, optional
Number of iterations of the IMLinUCB algorithm. The more, the better the
results. Default: 30
num_nodes_tim : int, optional
Number of nodes to use for the offline IM algorithm, TIM [2]. Default: -1
oracle : function, optional
A function to use as the Offline IM algorithm. Default: tim [2]
Returns
-------
return_dict : dict
A dictionary consisting of following keys:
- rewards, a list of all rewards (number of inf nodes) obtained by the algorithm
- rewards_edges, a list of edge rewards (number of inf edges) obtained
- s_best, the list of the selected seed nodes
- u_e_best, the approximated probabilities
- reward_best, the reward obtained by running IC with s_best
References
----------
.. [1] Wen, Zheng, Branislav Kveton, and Michal Valko.
"Influence maximization with semi-bandit feedback." CoRR, abs/1605.06593 (2016).
.. [2] Tang, Youze, Xiaokui Xiao, and Yanchen Shi.
"Influence maximization: Near-optimal time complexity meets practical efficiency."
Proceedings of the 2014 ACM SIGMOD international conference on Management of data. 2014.
.. [3] Grover, Aditya, and Jure Leskovec.
"node2vec: Scalable feature learning for networks."
Proceedings of the 22nd ACM SIGKDD international conference on
Knowledge discovery and data mining. 2016.
"""
logger_tlu.debug("Started Online Influence Maximization...")
logger_tlu.debug("Setting parameters")
num_feats = df_feats.shape[1]
num_edges_t = df.shape[0]
# "True" probabilities - effectively our test set
true_weights = df["probab"].copy()
# b, M_inv - used by IMLinUCB
b = np.zeros((num_feats, 1))
m_inv = np.eye(num_feats, num_feats)
# Returning these
s_best = []
reward_best = 0
u_e_best = []
rewards = []
rewards_edges = []
for iter_oim in tqdm(
range(num_repeats),
desc=f"OIM iters {num_edges_t} edges",
leave=False,
file=sys.stderr,
):
# ---- Step 1 - Calculating the u_e ----
theta = (m_inv @ b) / (sigma * sigma)
# xMx = (df_feats.values @ m_inv @ df_feats.T.values).clip(min=0)
u_e = []
for i in range(num_edges_t):
x_e = df_feats.loc[i].values
xMx = x_e @ m_inv @ x_e.T # .clip(min=0)
u_e.append(np.clip(x_e @ theta + c * np.sqrt(xMx), 0, 1))
# u_e.append(expit(x_e @ theta + c * np.sqrt(xMx)))
u_e = np.array(u_e)
# ---- Step 2 - Evaluating the performance ----
# Loss function
df["probab"] = u_e
s_oracle = sorted(
oracle(
df[["source", "target", "probab"]],
num_nodes_tim,
num_edges_t,
num_inf,
epsilon,
)
)
# Observing edge-level feedback
df["probab"] = true_weights
algo_act_nodes, algo_act_edges, algo_obs_edges = run_ic_eff(df, s_oracle)
algo_num_nodes = len(algo_act_nodes)
algo_num_edges = len(algo_act_edges)
rewards.append(algo_num_nodes)
rewards_edges.append(algo_num_edges)
logger_tlu.debug(f"Algo seeds: {s_oracle}")
logger_tlu.debug(f"Algo reward: {algo_num_nodes}")
logger_tlu.debug(f"Best algo reward: {reward_best}")
logger_tlu.debug(f"Rewards: {rewards}")
logger_tlu.debug(f"Edge rewards: {rewards_edges}")
logger_tlu.debug(f"Algo weights {u_e[80:90]}".replace("\n", ""))
logger_tlu.debug(f"Real weights {true_weights[80:90]}".replace("\n", ""))
if algo_num_nodes > reward_best:
reward_best = algo_num_nodes
s_best = s_oracle
u_e_best = u_e
# ---- Step 3 - Calculating updates ----
for i in algo_obs_edges:
x_e = np.array([df_feats.loc[i].values])
m_inv -= (m_inv @ x_e.T @ x_e @ m_inv) / (
x_e @ m_inv @ x_e.T + sigma * sigma
)
b += x_e.T * int(i in algo_act_edges)
return_dict = {
"rewards": rewards,
"rewards_edges": rewards_edges,
"s_best": s_best,
"u_e_best": u_e_best,
"reward_best": reward_best,
}
logger_tlu.debug("The algorithm has finished running.")
logger_tlu.debug(f"Returning: {return_dict}")
return return_dict
def oim_node2vec(
df,
df_feats,
nodes,
num_inf=10,
sigma=4,
c=0.1,
epsilon=0.4,
num_repeats=30,
num_repeats_reward=20,
oracle=tim,
persist=False,
b=None,
m_inv=None,
hide_tqdm=False,
):
""" Online IM with Node2Vec features
Run the IMLinUCB[1] algorithm with pre-trained Node2Vec [3] features.
Parameters
----------
df : pandas.DataFrame
The graph we run the OIM on, in the form of a DataFrame. A row represents one
edge in the graph, with columns being named "source", "target", and "probab".
"probab" column is the "true" activation probability.
df_feats : pandas.DataFrame
A dataframe with node embeddings generated by node2vec [3]. Should have n rows,
where n is the number of nodes in the graph. The number of columns is specified
when running node2vec on the graph. The smaller the number of columns is the
more uncertain the embedding, but it also speeds up processing.
nodes : pandas.DataFrame
A dataframe of all unique nodes in df. Is used to calculate the number of nodes
for TIM.
num_inf : int, optional
Number of seed nodes to find. Default: 10.
sigma : float, optional
A parameter used by the IMLinUCB algorithm. Refer to the IMLinUCB paper for
more details. [1] Default: 4
c: float, optional
A parameter used by the IMLinUCB algorithm. Refer to the IMLinUCB paper for
more details. [1] Default: 0.1
epsilon : float, optional
A parameter used by the TIM algorithm. Refer to the TIM paper for
more details. [2] Default: 0.4
num_repeats : int, optional
Number of iterations of the IMLinUCB algorithm. The more, the better the
results. Default: 30
num_repeats_reward : int, optional
Number of times to run the simulation at every step of the IMLinUCB algorithm.
A larger number should better the results. Default: 20
num_nodes_tim : int, optional
Number of nodes to use for the offline IM algorithm, TIM [2]. Default: -1
oracle : function, optional
A function to use as the Offline IM algorithm. Default: tim [2]
persist : boolean, optional
A parameter that determines if we are running this algorithm in a temporal IM
toolchain and would want to persist parameters. If True, you need to provide b
and m_inv as well. Default: False
b : pandas.DataFrame, optional
A dataframe of b taken from the previous time step in the temporal IM.
Used if we want to persist parameters through time steps. Default: None
m_inv : pandas.DataFrame, optional
A dataframe of m_inv taken from the previous time step in the temporal IM.
Used if we want to persist parameters through time steps. Default: None
hide_tqdm : boolean, optional
A paremeters used if you want to hide all tqdm progress bars. It's useful if
you want to paralellize the algorithm. Default: False
Returns
-------
return_dict : dict
A dictionary consisting of following keys:
- s_best, the list of the selected seed nodes
- u_e_best, the approximated probabilities
- reward_best, the reward obtained by running IC with s_best
- m_inv, a parameter to use in the next it of temporal OIM
(only if persist is True)
- b, a parameter to use in the next it of temporal OIM
(only if persist is True)
References
----------
.. [1] Wen, Zheng, Branislav Kveton, and Michal Valko.
"Influence maximization with semi-bandit feedback." CoRR, abs/1605.06593 (2016).
.. [2] Tang, Youze, Xiaokui Xiao, and Yanchen Shi.
"Influence maximization: Near-optimal time complexity meets practical efficiency."
Proceedings of the 2014 ACM SIGMOD international conference on Management of data. 2014.
.. [3] Grover, Aditya, and Jure Leskovec.
"node2vec: Scalable feature learning for networks."
Proceedings of the 22nd ACM SIGKDD international conference on
Knowledge discovery and data mining. 2016.
"""
logger_tlu.debug("Started Online Influence Maximization...")
logger_tlu.debug("Setting parameters")
num_feats = df_feats.shape[1]
num_edges_t = df.shape[0]
num_nodes_tim = nodes[-1] + 1 # TIM counts from 0
# "True" probabilities - effectively our test set
true_weights = df["probab"].copy()
# b, M_inv - used by IMLinUCB
if not persist:
b = np.zeros((num_feats, 1))
m_inv = np.eye(num_feats, num_feats)
# Returning these
s_best = []
reward_best = 0
std_best = 0
u_e_best = []
if hide_tqdm:
oim_iterator = range(num_repeats)
else:
oim_iterator = tqdm(
range(num_repeats),
desc=f"OIM iters {num_edges_t} edges",
leave=False,
file=sys.stderr,
)
for iter_oim in oim_iterator:
# ---- Step 1 - Calculating the u_e ----
theta = (m_inv @ b) / (sigma * sigma)
# xMx = (df_feats.values @ m_inv @ df_feats.T.values).clip(min=0)
u_e = []
for i in range(num_edges_t):
x_e = df_feats.loc[i].values
xMx = x_e @ m_inv @ x_e.T # .clip(min=0)
u_e.append(np.clip(x_e @ theta + c * np.sqrt(xMx), 0, 1))
# u_e.append(expit(x_e @ theta + c * np.sqrt(xMx)))
u_e = np.array(u_e)
# ---- Step 2 - Evaluating the performance ----
# Loss function
df["probab"] = u_e
s_oracle = sorted(
oracle(
df[["source", "target", "probab"]],
num_nodes_tim,
num_edges_t,
num_inf,
epsilon,
)
)
# Observing edge-level feedback
df["probab"] = true_weights
all_algo_nodes = []
all_algo_edges = []
all_algo_obs = []
for k in range(num_repeats_reward):
algo_act_nodes, algo_act_edges, algo_obs_edges = run_ic_eff(df, s_oracle)
all_algo_nodes.append(algo_act_nodes)
all_algo_edges.append(algo_act_edges)
all_algo_obs.append(algo_obs_edges)
# Mean node counts
num_affected_nodes = [len(i) for i in all_algo_nodes]
mean_algo_nodes = np.mean(num_affected_nodes)
std_algo_nodes = np.std(num_affected_nodes)
# Used for updating M and b later
all_algo_edges = np.unique(np.concatenate(all_algo_edges))
all_algo_obs = np.unique(np.concatenate(all_algo_obs))
logger_tlu.debug(f"Algo seeds: {s_oracle}")
logger_tlu.debug(f"Algo reward: {mean_algo_nodes}")
logger_tlu.debug(f"Best algo reward: {reward_best}")
logger_tlu.debug(f"Algo weights {u_e[80:90]}".replace("\n", ""))
logger_tlu.debug(f"Real weights {true_weights[80:90]}".replace("\n", ""))
if mean_algo_nodes > reward_best:
reward_best = mean_algo_nodes
std_best = std_algo_nodes
s_best = s_oracle
u_e_best = u_e
# ---- Step 3 - Calculating updates ----
for i in all_algo_obs:
x_e = np.array([df_feats.loc[i].values])
m_inv -= (m_inv @ x_e.T @ x_e @ m_inv) / (
x_e @ m_inv @ x_e.T + sigma * sigma
)
b += x_e.T * int(i in all_algo_edges)
if persist:
return_dict = {
"s_best": s_best,
"u_e_best": u_e_best,
"reward_best": reward_best,
"std_best": std_best,
"m_inv": m_inv,
"b": b,
}
else:
return_dict = {
"s_best": s_best,
"u_e_best": u_e_best,
"reward_best": reward_best,
"std_best": std_best,
}
logger_tlu.debug("The algorithm has finished running.")
logger_tlu.debug(f"Returning: {return_dict}")
return return_dict
# --------------------------------------------------------------------------------------
# %% ------------------------------ Temporal Online IM ---------------------------------
# --------------------------------------------------------------------------------------
def timlinucb(
df_edges,
df_feats,
times,
nodes,
num_seeds=5,
sigma=4,
c=0.1,
epsilon=0.4,
num_repeats_oim=10,
num_repeats_oim_reward=10,
style="additive",
persist=False,
hide_tqdm=False,
):
""" Temporal Online Influence Maximization
Run the IMLinUCB[1] algorithm with pre-trained Node2Vec[3] features at every time
step in the graph df_edges.
Parameters
----------
df_edges : pandas.DataFrame
The graph we run the TOIM on, in the form of a DataFrame. A row represents one
edge in the graph, with columns being named "source", "target", "probab",
and "day". "probab" column is the "true" activation probability and "day" should
correspond to the days specified in times.
df_feats : pandas.DataFrame
A dataframe with node embeddings generated by node2vec [3]. Should have n rows,
where n is the number of nodes in the graph. The number of columns is specified
when running node2vec on the graph. The smaller the number of columns is the
more uncertain the embedding, but it also speeds up processing.
times : pandas.Series, list
A series or a list of the times that we are going to iterate through. Useful
if you don't want to iterate through every day in the network.
nodes : pandas.DataFrame
A dataframe of all unique nodes in df. Is used to calculate the number of nodes
for TIM.
num_inf : int, optional
Number of seed nodes to find. Default: 5.
sigma : float, optional
A parameter used by the IMLinUCB algorithm. Refer to the IMLinUCB paper for
more details. [1] Default: 4
c: float, optional
A parameter used by the IMLinUCB algorithm. Refer to the IMLinUCB paper for
more details. [1] Default: 0.1
epsilon : float, optional
A parameter used by the TIM algorithm. Refer to the TIM paper for
more details. [2] Default: 0.4
num_repeats_oim : int, optional
Number of iterations of the IMLinUCB algorithm. The more, the better the
results. Default: 10
num_repeats_reward_oim : int, optional
Number of times to run the simulation at every step of the IMLinUCB algorithm.
A larger number should better the results. Default: 10
style : str, optional
Determines whether we take into account all edges up to t ("additive") or just
the ones that were formed at t ("dynamic"). Default: "additive"
persist : boolean, optional
Determines if we want to persist the OIM parameters. Default: False
hide_tqdm : boolean, optional
A paremeters used if you want to hide all tqdm progress bars. It's useful if
you want to paralellize the algorithm. Default: False
Returns
-------
results : DataFrame
A dataframe with the following columns
- s_best, the list of the selected seed nodes
- u_e_best, the approximated probabilities
- reward_best, the reward obtained by running IC with s_best
- time, the time step at which everything else was obtained
References
----------
.. [1] Wen, Zheng, Branislav Kveton, and Michal Valko.
"Influence maximization with semi-bandit feedback." CoRR, abs/1605.06593 (2016).
.. [2] Tang, Youze, Xiaokui Xiao, and Yanchen Shi.
"Influence maximization: Near-optimal time complexity meets practical efficiency."
Proceedings of the 2014 ACM SIGMOD international conference on Management of data. 2014.
.. [3] Grover, Aditya, and Jure Leskovec.
"node2vec: Scalable feature learning for networks."
Proceedings of the 22nd ACM SIGKDD international conference on
Knowledge discovery and data mining. 2016.
"""
results = []
# For persistent parameters - making the b and M matrices
if persist:
b = np.zeros((df_feats.shape[1], 1))
m_inv = np.eye(df_feats.shape[1], df_feats.shape[1])
else:
b = None
m_inv = None
times_iter = (
times
if hide_tqdm
else tqdm(times, desc=f"TOIM iters", leave=False, file=sys.stderr)
)
for t in times_iter:
if style == "additive":
df_t = df_edges[df_edges["day"] <= t].sort_values("source").reset_index()
elif style == "dynamic":
df_t = df_edges[df_edges["day"] == t].sort_values("source").reset_index()
df_feats_t = df_t["index"].apply(lambda x: df_feats.loc[x])
result_oim = oim_node2vec(
df_t,
df_feats_t,
nodes,
num_inf=num_seeds,
sigma=sigma,
c=c,
epsilon=epsilon,
num_repeats=num_repeats_oim,
num_repeats_reward=num_repeats_oim_reward,
persist=persist,
m_inv=m_inv,
b=b,
)
result_oim["time"] = t
if persist:
m_inv = result_oim.pop("m_inv")
b = result_oim.pop("b")
results.append(result_oim)
return pd.DataFrame(results)
def timlinucb_parallel_oim(
df_edges,
df_feats,
times,
nodes,
num_seeds=5,
sigma=4,
c=0.1,
epsilon=0.4,
num_repeats_oim=10,
num_repeats_oim_reward=10,
style="additive",
process_id=1,