tgb.linkproppred
LinkPropPredDataset
Bases: object
Source code in tgb/linkproppred/dataset.py
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 |
|
edge_feat: Optional[np.ndarray]
property
Returns the edge features of the dataset with dim [E, feat_dim] Returns: edge_feat: np.ndarray, [E, feat_dim] or None if there is no edge feature
edge_type: Optional[np.ndarray]
property
Returns the edge types of the dataset with dim [E, 1], only for temporal knowledge graph and temporal heterogeneous graph Returns: edge_type: np.ndarray, [E, 1] or None if it is not a TKG or THG
eval_metric: str
property
the official evaluation metric for the dataset, loaded from info.py Returns: eval_metric: str, the evaluation metric
full_data: Dict[str, Any]
property
the full data of the dataset as a dictionary with keys: 'sources', 'destinations', 'timestamps', 'edge_idxs', 'edge_feat', 'w', 'edge_label',
Returns:
Name | Type | Description |
---|---|---|
full_data |
Dict[str, Any]
|
Dict[str, Any] |
negative_sampler: NegativeEdgeSampler
property
Returns the negative sampler of the dataset, will load negative samples from disc Returns: negative_sampler: NegativeEdgeSampler
node_feat: Optional[np.ndarray]
property
Returns the node features of the dataset with dim [N, feat_dim] Returns: node_feat: np.ndarray, [N, feat_dim] or None if there is no node feature
node_type: Optional[np.ndarray]
property
Returns the node types of the dataset with dim [N], only for temporal heterogeneous graphs Returns: node_feat: np.ndarray, [N] or None if there is no node feature
num_edges: int
property
Returns the total number of edges in the dataset Returns: num_edges: int, the number of edges
num_nodes: int
property
Returns the total number of unique nodes in the dataset Returns: num_nodes: int, the number of unique nodes
num_rels: int
property
Returns the number of relation types in the dataset Returns: num_rels: int, the number of relation types
static_data: Optional[np.ndarray]
property
Returns the static edges related to this dataset, applies for tkgl-wikidata and tkgl-smallpedia, edges are (src, dst, rel_type) Returns: df: pd.DataFrame {"head": np.ndarray, "tail": np.ndarray, "rel_type": np.ndarray}
test_mask: np.ndarray
property
Returns the test mask of the dataset: Returns: test_mask: Dict[str, Any]
train_mask: np.ndarray
property
Returns the train mask of the dataset Returns: train_mask: training masks
val_mask: np.ndarray
property
Returns the validation mask of the dataset Returns: val_mask: Dict[str, Any]
__init__(name, root='datasets', meta_dict=None, preprocess=True)
Dataset class for link prediction dataset. Stores meta information about each dataset such as evaluation metrics etc. also automatically pre-processes the dataset. Args: name: name of the dataset root: root directory to store the dataset folder meta_dict: dictionary containing meta information about the dataset, should contain key 'dir_name' which is the name of the dataset folder preprocess: whether to pre-process the dataset
Source code in tgb/linkproppred/dataset.py
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 |
|
download()
downloads this dataset from url check if files are already downloaded
Source code in tgb/linkproppred/dataset.py
generate_processed_files()
turns raw data .csv file into a pandas data frame, stored on disc if not already Returns: df: pandas data frame
Source code in tgb/linkproppred/dataset.py
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 |
|
generate_splits(full_data, val_ratio=0.15, test_ratio=0.15)
Generates train, validation, and test splits from the full dataset Args: full_data: dictionary containing the full dataset val_ratio: ratio of validation data test_ratio: ratio of test data Returns: train_data: dictionary containing the training dataset val_data: dictionary containing the validation dataset test_data: dictionary containing the test dataset
Source code in tgb/linkproppred/dataset.py
load_test_ns()
load_val_ns()
pre_process()
Pre-process the dataset and generates the splits, must be run before dataset properties can be accessed generates the edge data and different train, val, test splits
Source code in tgb/linkproppred/dataset.py
preprocess_static_edges()
Pre-process the static edges of the dataset
Source code in tgb/linkproppred/dataset.py
PyGLinkPropPredDataset
Bases: Dataset
Source code in tgb/linkproppred/dataset_pyg.py
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 |
|
dst: torch.Tensor
property
Returns the destination nodes of the dataset Returns: dst: the idx of the destination nodes
edge_feat: torch.Tensor
property
Returns the edge features of the dataset Returns: edge_feat: the edge features
edge_label: torch.Tensor
property
Returns the edge labels of the dataset Returns: edge_label: the labels of the edges
edge_type: torch.Tensor
property
Returns the edge types for each edge Returns: edge_type: edge type tensor (int)
eval_metric: str
property
the official evaluation metric for the dataset, loaded from info.py Returns: eval_metric: str, the evaluation metric
negative_sampler: NegativeEdgeSampler
property
Returns the negative sampler of the dataset, will load negative samples from disc Returns: negative_sampler: NegativeEdgeSampler
node_feat: torch.Tensor
property
Returns the node features of the dataset Returns: node_feat: the node features
node_type: torch.Tensor
property
Returns the node types of the dataset Returns: node_type: the node types [N]
num_edges: int
property
Returns the total number of edges in the dataset Returns: num_edges: int, the number of edges
num_nodes: int
property
Returns the total number of unique nodes in the dataset Returns: num_nodes: int, the number of unique nodes
num_rels: int
property
Returns the total number of unique relations in the dataset Returns: num_rels: int, the number of unique relations
src: torch.Tensor
property
Returns the source nodes of the dataset Returns: src: the idx of the source nodes
static_data: torch.Tensor
property
Returns the static data of the dataset for tkgl-wikidata and tkgl-smallpedia Returns: static_data: the static data of the dataset
test_mask: torch.Tensor
property
Returns the test mask of the dataset: Returns: test_mask: the mask for edges in the test set
train_mask: torch.Tensor
property
Returns the train mask of the dataset Returns: train_mask: the mask for edges in the training set
ts: torch.Tensor
property
Returns the timestamps of the dataset Returns: ts: the timestamps of the edges
val_mask: torch.Tensor
property
Returns the validation mask of the dataset Returns: val_mask: the mask for edges in the validation set
__init__(name, root, transform=None, pre_transform=None)
PyG wrapper for the LinkPropPredDataset
can return pytorch tensors for src,dst,t,msg,label
can return Temporal Data object
Parameters:
name: name of the dataset, passed to LinkPropPredDataset
root (string): Root directory where the dataset should be saved, passed to LinkPropPredDataset
transform (callable, optional): A function/transform that takes in an, not used in this case
pre_transform (callable, optional): A function/transform that takes in, not used in this case
Source code in tgb/linkproppred/dataset_pyg.py
get(idx)
construct temporal data object for a single edge Parameters: idx: index of the edge Returns: data: TemporalData object
Source code in tgb/linkproppred/dataset_pyg.py
get_TemporalData()
return the TemporalData object for the entire dataset
Source code in tgb/linkproppred/dataset_pyg.py
len()
load_test_ns()
load_val_ns()
process_data()
convert the numpy arrays from dataset to pytorch tensors
Source code in tgb/linkproppred/dataset_pyg.py
Evaluator Module for Dynamic Link Prediction
Evaluator
Bases: object
Evaluator for Link Property Prediction
Source code in tgb/linkproppred/evaluate.py
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 |
|
__init__(name, k_value=10)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
name of the dataset |
required |
k_value |
int
|
the desired 'k' value for calculating metric@k |
10
|
Source code in tgb/linkproppred/evaluate.py
eval(input_dict, verbose=False)
evaluate the link prediction task this method is callable through an instance of this object to compute the metric
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_dict |
dict
|
a dictionary containing "y_pred_pos", "y_pred_neg", and "eval_metric" the performance metric is calculated for the provided scores |
required |
verbose |
bool
|
whether to print out the computed metric |
False
|
Returns:
Name | Type | Description |
---|---|---|
perf_dict |
dict
|
a dictionary containing the computed performance metric |
Source code in tgb/linkproppred/evaluate.py
Sample negative edges for evaluation of dynamic link prediction Load already generated negative edges from file, batch them based on the positive edge, and return the evaluation set
NegativeEdgeSampler
Bases: object
Source code in tgb/linkproppred/negative_sampler.py
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 |
|
__init__(dataset_name, first_dst_id=0, last_dst_id=0, strategy='hist_rnd')
Negative Edge Sampler Loads and query the negative batches based on the positive batches provided. constructor for the negative edge sampler class
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_name |
str
|
name of the dataset |
required |
first_dst_id |
int
|
identity of the first destination node |
0
|
last_dst_id |
int
|
indentity of the last destination node |
0
|
strategy |
str
|
will always load the pre-generated negatives |
'hist_rnd'
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in tgb/linkproppred/negative_sampler.py
load_eval_set(fname, split_mode='val')
Load the evaluation set from disk, can be either val or test set ns samples
Parameters:
fname: the file name of the evaluation ns on disk
split_mode: the split mode of the evaluation set, can be either val
or test
Returns:
Type | Description |
---|---|
None
|
None |
Source code in tgb/linkproppred/negative_sampler.py
query_batch(pos_src, pos_dst, pos_timestamp, edge_type=None, split_mode='test')
For each positive edge in the pos_batch
, return a list of negative edges
split_mode
specifies whether the valiation or test evaluation set should be retrieved.
modify now to include edge type argument
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pos_src |
Tensor
|
list of positive source nodes |
required |
pos_dst |
Tensor
|
list of positive destination nodes |
required |
pos_timestamp |
Tensor
|
list of timestamps of the positive edges |
required |
split_mode |
str
|
specifies whether to generate negative edges for 'validation' or 'test' splits |
'test'
|
Returns:
Name | Type | Description |
---|---|---|
neg_samples |
list
|
a list of list; each internal list contains the set of negative edges that should be evaluated against each positive edge. |
Source code in tgb/linkproppred/negative_sampler.py
reset_eval_set(split_mode='test')
Reset evaluation set
Parameters:
Name | Type | Description | Default |
---|---|---|---|
split_mode |
str
|
specifies whether to generate negative edges for 'validation' or 'test' splits |
'test'
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in tgb/linkproppred/negative_sampler.py
Sample and Generate negative edges that are going to be used for evaluation of a dynamic graph learning model
Negative samples are generated and saved to files ONLY once;
other times, they should be loaded from file with instances of the negative_sampler.py
.
NegativeEdgeGenerator
Bases: object
Source code in tgb/linkproppred/negative_generator.py
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 |
|
__init__(dataset_name, first_dst_id, last_dst_id, num_neg_e=100, strategy='rnd', rnd_seed=123, hist_ratio=0.5, historical_data=None)
Negative Edge Sampler class this is a class for generating negative samples for a specific datasets the set of the positive samples are provided, the negative samples are generated with specific strategies and are saved for consistent evaluation across different methods negative edges are sampled with 'oen_vs_many' strategy. it is assumed that the destination nodes are indexed sequentially with 'first_dst_id' and 'last_dst_id' being the first and last index, respectively.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_name |
str
|
name of the dataset |
required |
first_dst_id |
int
|
identity of the first destination node |
required |
last_dst_id |
int
|
indentity of the last destination node |
required |
num_neg_e |
int
|
number of negative edges being generated per each positive edge |
100
|
strategy |
str
|
how to generate negative edges; can be 'rnd' or 'hist_rnd' |
'rnd'
|
rnd_seed |
int
|
random seed for consistency |
123
|
hist_ratio |
float
|
if the startegy is 'hist_rnd', how much of the negatives are historical |
0.5
|
historical_data |
TemporalData
|
previous records of the positive edges |
None
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in tgb/linkproppred/negative_generator.py
generate_historical_edge_set(historical_data)
Generate the set of edges seen durign training or validation
ONLY train_data
should be passed as historical data; i.e., the HISTORICAL negative edges should be selected from training data only.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
historical_data |
TemporalData
|
contains the positive edges observed previously |
required |
Returns:
Name | Type | Description |
---|---|---|
historical_edges |
tuple
|
distict historical positive edges |
hist_edge_set_per_node |
tuple
|
historical edges observed for each node |
Source code in tgb/linkproppred/negative_generator.py
generate_negative_samples(data, split_mode, partial_path)
Generate negative samples
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
TemporalData
|
an object containing positive edges information |
required |
split_mode |
str
|
specifies whether to generate negative edges for 'validation' or 'test' splits |
required |
partial_path |
str
|
in which directory save the generated negatives |
required |
Source code in tgb/linkproppred/negative_generator.py
generate_negative_samples_hist_rnd(historical_data, data, split_mode, filename)
Generate negative samples based on the HIST-RND
strategy:
- up to 50% of the negative samples are selected from the set of edges seen during the training with the same source node.
- the rest of the negative edges are randomly sampled with the fixed source node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
historical_data |
TemporalData
|
contains the history of the observed positive edges including distinct positive edges and edges observed for each positive node |
required |
data |
TemporalData
|
an object containing positive edges information |
required |
split_mode |
str
|
specifies whether to generate negative edges for 'validation' or 'test' splits |
required |
filename |
str
|
name of the file to save generated negative edges |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in tgb/linkproppred/negative_generator.py
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 |
|
generate_negative_samples_rnd(data, split_mode, filename)
Generate negative samples based on the HIST-RND
strategy:
- for each positive edge, sample a batch of negative edges from all possible edges with the same source node
- filter actual positive edges
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
TemporalData
|
an object containing positive edges information |
required |
split_mode |
str
|
specifies whether to generate negative edges for 'validation' or 'test' splits |
required |
filename |
str
|
name of the file containing the generated negative edges |
required |
Source code in tgb/linkproppred/negative_generator.py
Sample and Generate negative edges that are going to be used for evaluation of a dynamic graph learning model
Negative samples are generated and saved to files ONLY once;
other times, they should be loaded from file with instances of the negative_sampler.py
.
TKGNegativeEdgeGenerator
Bases: object
Source code in tgb/linkproppred/tkg_negative_generator.py
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 |
|
__init__(dataset_name, first_dst_id, last_dst_id, strategy='time-filtered', num_neg_e=-1, rnd_seed=1, partial_path=None, edge_data=None)
Negative Edge Generator class for Temporal Knowledge Graphs constructor for the negative edge generator class
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_name |
str
|
name of the dataset |
required |
first_dst_id |
int
|
identity of the first destination node |
required |
last_dst_id |
int
|
indentity of the last destination node |
required |
num_neg_e |
int
|
number of negative edges being generated per each positive edge |
-1
|
strategy |
str
|
specifies which strategy should be used for generating the negatives |
'time-filtered'
|
rnd_seed |
int
|
random seed for reproducibility |
1
|
edge_data |
TemporalData
|
the positive edges to generate the negatives for, assuming sorted temporally |
None
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in tgb/linkproppred/tkg_negative_generator.py
generate_dst_dict(edge_data, dst_name)
Generate a dictionary of destination nodes for each type of edge
Parameters:
Name | Type | Description | Default |
---|---|---|---|
edge_data |
TemporalData
|
an object containing positive edges information |
required |
dst_name |
str
|
name of the file to save the generated dictionary of destination nodes |
required |
Returns:
Name | Type | Description |
---|---|---|
dst_dict |
dict
|
a dictionary of destination nodes for each type of edge |
Source code in tgb/linkproppred/tkg_negative_generator.py
generate_negative_samples(pos_edges, split_mode, partial_path)
Generate negative samples
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pos_edges |
TemporalData
|
positive edges to generate the negatives for |
required |
split_mode |
str
|
specifies whether to generate negative edges for 'validation' or 'test' splits |
required |
partial_path |
str
|
in which directory save the generated negatives |
required |
Source code in tgb/linkproppred/tkg_negative_generator.py
generate_negative_samples_dst(data, split_mode, filename)
now we consider (s, d, t, edge_type) as a unique edge Generate negative samples based on the random strategy: - for each positive edge, sample a batch of negative edges from all possible edges with the same source node - filter actual positive edges at the same timestamp with the same edge type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
TemporalData
|
an object containing positive edges information |
required |
split_mode |
str
|
specifies whether to generate negative edges for 'validation' or 'test' splits |
required |
filename |
str
|
name of the file containing the generated negative edges |
required |
Source code in tgb/linkproppred/tkg_negative_generator.py
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 |
|
generate_negative_samples_ftr(data, split_mode, filename)
now we consider (s, d, t, edge_type) as a unique edge Generate negative samples based on the random strategy: - for each positive edge, sample a batch of negative edges from all possible edges with the same source node - filter actual positive edges at the same timestamp with the same edge type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
TemporalData
|
an object containing positive edges information |
required |
split_mode |
str
|
specifies whether to generate negative edges for 'validation' or 'test' splits |
required |
filename |
str
|
name of the file containing the generated negative edges |
required |
Source code in tgb/linkproppred/tkg_negative_generator.py
generate_negative_samples_random(data, split_mode, filename)
generate random negative edges for ablation study
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
TemporalData
|
an object containing positive edges information |
required |
split_mode |
str
|
specifies whether to generate negative edges for 'validation' or 'test' splits |
required |
filename |
str
|
name of the file containing the generated negative edges |
required |
Source code in tgb/linkproppred/tkg_negative_generator.py
Sample negative edges for evaluation of dynamic link prediction Load already generated negative edges from file, batch them based on the positive edge, and return the evaluation set
TKGNegativeEdgeSampler
Bases: object
Source code in tgb/linkproppred/tkg_negative_sampler.py
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 |
|
__init__(dataset_name, first_dst_id, last_dst_id, strategy='time-filtered', partial_path=PROJ_DIR + '/data/processed')
Negative Edge Sampler Loads and query the negative batches based on the positive batches provided. constructor for the negative edge sampler class
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_name |
str
|
name of the dataset |
required |
first_dst_id |
int
|
identity of the first destination node |
required |
last_dst_id |
int
|
indentity of the last destination node |
required |
strategy |
str
|
will always load the pre-generated negatives |
'time-filtered'
|
partial_path |
str
|
the path to the directory where the negative edges are stored |
PROJ_DIR + '/data/processed'
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in tgb/linkproppred/tkg_negative_sampler.py
load_eval_set(fname, split_mode='val')
Load the evaluation set from disk, can be either val or test set ns samples
Parameters:
fname: the file name of the evaluation ns on disk
split_mode: the split mode of the evaluation set, can be either val
or test
Returns:
Type | Description |
---|---|
None
|
None |
Source code in tgb/linkproppred/tkg_negative_sampler.py
query_batch(pos_src, pos_dst, pos_timestamp, edge_type, split_mode='test')
For each positive edge in the pos_batch
, return a list of negative edges
split_mode
specifies whether the valiation or test evaluation set should be retrieved.
modify now to include edge type argument
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pos_src |
Union[Tensor, ndarray]
|
list of positive source nodes |
required |
pos_dst |
Union[Tensor, ndarray]
|
list of positive destination nodes |
required |
pos_timestamp |
Union[Tensor, ndarray]
|
list of timestamps of the positive edges |
required |
split_mode |
str
|
specifies whether to generate negative edges for 'validation' or 'test' splits |
'test'
|
Returns:
Name | Type | Description |
---|---|---|
neg_samples |
list
|
list of numpy array; each array contains the set of negative edges that should be evaluated against each positive edge. |
Source code in tgb/linkproppred/tkg_negative_sampler.py
Sample and Generate negative edges that are going to be used for evaluation of a dynamic graph learning model
Negative samples are generated and saved to files ONLY once;
other times, they should be loaded from file with instances of the negative_sampler.py
.
THGNegativeEdgeGenerator
Bases: object
Source code in tgb/linkproppred/thg_negative_generator.py
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 |
|
__init__(dataset_name, first_node_id, last_node_id, node_type, strategy='node-type-filtered', num_neg_e=-1, rnd_seed=1, edge_data=None)
Negative Edge Generator class for Temporal Heterogeneous Graphs this is a class for generating negative samples for a specific datasets the set of the positive samples are provided, the negative samples are generated with specific strategies and are saved for consistent evaluation across different methods
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_name |
str
|
name of the dataset |
required |
first_node_id |
int
|
the first node id |
required |
last_node_id |
int
|
the last node id |
required |
node_type |
Union[ndarray, Tensor]
|
the node type of each node |
required |
strategy |
str
|
the strategy to generate negative samples |
'node-type-filtered'
|
num_neg_e |
int
|
number of negative samples to generate |
-1
|
rnd_seed |
int
|
random seed |
1
|
edge_data |
TemporalData
|
the edge data object containing the positive edges |
None
|
Returns: None
Source code in tgb/linkproppred/thg_negative_generator.py
generate_negative_samples(pos_edges, split_mode, partial_path)
Generate negative samples
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pos_edges |
TemporalData
|
positive edges to generate the negatives for |
required |
split_mode |
str
|
specifies whether to generate negative edges for 'validation' or 'test' splits |
required |
partial_path |
str
|
in which directory save the generated negatives |
required |
Source code in tgb/linkproppred/thg_negative_generator.py
generate_negative_samples_nt(data, split_mode, filename)
now we consider (s, d, t, edge_type) as a unique edge, also adding the node type info for the destination node for convenience so (s, d, t, edge_type): (conflict_set, d_node_type) Generate negative samples based on the random strategy: - for each positive edge, retrieve all possible destinations based on the node type of the destination node - filter actual positive edges at the same timestamp with the same edge type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
TemporalData
|
an object containing positive edges information |
required |
split_mode |
str
|
specifies whether to generate negative edges for 'validation' or 'test' splits |
required |
filename |
str
|
name of the file containing the generated negative edges |
required |
Source code in tgb/linkproppred/thg_negative_generator.py
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 |
|
generate_negative_samples_random(data, split_mode, filename)
generate random negative edges for ablation study
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
TemporalData
|
an object containing positive edges information |
required |
split_mode |
str
|
specifies whether to generate negative edges for 'validation' or 'test' splits |
required |
filename |
str
|
name of the file containing the generated negative edges |
required |
Source code in tgb/linkproppred/thg_negative_generator.py
get_destinations_based_on_node_type(first_node_id, last_node_id, node_type)
get the destination node id arrays based on the node type Parameters: first_node_id: the first node id last_node_id: the last node id node_type: the node type of each node
Returns:
Name | Type | Description |
---|---|---|
node_type_dict |
dict
|
a dictionary containing the destination node ids for each node type |
Source code in tgb/linkproppred/thg_negative_generator.py
Sample negative edges for evaluation of dynamic link prediction Load already generated negative edges from file, batch them based on the positive edge, and return the evaluation set
THGNegativeEdgeSampler
Bases: object
Source code in tgb/linkproppred/thg_negative_sampler.py
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 |
|
__init__(dataset_name, first_node_id, last_node_id, node_type, strategy='node-type-filtered')
Negative Edge Sampler Loads and query the negative batches based on the positive batches provided. constructor for the negative edge sampler class
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_name |
str
|
name of the dataset |
required |
first_node_id |
int
|
identity of the first node |
required |
last_node_id |
int
|
indentity of the last destination node |
required |
node_type |
ndarray
|
the node type of each node |
required |
strategy |
str
|
will always load the pre-generated negatives |
'node-type-filtered'
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in tgb/linkproppred/thg_negative_sampler.py
load_eval_set(fname, split_mode='val')
Load the evaluation set from disk, can be either val or test set ns samples
Parameters:
fname: the file name of the evaluation ns on disk
split_mode: the split mode of the evaluation set, can be either val
or test
Returns:
Type | Description |
---|---|
None
|
None |
Source code in tgb/linkproppred/thg_negative_sampler.py
query_batch(pos_src, pos_dst, pos_timestamp, edge_type, split_mode='test')
For each positive edge in the pos_batch
, return a list of negative edges
split_mode
specifies whether the valiation or test evaluation set should be retrieved.
modify now to include edge type argument
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pos_src |
Union[Tensor, ndarray]
|
list of positive source nodes |
required |
pos_dst |
Union[Tensor, ndarray]
|
list of positive destination nodes |
required |
pos_timestamp |
Union[Tensor, ndarray]
|
list of timestamps of the positive edges |
required |
split_mode |
str
|
specifies whether to generate negative edges for 'validation' or 'test' splits |
'test'
|
Returns:
Name | Type | Description |
---|---|---|
neg_samples |
list
|
list of numpy array; each array contains the set of negative edges that should be evaluated against each positive edge. |