Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
beyondguo committed Nov 19, 2022
1 parent 77941c6 commit 3f8e0cf
Show file tree
Hide file tree
Showing 50 changed files with 58 additions and 219 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ __pycache__/
.DS_Store
.vscode
augmentation_tools/__pycache__
augmentation_clf/sta_saved_keywords
augmentation_clf/__pycache__

# wordvec weights
weights/
Expand Down
62 changes: 44 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
# GENIUS: Sketch-based Language Model Pre-training via Extreme and Selective Masking for Text Generation and Augmentation
# 💡GENIUS: Generating text using sketches as input

**基于草稿的生成模型**
**基于草稿的文本生成模型**

**GENIUS** is a powerful conditional text generation model using sketches as input, which can fill in the missing contexts for a given **sketch** (key information consisting of textual spans, phrases, or words, concatenated by mask tokens). GENIUS uses an encoder-decoder structure (based on the BART architecture) and is pre-trained on the `C4-realnewslike` corpus.
- Paper: [GENIUS: Sketch-based Language Model Pre-training via Extreme and Selective Masking for Text Generation and Augmentation](https://github.com/beyondguo/genius/blob/master/GENIUS_gby_arxiv.pdf)

**GENIUS** can also be used as a **general textual data augmentation tool** for **various NLP tasks** (including sentiment analysis, topic classification, NER, and QA).
💡**GENIUS** is a powerful conditional text generation model using sketches as input, which can fill in the missing contexts for a given **sketch** (key information consisting of textual spans, phrases, or words, concatenated by mask tokens). GENIUS is pre-trained on a large- scale textual corpus with a novel *reconstruction from sketch* objective using an *extreme and selective masking* strategy, enabling it to generate diverse and high-quality texts given sketches.

**Example 1:**

- sketch: `__machine learning__my research interest__data science__`
- BART: `The machine learning aspect of my research interest in data science.`
- **GENIUS**: `I am a Ph.D. student in machine learning, and my research interest is in data science. I am interested in understanding how humans and machines interact and how we can improve the quality of life for people around the world.`

**Example 2:**

- sketch: `自然语言处理__谷歌__通用人工智能__`
- BART: `自然语言处理是谷歌的通用人工智能技术`
- **GENIUS**: `自然语言处理是谷歌在通用人工智能领域的一个重要研究方向,其目的是为了促进人类智能的发展。 `



**GENIUS** can also be used as a general textual **data augmentation tool** for **various NLP tasks** (including sentiment analysis, topic classification, NER, and QA).


![image-20221119164544165](https://cdn.jsdelivr.net/gh/beyondguo/mdnice_pictures/typora/hi-genius.png)

![genius-illustration](https://cdn.jsdelivr.net/gh/beyondguo/mdnice_pictures/typora/what-is-genius.png)

- Paper: [genius: SkEtch-based Generative Augmentation (preprint)](https://github.com/beyondguo/SEGA/blob/master/SEGA_gby_preprint.pdf)

- Models hosted in 🤗 Huggingface:

Expand All @@ -25,22 +41,29 @@

<img src="https://cdn.jsdelivr.net/gh/beyondguo/mdnice_pictures/typora/sega-hf-api.jpg" width="50%" />

**GENIUS** is able to write complete paragraphs given a sketch (or framework), which can be composed of:
- keywords /key-phrases, like "––NLP––AI––computer––science––"
- spans, like "Conference on Empirical Methods––submission of research papers––"
- sentences, like "I really like machine learning––I work at Google since last year––"
- or mixup~
## Usage

### What is a sketch?

First, what is a **sketch**? As defined in our paper, a sketch is "key information consisting of textual spans, phrases, or words, concatenated by mask tokens". It's like a draft or framework when you begin to write an article. With GENIUS model, you can input some key elements you want to mention in your wrinting, then the GENIUS model can generate cohrent text based on your sketch.

The sketch which can be composed of:

### How to use
#### 1. If you want to generate sentences given a **sketch**
- keywords /key-phrases, like `__NLP__AI__computer__science__`
- spans, like `Conference on Empirical Methods__submission of research papers__`
- sentences, like `I really like machine learning__I work at Google since last year__`
- or a mixup!


### How to use the model
#### 1. If you already have a sketch in mind, and want to get a paragraph based on it...
```python
from transformers import pipeline
# 1. load the model with the huggingface `pipeline`
genius = pipeline("text2text-generation", model='beyond/genius-large', device=0)
# 2. provide a sketch (joint by <mask> tokens)
sketch = "<mask> Conference on Empirical Methods <mask> submission of research papers <mask> Deep Learning <mask>"
# 3. just do it!
# 3. here we go!
generated_text = genius(sketch, num_beams=3, do_sample=True, max_length=200)[0]['generated_text']
print(generated_text)
```
Expand All @@ -49,15 +72,18 @@ Output:
'The Conference on Empirical Methods welcomes the submission of research papers. Abstracts should be in the form of a paper or presentation. Please submit abstracts to the following email address: eemml.stanford.edu. The conference will be held at Stanford University on April 1618, 2019. The theme of the conference is Deep Learning.'
```

#### 2. If you want to do **data augmentation** to generate new training samples
Please check [genius/augmentation_tools](https://github.com/beyondguo/genius/tree/master/augmentation_tools), where we provide ready-to-run scripts for data augmentation for text classification/NER/MRC tasks.
If you have a lot of sketches, you can batch-up your sketches to a Huggingface `Dataset` object, which can be much faster.

TODO: we are also building a python package for more convenient use of GENIUS, which will be released in few weeks.

#### 2. If you have an NLP dataset (e.g. classification) and want to do data augmentation to enlarge your dataset...

Please check [genius/augmentation_clf](https://github.com/beyondguo/genius/tree/master/augmentation_clf) and [genius/augmentation_ner_qa](https://github.com/beyondguo/genius/tree/master/augmentation_ner_qa), where we provide ready-to-run scripts for data augmentation for text classification/NER/MRC tasks.


---

## GENIUS as A Strong Data Augmentation Tool:
## Augmentation Experiments:
Data augmentation is an important application for natural language generation (NLG) models, which is also a valuable evaluation of whether the generated text can be used in real applications.
- Setting: Low-resource setting, where only n={50,100,200,500,1000} labeled samples are available for training. The below results are the average of all training sizes.
- Text Classification Datasets: [HuffPost](https://huggingface.co/datasets/khalidalt/HuffPost), [BBC](https://huggingface.co/datasets/SetFit/bbc-news), [SST2](https://huggingface.co/datasets/glue), [IMDB](https://huggingface.co/datasets/imdb), [Yahoo](https://huggingface.co/datasets/yahoo_answers_topics), [20NG](https://huggingface.co/datasets/newsgroup).
- Base classifier: [DistilBERT](https://huggingface.co/distilbert-base-cased)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
print('>>>After filtering: ',len(filtered_augmented_dataset['context']))


# 保存
# save
df_filter = pd.DataFrame(filtered_augmented_dataset)
print(len(df_filter))
df_filter.to_pickle(f'qa_data/squad_first{N_TRAIN}_aug{N_AUG}_v{v}_filtered.pkl')
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

# genius model
device = int(args.device)
model = 'beyond/genius-base'
model = 'beyond/genius-large'
genius = pipeline("text2text-generation", model=model, device=device)


Expand Down Expand Up @@ -115,7 +115,7 @@ def get_topk(s,max_k=8):
# m_pre_context = mask_unimportant_parts(pre_context, kws)
_, kws = sketch_extractor.get_kws(pre_context, aspect_keywords=[question], top=get_topk(pre_context))
m_pre_context = sketch_extractor.get_sketch_from_kws(pre_context, kws)
else: # 没有上文,补一个mask
else: # no pre-context, add a mask
m_pre_context = '<mask> '

# find and mask post-context
Expand All @@ -125,7 +125,7 @@ def get_topk(s,max_k=8):
# m_post_context = mask_unimportant_parts(post_context, kws)
_, kws = sketch_extractor.get_kws(post_context, aspect_keywords=[question], top=get_topk(post_context))
m_post_context = sketch_extractor.get_sketch_from_kws(post_context, kws)
else: # 没有下文,补一个mask
else: # no post-context, add a mask
m_post_context = ' <mask>'

# concatenate into a new context, and determine the new answer start
Expand All @@ -149,8 +149,8 @@ def get_topk(s,max_k=8):
print('** Working Hard to Augment Your Dataset......')
m_dataset = List2Dataset(m_contexts)
generated_contexts = []
for _ in range(N_AUG): # 增强多次
for out in tqdm(genius(m_dataset, num_beams=3, do_sample=True, max_length=200, length_penalty=2, batch_size=32,repetition_penalty=2.)): # 原来200, no repetition_penalty
for _ in range(N_AUG):
for out in tqdm(genius(m_dataset, num_beams=3, do_sample=True, max_length=200, length_penalty=2, batch_size=32,repetition_penalty=2.)):
generated_text = out[0]['generated_text']
generated_contexts.append(generated_text)

Expand All @@ -161,19 +161,20 @@ def get_topk(s,max_k=8):
try:
a_s_idx = c.index(a_s) # index of the answer sentence
except Exception as e:
# 一个严重的问题,原始的句子不一定会原封不动地输出,可能会有些微小变化
# 这样原来的answer sent就不一定找得到了,最好能用近似匹配,即重合率高于某阈值即可
# a problem using GENIUS is that the original input sentence may have small changes,
# resulting in the mismatch in output sequence
# therefore we calculate an overlap ratio to find the right sentence
sents = sent_tokenize(c)
for s in sents:
words = word_tokenize(s)
orig_words = word_tokenize(a_s)
n = len([w for w in words if w in orig_words])
# 重合率达到0.6,且answer也在该句子中,说明这个句子就对应原始答案句
# overlap > 0.6 and the answer is also in the sentence, then this is the right sentence we want
if n/len(words) > 0.6 and a in s:
a_s = s
a_s_idx = c.index(a_s)
break
if a_s_idx > -1: # 确认找到了答案句子
if a_s_idx > -1: # we've got the right answer
start = a_s_idx + a_s.index(a)
assert c[start:start+len(a)] == a, '%s Answer Position Mismatch!'%i

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
190 changes: 0 additions & 190 deletions ner_and_qa/ner_filter.py

This file was deleted.

2 changes: 1 addition & 1 deletion yake/yake.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

"""Main module."""
"""Main module."""

import string
import os
Expand Down

0 comments on commit 3f8e0cf

Please sign in to comment.