README.md 2.34 KB
Newer Older
Rosanne Liu's avatar
Rosanne Liu committed
1
# Plug and Play Language Models: a Simple Approach to Controlled Text Generation
Julien Chaumond's avatar
readme  
Julien Chaumond committed
2

Rosanne Liu's avatar
Rosanne Liu committed
3
Authors: [Sumanth Dathathri](https://dathath.github.io/), [Andrea Madotto](https://andreamad8.github.io/), Janice Lan, Jane Hung, Eric Frank, [Piero Molino](https://w4nderlu.st/), [Jason Yosinski](http://yosinski.com/), and [Rosanne Liu](http://www.rosanneliu.com/)
Julien Chaumond's avatar
readme  
Julien Chaumond committed
4

Rosanne Liu's avatar
Rosanne Liu committed
5
This folder contains the original code used to run the Plug and Play Language Model (PPLM).
Julien Chaumond's avatar
readme  
Julien Chaumond committed
6

Rosanne Liu's avatar
Rosanne Liu committed
7
Paper link: https://arxiv.org/abs/1912.02164
Julien Chaumond's avatar
readme  
Julien Chaumond committed
8
9
10

Blog link: https://eng.uber.com/pplm

Rosanne Liu's avatar
Rosanne Liu committed
11
12
Please check out the repo under uber-research for more information: https://github.com/uber-research/PPLM

13
14
15
# Note

鈿狅笍 This project should be run with pytorch-lightning==1.0.4 which has a potential security vulnerability
Julien Chaumond's avatar
readme  
Julien Chaumond committed
16
17

## Setup
18
19
20

```bash
git clone https://github.com/huggingface/transformers && cd transformers
21
pip install .
22
pip install nltk torchtext # additional requirements.
23
cd examples/research_projects/pplm
24
```
Julien Chaumond's avatar
readme  
Julien Chaumond committed
25

26
## PPLM-BoW
Julien Chaumond's avatar
readme  
Julien Chaumond committed
27
28

### Example command for bag-of-words control
29
30

```bash
Rosanne Liu's avatar
Rosanne Liu committed
31
python run_pplm.py -B military --cond_text "The potato" --length 50 --gamma 1.5 --num_iterations 3 --num_samples 10 --stepsize 0.03 --window_length 5 --kl_scale 0.01 --gm_scale 0.99 --colorama --sample
Julien Chaumond's avatar
readme  
Julien Chaumond committed
32
33
34
```

### Tuning hyperparameters for bag-of-words control
35

36
1. Increase `--stepsize` to intensify topic control, and decrease its value to soften the control. `--stepsize 0` recovers the original uncontrolled GPT-2 model.
Julien Chaumond's avatar
readme  
Julien Chaumond committed
37
38
39
40
41
42
43
44

2. If the language being generated is repetitive (For e.g. "science science experiment experiment"), there are several options to consider: </br>
	a) Reduce the `--stepsize` </br>
	b) Increase `--kl_scale` (the KL-loss coefficient) or decrease `--gm_scale` (the gm-scaling term) </br>
	c) Add `--grad-length xx` where xx is an (integer <= length, e.g. `--grad-length 30`).</br>


## PPLM-Discrim
45

Julien Chaumond's avatar
readme  
Julien Chaumond committed
46
### Example command for discriminator based sentiment control
47
48

```bash
Rosanne Liu's avatar
Rosanne Liu committed
49
python run_pplm.py -D sentiment --class_label 2 --cond_text "My dog died" --length 50 --gamma 1.0 --num_iterations 10 --num_samples 10 --stepsize 0.04 --kl_scale 0.01 --gm_scale 0.95 --sample
Julien Chaumond's avatar
readme  
Julien Chaumond committed
50
51
52
```

### Tuning hyperparameters for discriminator control
53

54
1. Increase `--stepsize` to intensify topic control, and decrease its value to soften the control. `--stepsize 0` recovers the original uncontrolled GPT-2 model.
Julien Chaumond's avatar
readme  
Julien Chaumond committed
55
56

2. Use `--class_label 3` for negative, and `--class_label 2` for positive