Download code/multi-hop/readme.md from fhyfhy/Implicit-Reasoning-checkpoints: direct link, hf CLI and curl.
- Browser
- Download file 3.47 kB
-
https://huggingface.co/fhyfhy/Implicit-Reasoning-checkpoints/resolve/main/code/multi-hop/readme.md
- Command line
-
hf download hf://fhyfhy/Implicit-Reasoning-checkpoints/code/multi-hop/readme.md
-
curl -L -o readme.md https://huggingface.co/fhyfhy/Implicit-Reasoning-checkpoints/resolve/main/code/multi-hop/readme.md
Implicit Multi-Hop Reasoning
Requirements
Use python 3.8+ and install the required packages using pip.
pip install -r requirements.txt
Online Data Generation & Training
This project implements efficient online data generation for training multi-hop reasoning models. Instead of pre-generating a large static dataset, this approach generates data on-the-fly during training, allowing for effectively infinite training data and better coverage of the graph space.
Implementation Details
1. Online Dataset (online_data.py)
The core component is the OnlineDataset class (inherits from torch.utils.data.IterableDataset).
- Efficiency: Pre-indexes the knowledge graph into
entity_to_facts(mapping entities to their relations/neighbors) forO(1)access during profile generation. - Parallelism: Supports PyTorch's
DataLoadermultiprocessing (num_workers > 0) with unique seeding per worker. - Streaming: The dataset is infinite. Training length is controlled by
max_steps.
2. Training Integration (train.py)
The train.py script supports the --online_data flag. When enabled:
- It initializes the
DataGeneratorand builds the graph structure once. - It wraps the generator in an
OnlineDataset. - It ignores
num_train_epochsand usesmax_steps.
Usage
We provide two Bash scripts to simplify configuration and execution. All parameters are defined as variables at the top of the scripts.
1. Training (run_train_online.sh)
This script handles the entire training pipeline:
- Defines configuration variables (Data params, Model params, Training hyperparameters).
- Generates a temporary JSON config file.
- Runs
train.pywith this config.
Key Parameters to Adjust:
NUM_ENTITIES/NUM_RELATIONS: Graph size.EDGES_PER_ENTITY: Graph density.NUM_HOPS: Reasoning depth (e.g., 2, 3, 4).TRAIN_QA_RATIO: Balance between fact learning (0.0) and QA reasoning (1.0).USE_POWERLAW: Toggle Zipf distribution for relation sampling.SAMPLING_ALPHA: Alpha parameter for Power Law.TORCH_COMPILE: Enabletorch.compilefor faster training (requires PyTorch 2.0+).MAX_STEPS: Duration of training.BATCH_SIZE/GRAD_ACC_STEPS: Optimization settings.
Example Command:
./run_train_online.sh
2. Evaluation (run_eval_wrapper.sh)
This script wraps the evaluation logic. It requires a trained model checkpoint and a test dataset file.
Note: While training is online, standard evaluation is performed on a fixed test set to ensure comparable metrics.
Key Parameters:
MODEL_PATH: Path to your checkpoint (e.g.,archives/online_test_v1).DATA_PATH: Path to a static test set (JSON). You can generate one usinggenerate_data.pyif needed.
Example Command:
./run_eval_wrapper.sh
Quick Start Guide
Start Training Open
run_train_online.sh, adjustEXP_NAME="my_experiment", and run:chmod +x run_train_online.sh ./run_train_online.shGenerate a Test Set (if missing) If you don't have a test set for your specific graph configuration:
python generate_data.py --num_entities 50 --num_relations 50 --num_hops 2 --output_dir data/customEvaluate Open
run_eval_wrapper.sh, pointMODEL_PATHtoarchives/my_experiment, and run:chmod +x run_eval_wrapper.sh ./run_eval_wrapper.sh