Skip to content
Switch branches/tags

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

Attentive Recursive Tree (AR-Tree)

This repository is the pytorch implementation of paper

Learning to Embed Sentences Using Attentive Recursive Trees.

Jiaxin Shi, Lei Hou, Juanzi Li, Zhiyuan Liu, Hanwang Zhang.

In this paper, we propose an Attentive Recursive Tree model (AR-Tree), where the words are dynamically located according to their importance in the task. Specifically, we construct the latent tree for a sentence in a proposed important-first strategy, and place more attentive words nearer to the root; thus, AR-Tree can inherently emphasize important words during the bottom-up composition of the sentence embedding. If you find this code useful in your research, please cite

author = {Jiaxin Shi, Lei Hou, Juanzi Li, Zhiyuan Liu, Hanwang Zhang},
title = {Learning to Embed Sentences Using Attentive Recursive Trees},
booktitle = {AAAI},
year = {2019}


  • python==3.6
  • pytorch==0.4.0
  • ete3
  • torchtext
  • nltk


Before training the model, you need to first prepare data. First of all, you need to download the GloVe 300d pretrained vector as we use it for initialization in all experiments. After unzipping it, you need to convert the txt file to pickle file by

python --txt </path/to/840B.300d.txt> --pt </output/file/name>

Next we begin to prepare training corpus.


  1. Download the SNLI 1.0 corpus.
  2. Preprocess the original SNLI corpus and create the cache file by the following command:
python snli/ --data </path/to/the/corpus> --out </path/to/the/output/file>

The output file will be used in the data loader when training or testing.


  1. Download the SST corpus. OK that's enough, the torchtext package will help us.


  1. We have attach this corpus as the file age/ You need to unzip it first.
  2. Create the cache file by the following command:
python age/ --glove-path </path/to/840B.300d.txt> --data-dir </path/to/unzipped/folder> --save-path </output/file/name>


You can directly run these scripts to train the AR-Tree on different datasets:

  • snli/ to train on SNLI.
  • sst/ to train on SST2.
  • sst/ to train on SST5.
  • age/ to train on Age. Note that you should change the argument value of --data-path, --glove-path, and --save-dir according to your directory.

We implement two training strategies, which can be specified by the argument --model-type. The reinforcement learning described in our paper is selected by --model-type RL. Another implementation is --model-type STG, which uses straight-through gumble softmax instead of REINFORCE. --model-type Choi corresponds to Choi's TreeLSTM model, regarded as a baseline in our paper.


You can run for testing:

python --ckpt </path/to/checkpoint> --data-path </path/to/data> --mode ['vis', 'val']

Note that --mode vis is used for visualization of the learned tree structures, while --mode val is to calculate the accuracy on the test set.


We refer to some codes of these repos:


Pytorch implementation of the paper "Learning to Embed Sentences Using Attentive Recursive Trees".




No releases published


No packages published