Download code-release/multi-hop/export_two_graph_dataset.py from fhyfhy/Implicit-Reasoning-checkpoints: direct link, hf CLI and curl.
- Browser
- Download file 6.76 kB
-
https://huggingface.co/fhyfhy/Implicit-Reasoning-checkpoints/resolve/main/code-release/multi-hop/export_two_graph_dataset.py
- Command line
-
hf download hf://fhyfhy/Implicit-Reasoning-checkpoints/code-release/multi-hop/export_two_graph_dataset.py
-
curl -L -o export_two_graph_dataset.py https://huggingface.co/fhyfhy/Implicit-Reasoning-checkpoints/resolve/main/code-release/multi-hop/export_two_graph_dataset.py
6.76 kB
| #!/usr/bin/env python3 | |
| """ | |
| Build the static two-graph natural-language two-hop dataset (Sec. 5.2): | |
| - train: all atom data from graph 1 + graph 2, plus a train subset of multi-hop QA from graph 1 | |
| - test_id: disjoint multi-hop QA subset from graph 1 | |
| - test_ood: multi-hop QA subset from graph 2 | |
| - manifest: settings, counts, entities and relations; graph1_edges / graph2_edges: edge lists | |
| Paper datasets (run from multi-hop/; all other defaults are the paper settings): | |
| python export_two_graph_dataset.py --output-dir data/custom/two_graph_direct --multi-hop-format direct | |
| python export_two_graph_dataset.py --output-dir data/custom/two_graph_reverse --multi-hop-format reverse | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import random | |
| from pathlib import Path | |
| from typing import Dict, List | |
| from generate_data import DataGenerator | |
| from utils.data.name_space import single_names | |
| # Prefix every sequence with a space, so that the first word is tokenized like all | |
| # others (build_graph_tokenizer.py adds names and relations as " <word>" tokens). | |
| LEADING_SPACE = True | |
| def graph_to_records(generator: DataGenerator) -> List[Dict]: | |
| return [ | |
| {"source": source, "relation": relation, "target": target} | |
| for (source, relation), target in generator.knowledge_graph.items() | |
| ] | |
| def add_metadata(records: List[Dict], split: str, graph_name: str) -> List[Dict]: | |
| out: List[Dict] = [] | |
| for record in records: | |
| item = dict(record) | |
| item["split"] = split | |
| item["graph"] = graph_name | |
| out.append(item) | |
| return out | |
| def build_generators(args: argparse.Namespace) -> tuple[DataGenerator, DataGenerator]: | |
| total_entities = 2 * args.num_entities | |
| if total_entities > len(single_names): | |
| raise ValueError( | |
| f"Need {total_entities} unique entities for two graphs, but namespace only has {len(single_names)} names" | |
| ) | |
| rng = random.Random(args.seed) | |
| sampled_entities = rng.sample(single_names, total_entities) | |
| graph1_entities = sampled_entities[: args.num_entities] | |
| graph2_entities = sampled_entities[args.num_entities :] | |
| shared_relations = DataGenerator(num_entities=1, num_relations=args.num_relations, seed=args.seed).relations | |
| graph1 = DataGenerator( | |
| num_entities=args.num_entities, | |
| num_relations=args.num_relations, | |
| seed=args.seed, | |
| entities=graph1_entities, | |
| relations=shared_relations, | |
| ) | |
| graph1.generate_graph(edges_per_entity=args.edges_per_entity) | |
| graph2 = DataGenerator( | |
| num_entities=args.num_entities, | |
| num_relations=args.num_relations, | |
| seed=args.seed + 1, | |
| entities=graph2_entities, | |
| relations=shared_relations, | |
| ) | |
| graph2.generate_graph(edges_per_entity=args.edges_per_entity) | |
| return graph1, graph2 | |
| def write_json(path: Path, payload) -> None: | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| path.write_text(json.dumps(payload, indent=2)) | |
| print(f"Saved {path}") | |
| def parse_args() -> argparse.Namespace: | |
| parser = argparse.ArgumentParser(description="Export a static two-graph multi-hop dataset.") | |
| parser.add_argument("--output-dir", type=Path, required=True) | |
| parser.add_argument("--multi-hop-format", choices=["direct", "reverse"], required=True) | |
| parser.add_argument("--num-entities", type=int, default=200, help="Entities per graph.") | |
| parser.add_argument("--num-relations", type=int, default=50) | |
| parser.add_argument("--edges-per-entity", type=int, default=10) | |
| parser.add_argument("--num-hops", type=int, default=2) | |
| parser.add_argument("--num-train-multihop", type=int, default=2000) | |
| parser.add_argument("--num-test-id", type=int, default=1000) | |
| parser.add_argument("--num-test-ood", type=int, default=1000) | |
| parser.add_argument("--seed", type=int, default=42) | |
| return parser.parse_args() | |
| def main() -> None: | |
| args = parse_args() | |
| graph1, graph2 = build_generators(args) | |
| random.seed(args.seed + 101) | |
| train_atom = add_metadata( | |
| graph1.generate_all_atom_examples(leading_space=LEADING_SPACE), | |
| split="train", | |
| graph_name="graph1", | |
| ) | |
| random.seed(args.seed + 202) | |
| train_atom += add_metadata( | |
| graph2.generate_all_atom_examples(leading_space=LEADING_SPACE), | |
| split="train", | |
| graph_name="graph2", | |
| ) | |
| seen_graph1 = set() | |
| random.seed(args.seed + 303) | |
| train_multihop = add_metadata( | |
| graph1.sample_unique_multihop_examples( | |
| num_examples=args.num_train_multihop, | |
| num_hops=args.num_hops, | |
| multi_hop_format=args.multi_hop_format, | |
| leading_space=LEADING_SPACE, | |
| seen=seen_graph1, | |
| ), | |
| split="train", | |
| graph_name="graph1", | |
| ) | |
| random.seed(args.seed + 404) | |
| test_id = add_metadata( | |
| graph1.sample_unique_multihop_examples( | |
| num_examples=args.num_test_id, | |
| num_hops=args.num_hops, | |
| multi_hop_format=args.multi_hop_format, | |
| leading_space=LEADING_SPACE, | |
| seen=seen_graph1, | |
| ), | |
| split="test_id", | |
| graph_name="graph1", | |
| ) | |
| random.seed(args.seed + 505) | |
| test_ood = add_metadata( | |
| graph2.sample_unique_multihop_examples( | |
| num_examples=args.num_test_ood, | |
| num_hops=args.num_hops, | |
| multi_hop_format=args.multi_hop_format, | |
| leading_space=LEADING_SPACE, | |
| ), | |
| split="test_ood", | |
| graph_name="graph2", | |
| ) | |
| train_records = train_atom + train_multihop | |
| manifest = { | |
| "num_entities_per_graph": args.num_entities, | |
| "num_relations": args.num_relations, | |
| "edges_per_entity": args.edges_per_entity, | |
| "num_hops": args.num_hops, | |
| "multi_hop_format": args.multi_hop_format, | |
| "atom_sequence_mode": "single", # one atomic fact per training example | |
| "leading_space": LEADING_SPACE, | |
| "counts": { | |
| "train_atom": len(train_atom), | |
| "train_multihop": len(train_multihop), | |
| "train_total": len(train_records), | |
| "test_id": len(test_id), | |
| "test_ood": len(test_ood), | |
| }, | |
| "graph1_entities": graph1.entities, | |
| "graph2_entities": graph2.entities, | |
| "relations": graph1.relations, | |
| } | |
| write_json(args.output_dir / "train.json", train_records) | |
| write_json(args.output_dir / "test_id.json", test_id) | |
| write_json(args.output_dir / "test_ood.json", test_ood) | |
| write_json(args.output_dir / "manifest.json", manifest) | |
| write_json(args.output_dir / "graph1_edges.json", graph_to_records(graph1)) | |
| write_json(args.output_dir / "graph2_edges.json", graph_to_records(graph2)) | |
| if __name__ == "__main__": | |
| main() | |