-
Notifications
You must be signed in to change notification settings - Fork 18
Added tkgl i/o #371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Added tkgl i/o #371
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a223a28
Added tkgl i/o
342798e
added edgebank example for tkgl
56d97e0
Updated test
11a2225
Fixed bug on edge_event_idx from DGData
a622543
Fixed unit tests failed
0e4375b
Updated unit test for node event and egde event idx
5e0c4c7
Cleaned up
f9a051b
Updated time granularities
2ca093f
Updated unit test for time
e5f27af
Pull update from main and update edge_x to use edge_feature for tkgl
benjaminnNgo fa8ab2b
Pump timeout for edgebank for tkgl
benjaminnNgo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import argparse | ||
|
|
||
| import numpy as np | ||
| import torch | ||
| from tgb.linkproppred.evaluate import Evaluator | ||
| from tqdm import tqdm | ||
|
|
||
| from tgm import DGraph | ||
| from tgm.constants import METRIC_TGB_LINKPROPPRED | ||
| from tgm.data import DGData, DGDataLoader | ||
| from tgm.hooks import HookManager, TGBTKGNegativeEdgeSamplerHook | ||
| from tgm.nn import EdgeBankPredictor | ||
| from tgm.util.logging import enable_logging, log_latency, log_metric | ||
| from tgm.util.seed import seed_everything | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| description='EdgeBank LinkPropPred Example for knowledge graph', | ||
| formatter_class=argparse.ArgumentDefaultsHelpFormatter, | ||
| ) | ||
| parser.add_argument('--seed', type=int, default=1337, help='random seed to use') | ||
| parser.add_argument( | ||
| '--dataset', type=str, default='tkgl-smallpedia', help='Dataset name' | ||
| ) | ||
| parser.add_argument('--bsize', type=int, default=200, help='batch size') | ||
| parser.add_argument('--window-ratio', type=float, default=0.15, help='Window ratio') | ||
| parser.add_argument('--pos-prob', type=float, default=1.0, help='Positive edge prob') | ||
| parser.add_argument( | ||
| '--memory-mode', | ||
| type=str, | ||
| default='unlimited', | ||
| choices=['unlimited', 'fixed'], | ||
| help='Memory mode', | ||
| ) | ||
| parser.add_argument( | ||
| '--log-file-path', type=str, default=None, help='Optional path to write logs' | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
| enable_logging(log_file_path=args.log_file_path) | ||
|
|
||
|
|
||
| @log_latency | ||
| def eval( | ||
| loader: DGDataLoader, | ||
| model: EdgeBankPredictor, | ||
| evaluator: Evaluator, | ||
| ) -> float: | ||
| perf_list = [] | ||
| for batch in tqdm(loader): | ||
| for idx, neg_batch in enumerate(batch.neg_batch_list): | ||
| query_src = batch.edge_src[idx].repeat(len(neg_batch) + 1) | ||
| query_dst = torch.cat([batch.edge_dst[idx].unsqueeze(0), neg_batch]) | ||
|
|
||
| y_pred = model(query_src, query_dst) | ||
| input_dict = { | ||
| 'y_pred_pos': y_pred[0], | ||
| 'y_pred_neg': y_pred[1:], | ||
| 'eval_metric': [METRIC_TGB_LINKPROPPRED], | ||
| } | ||
| perf_list.append(evaluator.eval(input_dict)[METRIC_TGB_LINKPROPPRED]) | ||
| model.update(batch.edge_src, batch.edge_dst, batch.edge_time) | ||
|
|
||
| return float(np.mean(perf_list)) | ||
|
|
||
|
|
||
| seed_everything(args.seed) | ||
| evaluator = Evaluator(name=args.dataset) | ||
|
|
||
| data = DGData.from_tgb(args.dataset) | ||
| min_dst_node = data.edge_index[:, 1].min().int() | ||
| max_dst_node = data.edge_index[:, 1].max().int() | ||
|
|
||
| train_data, val_data, test_data = data.split() | ||
| train_dg = DGraph(train_data) | ||
| val_dg = DGraph(val_data) | ||
| test_dg = DGraph(test_data) | ||
|
|
||
| train_data = train_dg.materialize(materialize_features=False) | ||
|
|
||
|
|
||
| hm = HookManager(keys=['val', 'test']) | ||
| hm.register( | ||
| 'val', | ||
| TGBTKGNegativeEdgeSamplerHook( | ||
| args.dataset, | ||
| split_mode='val', | ||
| first_dst_id=min_dst_node, | ||
| last_dst_id=max_dst_node, | ||
| ), | ||
| ) | ||
| hm.register( | ||
| 'test', | ||
| TGBTKGNegativeEdgeSamplerHook( | ||
| args.dataset, | ||
| split_mode='test', | ||
| first_dst_id=min_dst_node, | ||
| last_dst_id=max_dst_node, | ||
| ), | ||
| ) | ||
|
|
||
| val_loader = DGDataLoader(val_dg, args.bsize, hook_manager=hm) | ||
| test_loader = DGDataLoader(test_dg, args.bsize, hook_manager=hm) | ||
|
|
||
| model = EdgeBankPredictor( | ||
| train_data.edge_src, | ||
| train_data.edge_dst, | ||
| train_data.edge_time, | ||
| memory_mode=args.memory_mode, | ||
| window_ratio=args.window_ratio, | ||
| pos_prob=args.pos_prob, | ||
| ) | ||
|
|
||
| with hm.activate('val'): | ||
| val_mrr = eval(val_loader, model, evaluator) | ||
| log_metric(f'Validation {METRIC_TGB_LINKPROPPRED}', val_mrr) | ||
|
|
||
| with hm.activate('test'): | ||
| test_mrr = eval(test_loader, model, evaluator) | ||
| log_metric(f'Test {METRIC_TGB_LINKPROPPRED}', test_mrr) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.