<figure><img src="image_1.png" /></figure>

HAL
open science

# Masking based approach to detect llm generated text

Atmadeep Sengupta

► To cite this version:

Atmadeep Sengupta. Masking based approach to detect llm generated text. 2024. hal-04676730v2

HAL Id: hal-04676730

https://hal.science/hal-04676730v2

Preprint submitted on 26 Aug 2024

HAL is a multi-disciplinary open access
archive for the deposit and dissemination of sci-
entific research documents, whether they are pub-
lished or not. The documents may come from
teaching and research institutions in France or
abroad, or from public or private research centers.

L'archive ouverte pluridisciplinaire HAL, est
destinée au dépôt et à la diffusion de documents
scientifiques de niveau recherche, publiés ou non,
émanant des établissements d'enseignement et de
recherche français ou étrangers, des laboratoires
publics ou privés.

Public Domain
# Masking based approach to detect llm generated text

Atmadeep Sengupta

Indian Statistical Institute

Kolkata, India

atmadeepschool@gmail.com

## Abstract

The usage of large language models in the field of academics, news and research is increasing rapidly. With the advancement of the technology, we should also be aware of its potential threats. One of the most discussed problems is the detection of llm generated texts. Here, we present a masking based approach, which we call MaskGPT, in a black box setup, to detect a text is llm generated or not. We mask a proportion of the text, then fill the masks, and calculate the proportion of masks filled accurately. The underlying hypothesis is, llms are more likely to fill masks similar as the input text if the text is llm generated than the case of human generated texts. We proof this hypothesis empirically, and conclude this method is highly discriminative for big models like llama 70b and chatgpt 3.5.

## 1 Introduction

Large language models are growing fast and proven to give consistent and fluent responses for variety of tasks. Models like LLaMA 70b (Touvron et al., 2023), chatgpt (OpenAI, ) can answer almost any type of queries, ranging from science to describing images. With this advancement, a potential threat of generating fake news or propaganda using rises. The human-like phrasing of llms makes them attractive for malpractice in essay writing of academic writing. Human expert only perform as good as random guess in discrimination of model generated texts and human written texts (Gehrmann et al., 2019). So, the interest upon building a reliable machine detection technique is rapidly increasing. The most intuitive technique is calculating the log probability of the text and thresholding (Solaiman et al., 2019);

(Gehrmann et al., 2019); (Ippolito et al., 2020)). Some other techniques include watermarking (Xi et al., 2023), finding similarity (GPTZero)(Tian and Cui, 2023) and perturbations (DetectGPT) (Mitchell et al., 2023). Among these, DetectGPT is highly acclaimed and reported to give satisfactory results. Our algorithm revolve around their hypothesis, and modify and simplify to give better results. In MaskGPT, at first, we mask a proportion of words of the text, ask an llm to fill the masks, and then calculate the proportion of masks filled accurately, i.e. same as the original text. Our hypothesis is, in case of language model generated texts, masks will be filled more accurately than human written texts. We have achieved a betterment of ~ 0.05 in AUROC score than what reported in DetectGPT, while working with bigger language models, like gpt 3.5 and llama 70b.

## 2 Method

### 2.1 Task and settings

Our objective is zero shot detection of machine generated text, where this is a binary classification problem. Given a text x, our model should be able to identify if the text is language model generated or not.

Our setting is a black box model, i.e., we have no information about the source model. Unlike detectGPT, we don't have an access to any model to know the log probabilities. We only rely upon the responses generated by the detector language model.

### 2.2 Baseline by DetectGPT and FastDetectGPT

Let us denote the detector model as $p_{\theta}$ and the input passage x. If the predefined perturbation model is $q_{\phi}$, i.e. the model which replaces the masks, then DetectGPT works with the statistic

$$d(x, p_{\theta}, q_{\phi}) = \log(p_{\theta}(x)) - E_{\bar{x} \sim q_{\phi}(\cdot|x)}[\log(p_{\theta}(x))]$$
<figure><img src="image_2.png" /><figcaption>Figure 1: MaskGPT at a glance</figcaption></figure>

and infers x to be $p_{\theta}$ generated if $d > \epsilon$ for a threshold $\epsilon$. Here, they use T5 as there perturbation model. Improving their algorithm, FastDetectGPT(Bao et al., 2023), uses the source model in white box setup and GPT-J in black box setup, to generate the perturbations. Moreover, they use the conditional probabilities, and use the statistic

$$d(x, p_{\theta}, q_{\phi}) = \frac{\log(p_{\theta}(\bar{x}|x) - \bar{\mu})}{\bar{\sigma}}$$

where $\bar{\mu} = E_{\bar{x} \sim q_{\phi}(\bar{x}|x)}[\log p_{\theta}(\bar{x}|x)]$ and $\bar{\sigma}^2 = E_{\bar{x} \sim q_{\phi}(\bar{x}|x)}[\log p_{\theta}(\bar{x}|x) - \bar{\mu}]^2$

## 2.3 MaskGPT

We want to observe the statistic

$$d(x, p_{\theta}, q_{\phi}) = E(1_{x=q_{\phi}(x)})$$

where $1_A$ is the indicator random variable of A. Our statistic doesn't involve $p_{\theta}$ as we don't need a detector model. As our perturbation model, we used gemini-1.5-flash (Gemini team, google, 2024) through api and LLaMa-70b instead of smaller GPT models, which were suggested by FastDetectGPT. Also, instead of masking a sentence, as suggested by DetectGPT, we mask only a word, of some chosen dependency relations.<fnref n="1" /> We will study this hypothesis empirically.

# 3 Experiments

## 3.1 Dataset

We have worked with WritingPrompts dataset(Fan et al., 2018), and generated contents from gpt-3.5-turbo and llama 70b via api access. We have worked with 250 datapoints each.

## Algorithm 1 MaskGPT Algorithm

**Input:** passage x, perturbation model $q_{\phi}$, perturbation times t, proportion of perturbation p

**Output:** true- probably machine generated, false- probably human written

**function** MaskGPT(x, $q_{\phi}$, t, p)

**for** i in 1, 2, ..., t **do**

$$x_i = 1_{x==q_{\phi}(x,p)}$$

**end for**

**return** $\sum_t x_i$

**function** $q_{\phi}(x, p)$

**idx=** indexes to be masked, sampled uniformly

**for** i in idx **do**

$$x[idx]<mask>$$

**end for**

**return** query('fill the masks'+x)

<figure><img src="image_3.png" /><figcaption>Figure 2: Percentage of accurately replaced words</figcaption></figure>

<figure><img src="image_4.png" /><figcaption>Figure 3: Percentage of accurately replaced words</figcaption></figure>

<footnote n="1">¹The chosen dependency relations are acl, advcl, advmod, amod, compound, csubj, nmod, nsubj, obj. (Qi et al., 2020)</footnote>
<figure><img src="image_5.png" /><figcaption>Figure 4: Percentage of accurately replaced words</figcaption></figure>

<figure><img src="image_6.png" /><figcaption>Figure 5: Percentage of accurately replaced words</figcaption></figure>

## 3.2 Baseline

We will compare our results with similar zero-shot methods like DetectGPT, NPR (Su et al., 2023), and DNA GPT (Yang et al., 2023). Along with that, we will compare with traditional methods like log probability detection, log rank detection, entropy (Gehrmann et al., 2019); (Solaiman et al., 2019); (Ippolito et al., 2020)), LRR, Roberta base, large (Liu et al., 2019) and GPTZero. We don't have results of those methods for LLaMa 70b, so we compare those, with LLaMa2 13b. All the results, except the ones of MaskGPT, are due to Bao et al. 2023.

## 3.3 Evaluation metric

Like other zero shot methods, we here invoke area under ROC(AUROC) as our evaluation metric. We are not fixing any threshold, but trying to see the overlap between two authorships.

# 4 Main results

We here present the comparison table,

<table><caption>Table 1: AUROC comparison table</caption><thead><tr><th>Method</th><th>ChatGPT</th><th>LLaMa 70 b</th></tr></thead><tbody><tr><td>RoBERTa base</td><td>0.7084</td><td>NA</td></tr><tr><td>RoBERTa large</td><td>0.5480</td><td>NA</td></tr><tr><td>GPTZero</td><td>0.9292</td><td>NA</td></tr><tr><td>Likelihood</td><td>0.9740</td><td>0.8737</td></tr><tr><td>Entropy</td><td>0.1902</td><td>0.4702</td></tr><tr><td>LogRank</td><td>0.9656</td><td>0.9069</td></tr><tr><td>LRR</td><td>0.8958</td><td>0.9326</td></tr><tr><td>DNA-GPT</td><td>0.9425</td><td>NA</td></tr><tr><td>NPR</td><td>0.8924</td><td>0.9003</td></tr><tr><td>DetectGPT</td><td>0.8811</td><td>0.7532</td></tr><tr><td>FASTDetectGPT</td><td>0.9916</td><td>0.9085</td></tr><tr><td>MASKGPT</td><td>-</td><td>-</td></tr><tr><td>LLaMa 70b, p=20%</td><td>0.8778</td><td>0.8641</td></tr><tr><td>Gemini-1.5-flash, p=10%</td><td>0.9348</td><td>0.8640</td></tr><tr><td>Gemini-1.5-flash, p=20%</td><td>0.9632</td><td>0.8529</td></tr></tbody></table>

# 5 Conclusion and further works

As visible in the analysis, as a zero shot model without any major computation, we can discriminate llm generated and human written text with a good accuracy. Furthermore, many works can be done upon this model. Firstly, a thorough experiment with many models can lead us to the most accurate model for this task. Also, under changed top P or top K, study of this model can be extended. Given the largespread usage of language models, we should be more aware to its potential misuses.

# 6 Acknowledgement

The author would like to thank Prof. Dr. Utpal Garain for his suggestions and valuable insights about the topic.

# References

[Touvron et al.2023] Hugo Touvron, Thibaut Lavril, Gautier Izacard, Xavier Martinet, Marie-Anne Lachaux, Timothée Lacroix, Baptiste Rozière, Nathan Goyal, Eric Hambro, Faisal Azhar, Aurelien Rodriguez, Armand Joulin, Edouard Grave, Guillaume Lample 2023 LLaMA: Open and Efficient Foundation Language Models arXiv preprint arXiv:2302.13971.

[OpenAI] OpenAI API. 2024. ChatGPT. https://chat.openai.com.

[Gehrmann et al.2019] Sebastian Gehrmann, Hendrik Strobelt, Alexander M. Rush 2019. GLTR: Statis-
tical Detection and Visualization of Generated Text
*arXiv preprint arXiv:1906.04043.*

[Solaiman et al.2019] Irene Solaiman, Miles Brundage, Jack Clark, Amanda Askell, Ariel Herbert-Voss, Jeff Wu, Alec Radford, Gretchen Krueger, Jong Wook Kim, Sarah Kreps, Miles McCain, Alex Newhouse, Jason Blazakis, Kris McGuffie, Jasmine Wang 2019. Release Strategies and the Social Impacts of Language Models *arXiv preprint arXiv:1908.09203*.

[Ippolito et al. 2020] ACL Anthology. 2020. Automatic detection of generated text is easiest when humans are fooled Daphne Ippolito, Daniel Duckworth, Chris Callison-Burch, and Douglas Eck. In Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics, pp. 1808–1822 https://aclanthology.org/volumes/2020.acl-main/.

[Xi et al.2023] Xi Yang, Kejiang Chen, Weiming Zhang, Chang Liu, Yuang Qi, Jie Zhang, Han Fang, Nenghai Yu 2023. Watermarking Text Generated by Black-Box Language Models *arXiv preprint arXiv:2305.08883*.

[Tian and Cui2023] Edward Tian and Alexander Cui. 2023. GPTZero: Towards detection of AI-generated text using zero-shot and supervised methods. https://gptzero.me.

[Mitchell et al.2023] Eric Mitchell, Yoonho Lee, Alexander Khazatsky, Christopher D. Manning, Chelsea Finn 2023. DetectGPT: Zero-Shot Machine-Generated Text Detection using Probability Curvature. *Proceedings of the 40th International Conference on Machine Learning*.

[Bao et al.2023] Guangsheng Bao, Yanbin Zhao, Zhiyang Teng, Linyi Yang, Yue Zhang 2023. Fast-DetectGPT: Efficient Zero-Shot Detection of Machine-Generated Text via Conditional Probability Curvature *arXiv preprint arXiv:2310.05130*.

[Gemini Team Google 2024] Petko Georgiev, Ving Ian Lei, Ryan Burnell, Libin Bai, Anmol Gulati, Garrett Tanzer, Damien Vincent, Zhufeng Pan, Shibo Wang, Soroosh Mariooryad, Yifan Ding, Xinyang Geng, Fred Alcober, Roy Frostig, Mark Omernick, Lexi Walker, Cosmin Paduraru, Christina Sorokin, Andrea Tacchetti, Colin Gaffney, Samira Daruki, Olcan Sercinoglu, Zach Gleicher, Juliette Love, Paul Voigtlaender, Rohan Jain, Gabriela Surita, Kareem Mohamed, Rory Blevins, Junwhan Ahn, Tao Zhu, Kornaphop Kawintiranon, Orhan Firat, Yiming Gu, Yujing Zhang, Matthew Rahtz, Manaal Faruqui, Natalie Clay, Justin Gilmer, JD Co-Reyes, Ivo Penchev, Rui Zhu, Nobuyuki Morioka, Kevin Hui, Krishna Haridasan, Victor Campos, Mahdis Mahdieh, Mandy Guo, Samer Hassan, Kevin Kilgour, Arpi Vezer, Heng-Tze Cheng, Raoul de Liedekerke, Srdharth Goyal, Paul Barham, DJ Strouse, Seb Noury, Jonas Adler, Mukund Sundararajan, Sharad Vikram,

Dmitry Lepikhin, Michela Paganini, Xavier Garcia, Fan Yang, Dasha Valter, Maja Trebacz, Kiran Vodrahalli, Chulayuth Asawaroengchai, Roman Ring, Norbert Kalb, Livio Baldini Soares, Srdhartha Brahma, David Steiner, Tianhe Yu, Fabian Mentzer, Antoine He, Lucas Gonzalez, Bibo Xu, Raphael Lopez Kaufman, Laurent El Shafey, Junhyuk Oh, Tom Hennigan, George van den Driessche, Seth Odoom, Mario Lucic, Becca Roelofs, Sid Lall, Amit Marathe, Betty Chan, Santiago Ontanon, Luheng He, Denis Teplyashin, Jonathan Lai, Phil Crone, Bogdan Damoc, Lewis Ho, Sebastian Riedel, Karel Lenc, Chih-Kuan Yeh et al. (1017 additional authors not shown) 2024. Gemini 1.5: Unlocking multimodal understanding across millions of tokens of context *arXiv preprint arXiv:2403.05530*.

[Qi et al.2020] Peng Qi, Yuhao Zhang, Yuhui Zhang, Jason Bolton, and Christopher D. Manning. 2020. Stanza: A Python natural language processing toolkit for many human languages. In *Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics: System Demonstrations*. https://nlp.stanford.edu/pubs/qi2020stanza.pdf.

[Yang et al.2023] Xianjun Yang, Wei Cheng, Yue Wu, Linda Petzold, William Yang Wang, Haifeng Chen 2023. DNA-GPT: Divergent N-Gram Analysis for Training-Free Detection of GPT-Generated Text *arXiv preprint arXiv:2305.17359*.

[Su et al.2023] Jinyan Su, Terry Yue Zhuo, Di Wang, Preslav Nakov 2023. DetectLLM: Leveraging Log Rank Information for Zero-Shot Detection of Machine-Generated Text *arXiv preprint arXiv:2306.05540*.

[Liu et al.2019] Yinhan Liu, Myle Ott, Naman Goyal, Jingfei Du, Mandar Joshi, Danqi Chen, Omer Levy, Mike Lewis, Luke Zettlemoyer, Veselin Stoyanov 2019. RoBERTa: A Robustly Optimized BERT Pretraining Approach *arXiv preprint arXiv:1907.11692*.

[Fan et al.2018] Angela Fan, Mike Lewis, and Yann Dauphin. 2018. Hierarchical neural story generation. In *Iryna Gurevych and Yusuke Miyao, editors, Proceedings of the 56th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)*, pages 889–898, Melbourne, Australia. Association for Computational Linguistics. https://aclanthology.org/P18-1082. 10.18653/v1/P18-1082.