Giter Club home page Giter Club logo

ar-tree's Introduction

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

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

Requirements

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

Preprocessing

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 pickle_glove.py --txt </path/to/840B.300d.txt> --pt </output/file/name>

Next we begin to prepare training corpus.

SNLI

  1. Download the SNLI 1.0 corpus.
  2. Preprocess the original SNLI corpus and create the cache file by the following command:
python snli/dump_dataset.py --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.

SST

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

Age

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

Train

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

  • snli/run_snli.sh to train on SNLI.
  • sst/run_sst2.sh to train on SST2.
  • sst/run_sst5.sh to train on SST5.
  • age/run_age.sh 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.

Test

You can run evaluate.py for testing:

python evaluate.py --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.

Acknowledgement

We refer to some codes of these repos:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.