This repository contains an adaptation of Graph Matching Networks (GMN) for linguistic dependency trees, focused on natural language inference (NLI) and semantic similarity tasks. The idea is to represent sentences as dependency trees to capture structural information, then apply graph neural network techniques to learn relationships between sentence pairs.
This project extends Graph Matching Networks to operate on linguistic trees and includes two main components:
- TMN_DataGen: A package for generating and processing dependency trees from raw text
- Tree-Matching-Networks: The model implementation for training and inference
.
βββ GMN/ # Original Graph Matching Networks code
βββ LinguisticTrees/ # My tree adaptations and training code
β βββ configs/ # Configuration files
β βββ data/ # Data loading and processing
β βββ models/ # Model architecture
β βββ training/ # Training and evaluation code
β βββ experiments/ # Training and evaluation scripts
βββ scripts/ # Demo and utility scripts
-
First, install TMN_DataGen:
git clone https://github.com/jlunder00/TMN_DataGen.git cd TMN_DataGen pip install .
-
Then, install this repository:
git clone https://github.com/jlunder00/Tree-Matching-Networks.git cd Tree-Matching-Networks pip install .
Before using the models, you'll need:
-
SpaCy Model: For dependency parsing.
python -m spacy download en_core_web_sm # or en_core_web_lg/md/trf -
Word2Vec Vocabulary: For word boundary correction.
- Download from Google News Vectors
- Set path in TMN_DataGen configuration
-
Embedding Cache: Create directory for caching word embeddings:
mkdir -p /path/to/embedding_cache
- Set path in configuration files
Try out the model with the demo script:
python -m Tree_Matching_Networks.scripts.demo \
--checkpoint /path/to/best_entailment_model_checkpoint/checkpoints/best_model.pt \
--config /path/to/custom/config.yaml \
--input input.tsv \
--spacy_model en_core_web_smNote that occasionally the provided config that comes with a checkpoint may not work in the demo script.
Providing a config override to an appropriately configured custom config or one such config from Tree_Matching_Networks/LinguisticTrees/configs/experiment_configs/ can resolve this issue.
See Demo Instructions for more details.
To process your own data, use TMN_DataGen:
python -m TMN_DataGen.run process \
--input_path your_data.jsonl \
--out_dir processed_data/your_dataset \
--dataset_type snli \
--spacy_model en_core_web_smSee TMN_DataGen README for more details.
Train a model on processed data:
python -m Tree_Matching_Networks.LinguisticTrees.experiments.train_aggregative \
--config Tree_Matching_Networks/LinguisticTrees/configs/experiment_configs/aggregative_config.yamlSee LinguisticTrees README for more configuration options.
Evaluate a trained model:
python -m Tree_Matching_Networks.LinguisticTrees.experiments.eval_aggregated \
--checkpoint /path/to/checkpoint \
--output_dir evaluation_results- Tree-Based Representation: Leverages dependency trees to capture sentence structure
- Cross-Graph Attention: Compares sentences using graph matching techniques
- Flexible Model Configuration: Supports different tasks and training approaches
- Contrastive Learning: Pretrain on large datasets for better transfer
- Multiple NLP Tasks: Supports entailment, similarity, and binary classification
My approach adapts Graph Matching Networks to work with linguistic trees:
- Text Processing: Convert sentences to dependency trees using SpaCy/DiaParser
- Feature Extraction: Embed words and dependency relations
- Graph Propagation: Use message passing to capture tree structure
- Graph Matching: Apply cross-graph attention to compare tree pairs
- Aggregation: Pool sentence trees into text-level representations
This project is MIT licensed, as found in the LICENSE file.
This project builds upon:
Yujia Li, Chenjie Gu, Thomas Dullien, Oriol Vinyals, Pushmeet Kohli. Graph Matching Networks for Learning the Similarity of Graph Structured Objects. ICML 2019. [paper]
Yijie Lin, Mouxing Yang, Jun Yu, Peng Hu, Changqing Zhang, Xi Peng. Graph Matching with Bi-level Noisy Correspondence. ICCV, 2023. [paper]