fhyfhy's picture
Add code/
74b7543 verified
|
Raw History Blame Contribute Delete
3.47 kB

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) for O(1) access during profile generation.
  • Parallelism: Supports PyTorch's DataLoader multiprocessing (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 DataGenerator and builds the graph structure once.
  • It wraps the generator in an OnlineDataset.
  • It ignores num_train_epochs and uses max_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:

  1. Defines configuration variables (Data params, Model params, Training hyperparameters).
  2. Generates a temporary JSON config file.
  3. Runs train.py with 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: Enable torch.compile for 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 using generate_data.py if needed.

Example Command:

./run_eval_wrapper.sh

Quick Start Guide

  1. Start Training Open run_train_online.sh, adjust EXP_NAME="my_experiment", and run:

    chmod +x run_train_online.sh
    ./run_train_online.sh
    
  2. Generate 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/custom
    
  3. Evaluate Open run_eval_wrapper.sh, point MODEL_PATH to archives/my_experiment, and run:

    chmod +x run_eval_wrapper.sh
    ./run_eval_wrapper.sh