Tensorflow Seq2seq Text Summarization
This branch uses new tf.contrib.seq2seq APIs in tensorflow r1.1. For r1.0 users, please check Branch tf1.0
This is an implementation of sequence-to-sequence model using a bidirectional GRU encoder and a GRU decoder. This project aims to help people start working on Abstractive Short Text Summarization immediately. And hopefully, it may also work on machine translation tasks.
Please check harvardnlp/sent-summary.
If you want to train the model and have Nvidia GPUs (like GTX 1080, GTX Titan, etc), please setup CUDA environment and install tensorflow-gpu.
> pip3 install -U tensorflow-gpu==1.1
You can check whether the GPU works by
> python3 >>> import tensorflow >>>
and make sure there are no error outputs.
If you don't have a GPU, you can still use the pretrained models and generate summaries using your CPU.
> pip3 install -U tensorflow==1.1
Model and Data
Files should be organized like this.
Please find these files in the harvardnlp/sent-summary and rename them as
duc2003/input.txt -> test.duc2003.txt duc2004/input.txt -> test.duc2004.txt Giga/input.txt -> test.giga.txt
> python3 script/train.py can reproduce the experiments shown below.
By doing so, it will train 200k batches first. Then do generation on
[giga, duc2003, duc2004] with beam_size in
[1, 10] respectively every 20k batches. It will terminate at 300k batches. Also, the model will be saved every 20k batches.
> python3 script/test.py will automatically use the most updated model to do generation.
To do customized test, please put input data as
script/test.py line 13-14 from
datasets = ["giga", "duc2003", "duc2004"] geneos = [True, False, False]
datasets = ["your_test_name"] geneos = [True]
For advanced users,
python3 src/summarization.py -h can print help. Please check the code for details.
In tensorflow r0.11 and earlier, using bucketing is recommended. r1.0 provides dynamic rnn seq2seq framework which is much easier to understand than the tricky bucketing mechanism.
We use dynamic rnn to generate compute graph. There is only one computing graph in our implemention. However, we still split the dataset into several buckets and use data from the same bucket to create a batch. By doing so, we can add less padding, leading to a better efficiency.
The attention mechanism follows Bahdanau et. al.
We follow the implementation in tf.contrib.seq2seq. We refine the softmax function in attention so that paddings always get 0.
For simplicity and flexibility, we implement the beam search algorithm in python while leave the network part in tensorflow. In testing, we consider batch_size as beam_size. The tensorflow graph will generate only 1 word, then some python code will create a new batch according to the result. By iteratively doing so, beam search result is generated.
bigru_model.py for details.
We train the model for 300k batches with batch size 80. We clip all summaries to 75 bytes. For DUC datasets, we eliminate EOS and generate 12 words. For GIGA dataset, we let the model to generate EOS.
Negative Log Likelihood of Sentence
- Tensorflow r1.1
- Improve automatic scripts by parameterizing magic numbers.
- Some tricks caused by new tensorflow seq2seq framework.