This repo contains the code and data of the following paper:
"Style Transfer from Non-Parallel Text by Cross-Alignment". Tianxiao Shen, Tao Lei, Regina Barzilay, and Tommi Jaakkola. NIPS 2017. arXiv
The method learns to perform style transfer between two non-parallel corpora. For example, given positive and negative reviews as two corpora, the model can learn to reverse the sentiment of a sentence.
Please name the corpora of two styles by "x.0" and "x.1" respectively, and use "x" to refer to them in options. Each file should consist of one sentence per line with tokens separated by a space.
The data/yelp/
directory contains an example Yelp review dataset.
- To train a model, first create a
tmp/
folder (where the model and results will be saved), then go to thecode/
folder and run the following command:
python style_transfer.py --train ../data/yelp/sentiment.train --dev ../data/yelp/sentiment.dev --output ../tmp/sentiment.dev --vocab ../tmp/yelp.vocab --model ../tmp/model
- To test the model, run the following command:
python style_transfer.py --test ../data/yelp/sentiment.test --output ../tmp/sentiment.test --vocab ../tmp/yelp.vocab --model ../tmp/model --load_model true --beam 8
-
To download a trained model, run
bash download_model.sh
, and then run the testing command with--vocab
and--model
options specifying../model/yelp.vocab
and../model/model
respectively. -
Check
code/options.py
for all running options.
Python >= 3.6, TensorFlow 1.3.0
As always it is a pain to manage versions of python and it's libraries.
This repo relies on tensorflow==1.3.0
which is available only for
python 3.6, so you will have to install it if you don't have it. In
order to run this code before proceeding with instructions above do the
following:
sudo apt update && sudo apt install python3.6
skip if you already have python3.6 installedpython3.6 -m venv venv
create virtual environment namedvenv
source venv/bin/activate
activate newly created environment (do this every time you want to run a code in this repo)pip install -r requirements.txt
(tested on Ubuntu 18).