# CVDL final project
## Background & Motivations
現在LLM盛行, chatGPT也被大量使用, 從郵件內容到作業考試都有可能是這些model生成的, 所以我們想找到方法區別machine generated text跟human generated
## DetectGPT
[Github](https://github.com/eric-mitchell/detect-gpt)
[Implementation](https://github.com/BurhanUlTayyab/DetectGPT)
[Paper](https://arxiv.org/abs/2301.11305v1)
### Abstract & Intro
過往多數偵測machine generated text的方式是另外訓練一個network, 專門用來區分某個model產生的文本, 缺點顯而易見
* 容易overfit
* 每出現一個新的language model就需要重新訓練一個對應的detector
此論文採用[zero-shot](https://biic.ee.nthu.edu.tw/blog-detail.php?id=12)演算法來解決上述問題
僅僅利用generated text當中數個token的[log probability](https://en.wikipedia.org/wiki/Log_probability)平均值與threshold的比較, 就可以判斷text是機器產生或人類寫的
### Theory
source model : $p_{\theta}$
candidate passage : $x$
log probability of sample : $log(p_{\theta}(x))$
DetectGPT的假設是, 從source model $p_{\theta}$當中產生的samples, 通常都會位於$p_{\theta}$的log probability function的**negative curvature areas**
因此對於machine generated text來說, 如果把原本的sample $x$經過一點修改$x \sim p_{\theta}$ 成為 $\tilde{x}$, 則$log(p_{\theta}(x)) - log(p_{\theta}(\tilde{x}))$平均來說會比human-written text做相同的運算還要大很多
我們將perturbation清楚定義為一個function $q(\cdot | x)$, 事實上perturbation可以有幾種方式來實作
* 請人把文本當中某句話重寫, 文字意義要相同
* 用另外的model重新改寫內容
以下定義*perturbation descrepancy*
$d(x, p_{\theta}, q) = log(p_{\theta}(x)) - E_{\tilde{x} \sim q(\cdot | x)}log(p_{\theta}(\tilde{x}))$
有了$d(x, p_{\theta}, q)$, 我們可以更明確的定義我們的假設
* 如果$q$可以對sample的資料進行微調, 則對於$x\sim p_{\theta}$ 來說 $d(x, p_{\theta}, q) > 0$, 對於human-written text, $d(x, p_{\theta}, q)$則傾向靠近0
以上定理再透過設定threshold, 我們就可以輕易地判斷文本是機器產生還是人類寫的, 並且在實作上我們會做perturbation descrepancy做normalization
能使得$E_{\tilde{x} \sim q(\cdot | x)}log(p_{\theta}(\tilde{x}))$得到更好的結果, 會讓AUROC增加0.02

事實上perturbation descrepancy就是a measure of the local curvature of the log probability function near candidate passage
更精確的說, 它和log probability function的Hessian matrix的negative trace成正比
至於此篇論文計算matrix trace的方式稱為Hutchinson's trace estimator
$tr(A) = E_z z^TAz$, 其中$z \sim q_z$為 [IID](https://zh.wikipedia.org/zh-tw/%E7%8B%AC%E7%AB%8B%E5%90%8C%E5%88%86%E5%B8%83), $E[z_i] = 0 , Var(z_i) = 1$
要利用這個公式來計算Hessian matrix的trace的話, 我們必須計算$z^TH_{f}(x)z$的期望值
$z^TH_f(x)z \approx \cfrac{f(x+hz) + f(x-hz) - 2f(x)}{h^2}$
如果將上述公式結合並且假設$h=1$
$-tr(H)_f(x) = -E_zz^T(H_f(x))z \approx - E_z(f(x+hz) + f(x-hz) - 2f(x)) = 2f(x) - E_z[f(x+z) + f(x-z)]$
若假設noise distribution為symmetric, 也就是$p(z) = p(-z), \forall z$, 則可以將上式簡化為
$\cfrac{-tr(H)_f(x)}{2} \approx f(x) - E_zf(x+z)$
仔細觀察就可以發現$f(x) - E_zf(x+z)$正是perturbation descrepancy!
* $f(x)$ 換成 $log(p_{\theta(x)})$
* perturbation function $q(\tilde{x} | x)$換成Hutchinson's trace estimator中的distribution $q_z(z)$
* $\tilde{x}$為一個high-dimensional sequence of tokens, $q_z$則是a vector in a compact semantic space
我們可以將mask-filling model想像為先從semantic space當中做sampling, $\tilde{z} \sim q_z$, 接著再把它映射到一個token sequence, $\tilde{z} \mapsto \tilde{x}$
從semantic space當中做sample可以保證所有sample都靠近data manifold
## Test
按照[Github](https://github.com/BurhanUlTayyab/DetectGPT)的敘述把環境安裝好
改變infer.py當中的`sentence`
目前把chatgpt產生的文字丟進去還是都判斷成human-generated
chatgpt使用gpt3.5, 對於此專案的評估模型來說可能太強
利用gpt2產生的文本可以被判斷為AI generated
```python
import torch
from transformers import GPT2LMHeadModel, GPT2Tokenizer
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
model = GPT2LMHeadModel.from_pretrained('gpt2', pad_token_id=tokenizer.eos_token_id)
sequence = "How are you feeling today?"
inputs = tokenizer.encode(sequence, return_tensors='pt')
outputs = model.generate(inputs, max_length=200, do_sample=True, num_beams=5, no_repeat_ngram_size=2, early_stopping=True)
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(text)
```
## Resources
* [Awesome-Machine-Generated-Text
](https://github.com/ICTMCG/Awesome-Machine-Generated-Text#detection)
* [Fake-News-Detection-using-Twitter](https://github.com/sherylWM/Fake-News-Detection-using-Twitter)
* [Kaggle competition](https://www.kaggle.com/competitions/llm-detect-ai-generated-text)
## To-Do list
- [ ] Demo Video
- Generate machine texts and human texts
- run test
- [ ] Powerpoint
- Motivation
- Framework
- Flow diagram
- Data collection & Experiments
- Discussion and future work
- Reference