Skip to content

Repository files navigation

TextBox Logo


TextBox (妙笔)

“李太白少时,梦所用之笔头上生花后天才赡逸,名闻天下。”——王仁裕《开元天宝遗事·梦笔头生花》

PyPi Latest ReleaseReleaseDocumentation StatusLicense

Docs | Model | Dataset | Paper | 中文版

TextBox is developed based on Python and PyTorch for reproducing and developing text generation algorithms in a unified, comprehensive and efficient framework for research purpose. Our library includes 21 text generation algorithms, covering two major tasks:

  • Unconditional (input-free) Generation
  • Conditional (Seq2Seq) Generation, including Machine Translation, Text Summarization, Attribute-to-Text, and Dialogue Systems

We provide the support for 9 benchmark text generation datasets. A user can apply our library to process the original data copy, or simply download the processed datasets by our team.

TextBox v0.2 architecture
Figure: The Overall Architecture of TextBox

Feature

  • Unified and modularized framework. TextBox is built upon PyTorch and designed to be highly modularized, by decoupling diverse models into a set of highly reusable modules.
  • Comprehensive models, benchmark datasets and standardized evaluations. TextBox also contains a wide range of text generation models, covering the categories of VAE, GAN, RNN or Transformer based models, and pre-trained language models (PLM).
  • Extensible and flexible framework. TextBox provides convenient interfaces of various common functions or modules in text generation models, RNN encoder-decoder, Transformer encoder-decoder and pre-trained language model.
  • Easy and convenient to get started. TextBox provides flexible configuration files, which allows green hands to run experiments without modifying source code, and allows researchers to conduct qualitative analysis by modifying few configurations.

Installation

TextBox requires:

  • Python >= 3.6.2

  • torch >= 1.6.0. Please follow the official instructions to install the appropriate version according to your CUDA version and NVIDIA driver version.

  • GCC >= 5.1.0

Install from pip

pip install textbox

If you face a problem when installing fast_bleu, for Linux, please ensure GCC >= 5.1.0. For Windows, you can use the wheels in fast_bleu_wheel4windows for installation. For MacOS, you can install with the following command:

pip install fast-bleu --install-option="--CC=<path-to-gcc>" --install-option="--CXX=<path-to-g++>"

After installing fast_bleu successfully, just reinstall textbox.

Install from source

git clone https://github.com/RUCAIBox/TextBox.git &&cd TextBox
pip install -e . --verbose

Quick-Start

Start from source

With the source code, you can use the provided script for initial usage of our library:

python run_textbox.py

This script will run the RNN model on the COCO dataset to conduct unconditional generation. Typically, this example takes a few minutes. We will obtain the output log like example.log.

If you want to change the parameters, such as rnn_type, max_vocab_size, just set the additional command parameters as you need:

python run_textbox.py --rnn_type=lstm --max_vocab_size=4000

We also support to modify YAML configuration files in corresponding dataset and model properties folders and include it in the command line.

If you want to change the model, the dataset or the task type, just run the script by modifying corresponding command parameters:

python run_textbox.py --model=[model_name] --dataset=[dataset_name]

model_name is the model to be run, such as RNN and BART. Models we implemented can be found in Model.

If you want to change the datasets, please refer to Dataset.

Start from API

If TextBox is installed from pip, you can create a new python file, download the dataset, and write and run the following code:

fromtextbox.quick_startimportrun_textboxrun_textbox(config_dict={'model': 'RNN',
'dataset': 'COCO',
'data_path': './dataset'})

This will perform the training and test of the RNN model on the COCO dataset.

If you want to run different models, parameters or datasets, the operations are same with Start from source.

Use Pretrained Language Model

TextBox supports to apply part of pretrained language models (PLM) to conduct text generation. Take the GPT-2 for example, we will show you how to use PLMs to fine-tune.

  1. Download the GPT-2 model provided from Hugging Face (https://huggingface.co/gpt2/tree/main), including config.json, merges.txt, pytorch_model.bin, tokenizer.jsonand vocab.json. Then put them in a folder at the same level as textbox, such as pretrained_model/gpt2.

  2. After downloading, you just need to run the command:

python run_textbox.py --model=GPT2 --dataset=COCO \
--pretrained_model_path=pretrained_model/gpt2

Train with Distributed Data Parallel (DDP)

TextBox supports to train models with multiple GPUs conveniently. You don't need to modify the model, just run the following command:

python -m torch.distributed.launch --nproc_per_node=[gpu_num] \
run_textbox.py --model=[model_name] \
--dataset=[dataset_name] --gpu_id=[gpu_ids] --DDP=True

gpu_num is the number of GPUs you want to train with (such as 4), and gpu_ids is the usable GPU id list (such as 0,1,2,3).

Notice that: we only support DDP for end-to-end model. We will add support for non-end-to-end models, such as GAN, in the future.

Architecture

The above Figure presents the overall architecture of our library. The running procedure relies on some experimental configuration, obtained from the files, command line or parameter dictionaries. The dataset and model are prepared and initialized according to the configured settings, and the execution module is responsible for training and evaluating models. The details of interfaces can be obtained in our document.

Model

We implement 21 text generation models, covering unconditional generation and sequence-to-sequence generation, in the following table:

CategoryModelReference
VAELSTMVAE(Bowman et al., 2016)
CNNVAE(Yang et al., 2017)
HybridVAE(Semeniuta et al., 2017)
CVAE(Li et al., 2018)
GANSeqGAN(Yu et al., 2017)
TextGAN(Zhang et al., 2017)
RankGAN(Lin et al., 2017)
MaliGAN(Che et al., 2017)
LeakGAN(Guo et al., 2018)
MaskGAN(Fedus et al., 2018)
PLMGPT-2(Radford et al., 2019)
XLNet(Yang et al., 2019)
BERT2BERT(Rothe et al., 2020)
BART(Lewis et al., 2020)
T5(Raffel et al., 2020)
ProphetNet(Qi et al., 2020)
Seq2SeqRNN(Sutskever et al., 2014)
Transformer(Vaswani et al., 2017b)
Context2Seq(Tang et al., 2016)
Attr2Seq(Dong et al., 2017)
HRED(Serban et al., 2016)

Dataset

We have also collected 9 datasets that are commonly used for six tasks, which can be downloaded from Google Drive and Baidu Wangpan (Password: lwy6), including raw data and processed data.

We list the 9 datasets in the following table:

TaskDataset
UnconditionalImage COCO Caption
EMNLP2017 WMT News
IMDB Movie Review
TranslationIWSLT2014 German-English
WMT2014 English-German
SummarizationGigaWord
DialogPersona Chat
Attribute to TextAmazon Electronic
Poem GenerationChinese Classical Poetry Corpus
The downloaded dataset should be placed in the `dataset` folder, just as our main branch.

We also support you to run our model using your own dataset. Just follow the three steps:

  1. Create a new folder under the dataset folder to put your own corpus file which includes a sequence per line, e.g. dataset/YOUR_DATASET;

  2. Write a YAML configuration file using the same file name to set the hyper-parameters of your dataset, e.g. textbox/properties/dataset/YOUR_DATASET.yaml.

    If you want to splitted the dataset, please set split_strategy: "load_split" in the yaml, just as the COCO yaml or IWSLT14_DE_EN yaml.

    If you want to split the dataset by ratio automaticly, please set split_strategy: "by_ratio" and your desired split_ratio in the yaml, just as the IMDB yaml.

  3. For unconditional generation, name the corpus file corpus.txt if you set"by_ratio", name the corpus files train.txt, valid.txt, dev.txt if you set "load_split".

    For sequence-to-sequence generation, please name the corpus files train.[xx/yy], valid.[xx/yy], dev.[xx/yy], and the xx or yy is the suffix of the source or target file which should be consistent with source_suffix and target_suffix in the YAML.

Experiment Results

We have implemented various text generation models, and compared their performance on unconditional and conditional text generation tasks. We also show a few generated examples, and more examples can be found in generated_examples.

The following results were obtained from our TextBox in preliminary experiments. However, these algorithms were implemented and tuned based on our understanding and experiences, which may not achieve their optimal performance. If you could yield a better result for some specific algorithm, please kindly let us know. We will update this table after the results are verified.

Uncondition Generation

Image COCO Caption

Negative Log-Likelihood (NLL), BLEU and Self-BLEU (SBLEU) on test dataset:

ModelNLLBLEU-2BLEU-3BLEU-4BLEU-5SBLEU-2SBLEU-3SBLEU-4SBLEU-5
RNNVAE33.0280.4651.525.8911.5589.1861.5832.6914.03
CNNVAE36.610.630.270.280.293.100.280.290.30
HybridVAE56.4431.963.751.611.7677.7926.775.712.49
SeqGAN30.5680.1549.8824.9511.1084.4554.2627.4211.87
TextGAN32.4677.4745.7421.579.1882.9351.3424.4110.01
RankGAN31.0777.3645.0521.469.4183.1350.6223.7910.08
MaliGAN31.5080.0849.5224.0310.3684.8555.3228.2812.09
LeakGAN25.1193.4982.0362.5942.0689.7364.5735.6014.98
MaskGAN95.9358.0721.225.071.8876.1043.4120.069.37
GPT-226.8275.5158.8738.2221.6692.7875.4751.7432.39

Part of generated examples:

ModelExamples
RNNVAEpeople playing polo to eat in the woods .
LeakGANa man is standing near a horse on a lush green grassy field .
GPT-2cit a large zebra lays down on the ground.

EMNLP2017 WMT News

NLL, BLEU and SBLEU on test dataset:

ModelNLLBLEU-2BLEU-3BLEU-4BLEU-5SBLEU-2SBLEU-3SBLEU-4SBLEU-5
RNNVAE142.2358.8119.705.572.0172.7927.047.852.73
CNNVAE164.790.820.170.180.182.780.190.190.20
HybridVAE177.7529.581.620.470.4959.8510.31.431.10
SeqGAN142.2263.9020.895.641.8170.9725.567.052.18
TextGAN140.9060.3718.864.821.5268.3223.246.101.84
RankGAN142.2761.2819.815.581.8267.7123.156.632.09
MaliGAN149.9345.0012.693.161.1765.1020.555.411.91
LeakGAN162.7076.6139.1415.846.0885.0454.7029.3514.63
MaskGAN303.0063.0821.145.401.8083.9247.7919.967.51
GPT-288.0155.8821.655.341.4075.6736.7112.673.88

Part of generated examples:

ModelExamples
RNNVAElewis holds us in total because they have had a fighting opportunity to hold any bodies when companies on his assault .
LeakGANwe ' re a frustration of area , then we do coming out and play stuff so that we can be able to be ready to find a team in a game , but I know how we ' re going to say it was a problem .
GPT-2russ i'm trying to build a house that my kids can live in, too, and it's going to be a beautiful house.

IMDB Movie Review

NLL, BLEU and SBLEU on test dataset:

ModelNLLBLEU-2BLEU-3BLEU-4BLEU-5SBLEU-2SBLEU-3SBLEU-4SBLEU-5
RNNVAE445.5529.1413.734.811.8538.7714.396.615.16
CNNVAE552.091.880.110.110.113.080.130.130.13
HybridVAE318.4638.652.530.340.3170.0517.271.570.59
SeqGAN547.0966.3326.896.801.7972.4835.4811.603.31
TextGAN488.3763.9525.826.811.5172.1130.568.201.96
RankGAN518.1058.0823.716.841.6769.9331.6811.123.78
MaliGAN552.4544.5015.013.691.2357.2522.047.363.26
LeakGAN499.5778.9358.9632.5812.6592.9179.2160.1039.79
MaskGAN509.5856.6121.414.490.8692.0977.8859.6242.36
GPT-2348.6772.5241.7515.404.2286.2158.2630.0312.56

Part of generated examples (with max_length=100):

ModelExamples
RNNVAEbest brilliant known plot , sound movie , although unfortunately but it also like . the almost five minutes i will have done its bad numbers . so not yet i found the difference from with
LeakGANi saw this film shortly when I storms of a few concentration one before it all time . It doesn t understand the fact that it is a very good example of a modern day , in the <|unk|> , I saw it . It is so bad it s a <|unk|> . the cast , the stars , who are given a little
GPT-2be a very bad, low budget horror flick that is not worth watching and, in my humble opinion, not worth watching any time. the acting is atrocious, there are scenes that you could laugh at and the story, if you can call it that, was completely lacking in logic and

Sequence-to-Sequence Generation

GigaWord (Summarization)

ROUGE metric on test dataset using beam search (with beam_size=5):

ModelROUGE-1ROUGE-2ROUGE-LROUGE-W
RNN with Attention36.3217.6338.3625.08
Transformer36.2117.6438.1024.89
BART39.3420.0741.2527.13
BERT2BERT38.1618.8940.0626.21
ProphetNet38.4918.4139.8426.12
T538.8319.6840.7626.73
Part of generated examples:
Articlejapan 's nec corp. and computer corp. of the united states said wednesday they had agreed to join forces in supercomputer sales .
Gold Summarynec in computer sales tie-up
RNN with Attentionnec computer corp .
Transformernec computer to join forces in chip sales
BARTnec computer corp.
BERT2BERTnec computer form alliance for supercomputer sales
ProphetNetnec computer to join forces in supercomputer sales
T5nec computer to join forces in supercomputer sales

IWSLT2014 German-English (Translation)

BLEU metric on test dataset with three decoding strategies: top-k sampling, greedy search and beam search (with beam_size=5):

ModelStrategyBLEU-2BLEU-3BLEU-4BLEU
RNN with AttentionTop-k sampling26.6816.9510.8519.66
Greedy search33.7423.0315.7926.23
Beam search35.6824.9417.4228.23
TransformerTop-k sampling30.9620.8314.1623.91
Greedy search35.4824.7617.4128.10
Beam search36.8826.1018.5429.49
BARTBeam search29.0219.5813.4822.42
BERT2BERTBeam search27.6118.4112.4621.07
Part of generated examples:
Source (Germany)wissen sie , eines der großen < unk > beim reisen und eine der freuden bei der < unk > forschung ist , gemeinsam mit den menschen zu leben , die sich noch an die alten tage erinnern können . die ihre vergangenheit noch immer im wind spüren , sie auf vom regen < unk > steinen berühren , sie in den bitteren blättern der pflanzen schmecken .
Gold Target (English)you know , one of the intense pleasures of travel and one of the delights of < unk > research is the opportunity to live amongst those who have not forgotten the old ways , who still feel their past in the wind , touch it in stones < unk > by rain , taste it in the bitter leaves of plants .
RNN with Attentionyou know , one of the great < unk > trips is a travel and one of the friends in the world & apos ; s investigation is located on the old days that you can remember the past day , you & apos ; re < unk > to the rain in the < unk > chamber of plants .
Transformeryou know , one of the great < unk > about travel , and one of the pleasure in the < unk > research is to live with people who remember the old days , and they still remember the wind in the wind , but they & apos ; re touching the < unk > .

Persona Chat (Dialogue)

BLEU and distinct metrics on test dataset using beam search (with beam_size=5):

ModelDistinct-1Distinct-2BLEU-1BLEU-2BLEU-3BLEU-4
RNN with Attention0.240.7217.514.652.111.47
Transformer0.382.2817.294.852.321.65
HRED0.220.6317.294.722.201.60

Amazon Electronic (Attribute to text)

BLEU and distinct metrics on test dataset using beam search (with beam_size=5):

ModelDistinct-1Distinct-2BLEU-1BLEU-2BLEU-3BLEU-4
Context2Seq0.070.3917.212.800.830.43
Attr2Seq0.142.8117.142.810.870.48

Releases

ReleasesDateFeatures
v0.2.115/04/2021TextBox
v0.1.501/11/2021Basic TextBox

Contributing

Please let us know if you encounter a bug or have any suggestions by filing an issue.

We welcome all contributions from bug fixes to new features and extensions.

We expect all contributions discussed in the issue tracker and going through PRs.

We thank @LucasTsui0725 for contributing HRED model and @Richar-Du for CVAE model.

Reference

If you find TextBox useful for your research or development, please cite the following paper:

@article{textbox,
title={TextBox: A Unified, Modularized, and Extensible Framework for Text Generation},
author={Junyi Li, Tianyi Tang, Gaole He, Jinhao Jiang, Xiaoxuan Hu, Puzhao Xie, Wayne Xin Zhao, Ji-Rong Wen},
year={2021},
journal={arXiv preprint arXiv:2101.02046}
}

The Team

TextBox is developed and maintained by AI Box.

License

TextBox uses MIT License.

About

TextBox is an open-source library for building text generation system.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages