CN114510924A - 一种基于预训练语言模型的文本生成方法 - Google Patents

一种基于预训练语言模型的文本生成方法 Download PDF

Info

Publication number
CN114510924A
CN114510924A CN202210133050.1A CN202210133050A CN114510924A CN 114510924 A CN114510924 A CN 114510924A CN 202210133050 A CN202210133050 A CN 202210133050A CN 114510924 A CN114510924 A CN 114510924A
Authority
CN
China
Prior art keywords
style
current decoding
language model
text
decoding position
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Granted
Application number
CN202210133050.1A
Other languages
English (en)
Other versions
CN114510924B (zh
Inventor
冯骁骋
秦兵
顾宇轩
刘挺
马思成
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Harbin Institute of Technology
Original Assignee
Harbin Institute of Technology
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Harbin Institute of Technology filed Critical Harbin Institute of Technology
Priority to CN202210133050.1A priority Critical patent/CN114510924B/zh
Publication of CN114510924A publication Critical patent/CN114510924A/zh
Application granted granted Critical
Publication of CN114510924B publication Critical patent/CN114510924B/zh
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F40/00Handling natural language data
    • G06F40/20Natural language analysis
    • G06F40/205Parsing
    • G06F40/216Parsing using statistical methods
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/30Information retrieval; Database structures therefor; File system structures therefor of unstructured textual data
    • G06F16/35Clustering; Classification
    • G06F16/353Clustering; Classification into predefined classes
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F40/00Handling natural language data
    • G06F40/20Natural language analysis
    • G06F40/279Recognition of textual entities
    • G06F40/289Phrasal analysis, e.g. finite state techniques or chunking
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F40/00Handling natural language data
    • G06F40/30Semantic analysis

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • Artificial Intelligence (AREA)
  • Audiology, Speech & Language Pathology (AREA)
  • Computational Linguistics (AREA)
  • General Health & Medical Sciences (AREA)
  • Health & Medical Sciences (AREA)
  • Probability & Statistics with Applications (AREA)
  • Data Mining & Analysis (AREA)
  • Databases & Information Systems (AREA)
  • Machine Translation (AREA)

Abstract

一种基于预训练语言模型的文本生成方法,它属于自然语言处理技术领域。本发明解决了现有加权解码的可控生成方法无法在保证生成文本质量的前提下达到理想风格强度的问题。本发明提出了能够动态调节控制器权重的加权解码框架,该框架使用了一个规整模块,能够根据当前解码位置的语言模型信息对控制器的行为进行约束。如果当前解码位置不适合生成包含目标风格的词语时,规整模块会抑制控制器并使其失效;如果当前解码位置可以生成与目标风格相关的词语时,规整模块会增强控制器并确保在该解码位置生成的词语包含目标风格。本发明方法可以应用于可控文本生成。

Description

一种基于预训练语言模型的文本生成方法
技术领域
本发明属于自然语言处理技术领域,具体涉及一种基于预训练语言模型的可控文本生成方法。
背景技术
可控文本生成旨在生成一段与指定风格相关的文本(题目:ExploringControllable Text Generation Techniques,作者:Shrimai Prabhumoye,Alan W Black和Ruslan Salakhutdinov,年份:2020年,文献引自Proceedings of the 28thInternational Conference on Computational Linguistics)。随着生成式预训练语言模型的发展(题目:Language Models are Few-Shot Learners,作者:Tom Brown,BenjaminMann,Nick Ryder,Melanie Subbiah,Jared D Kaplan,Prafulla Dhariwal,ArvindNeelakantan,Pranav Shyam,Girish Sastry,Amanda Askell,Sandhini Agarwal,ArielHerbert-Voss,Gretchen Krueger,Tom Henighan,Rewon Child,Aditya Ramesh,DanielZiegler,Jeffrey Wu,Clemens Winter,Chris Hesse,Mark Chen,Eric Sigler,MateuszLitwin,Scott Gray,Benjamin Chess,Jack Clark,Christopher Berner,SamMcCandlish,Alec Radford,Ilya Sutskever和Dario Amodei,年份:2020年,文献引自Proceedings of the 34th Conference on Neural Information Processing Systems),PPLM提出了一种在解码阶段偏置语言模型生成结果的可控文本生成技术,我们将其与后续这类技术统称为加权解码。加权解码方法不需要修改语言模型的结构和参数,而是通过额外增加一个控制器在语言模型每一步解码时施加一个导向目标风格的偏置信号。加权解码方法可以通过调节控制器的权重来影响生成文本包含目标风格的程度,但是随着控制强度的缓慢增加,生成文本的流畅度会显著下降,导致模型实际上并不能在保证文本质量的前提下达到理想的风格强度。
发明内容
本发明的目的是为解决现有加权解码的可控生成方法无法在保证生成文本质量的前提下达到理想风格强度的问题,而提出的一种通过动态调节控制器权重来保证生成文本质量的可控生成方法。
本发明为解决上述技术问题所采取的技术方案是:一种基于预训练语言模型的文本生成方法,所述方法具体包括以下步骤:
步骤一、获取用于文本生成的基线模型;
步骤二、计算获取的基线模型中的预训练语言模型在当前解码位置下生成目标风格的倾向;
步骤三、根据预训练语言模型在当前解码位置下生成目标风格的倾向对控制器进行约束,得到在当前解码位置下的条件生成概率;
根据条件生成概率进行目标风格的文本生成。
本发明的有益效果是:
本发明提出了能够动态调节控制器权重的加权解码框架,该框架使用了一个规整模块,能够根据当前解码位置的语言模型信息对控制器的行为进行约束。如果当前解码位置不适合生成包含目标风格的词语时,规整模块会抑制控制器并使其失效;如果当前解码位置可以生成与目标风格相关的词语时,规整模块会增强控制器并确保在该解码位置生成的词语包含目标风格。
而且,本发明提出的框架适用于目前所有的四种加权解码方法:PPLM、Fudge、GeDi和DExperts,本发明在情感极性控制、主题控制和文本去毒任务上进行了实验,结果表明本发明的框架可以在保证生成文本风格强度的前提下显著提升文本质量。
附图说明
图1为本发明模型的示意图。
具体实施方式
具体实施方式一、结合图1说明本实施方式。本实施方式所述的一种基于预训练语言模型的文本生成方法,所述方法具体为:
步骤一、获取用于文本生成的基线模型;
步骤二、计算获取的基线模型中的预训练语言模型在当前解码位置下生成目标风格的倾向;
步骤三、根据预训练语言模型在当前解码位置下生成目标风格的倾向对控制器进行约束,得到在当前解码位置下的条件生成概率;
根据条件生成概率进行目标风格的文本生成。
当生成的文本出现语法错误,逻辑冲突和内容重复等影响流畅度的错误时,预训练语言模型在当前解码位置输出的概率分布与目标风格的分布不一致。因此本发明根据预训练语言模型在当前位置的概率分布,计算其与目标风格的差异程度来指导控制器的偏置程度。如果当前解码位置不适合生成包含目标风格的词语时,规整模块会抑制控制器并使其失效。如果当前解码位置可以生成与目标风格相关的词语时,规整模块会增强控制器并确保在该解码位置生成的词语包含目标风格。本发明方法可以在保证生成文本风格强度的前提下显著提升文本质量。
具体实施方式二:本实施方式是对具体实施方式一的进一步限定,所述基线模型为PPLM、Fudge、GeDi或DExperts。
基线模型PPLM(题目:Plug and play language models:A simple approach tocontrolled text generation,作者:Sumanth Dathathri,Andrea Madotto,Janice Lan,Jane Hung,Eric Frank,Piero Molino,Jason Yosinski和Rosanne Liu,年份:2020年,文献引自Proceedings of the 2020International Conference on LearningRepresentations)使用的控制器将生成文本X预测为风格a时交叉熵返回的梯度作为控制信号;
基线模型Fudge(题目:FUDGE:Controlled text generation with futurediscriminators,作者:Kevin Yang和Dan Klein,年份:2021年,文献引自Proceedings ofthe 2021 Conference of the North American Chapter of the Association forComputational Linguistics)使用的控制器把根据当前解码步骤将要生成的词语使得整个句子偏向目标风格的概率作为控制信号;
基线模型GeDi(题目:GeDi:Generative Discriminator Guided SequenceGeneration,作者:Ben Krause,Akhilesh Deepak Gotmare,Bryan McCann,NitishShirish Keskar,Shafiq Joty,Richard Socher和Nazneen Fatema Rajani,年份:2021年,文献引自Findings of the Association for Computational Linguistics:EMNLP 2021)训练了两个生成模型,分别输出当前解码步骤下属于目标风格的词语分布和不属于目标风格的词语分布,控制器的信号则是两种分布的比值;
基线模型DExperts(题目:DExperts:Decoding-time controlled textgeneration with experts and anti-experts,作者:Alisa Liu,Maarten Sap,XimingLu,Swabha Swayamdipta,Chandra Bhagavatula,NoahA.Smith和Yejin Choi,年份:2021年,文献引自Proceedings of the 59th Annual Meeting of the Association forComputational Linguistics and the 11th International Joint Conference onNatural Language Processing)训练了两个生成模型,控制器的信号则是两种分布的差值。
可控生成的任务可以形式化为:给定一个风格a,基线模型需要生成一个与目标风格相关的句子X,即建模条件概率P(X|a)。现有的加权解码方法利用贝叶斯公式,将条件概率转化为:P(X|a)∝P(X)P(a|X)λ,即预训练语言模型P(X)与权重为λ的控制器P(a|X)的乘积。在逐步解码时,预训练语言模型和控制器都只能根据已经生成的上文进行下一步生成,条件生成概率近似为
Figure BDA0003503464850000031
其中i是当前解码位置。
其它步骤及参数与具体实施方式一相同。
具体实施方式三:本实施方式是对具体实施方式二的进一步限定,所述基线模型使用的预训练语言模型为GPT2(题目:Language models are unsupervised multitasklearners,作者:Alec Radford,Jeffrey Wu,Rewon Child,David Luan,Dario Amodei和Ilya Sutskever,年份:2019年,文献引自OpenAI blog)。
其它步骤及参数与具体实施方式二相同。
具体实施方式四:本实施方式是对具体实施方式三的进一步限定,所述计算获取的基线模型中的预训练语言模型在当前解码位置下生成目标风格的倾向,其具体过程为:
给定主题风格a和主题风格a所对应的词表Wa={w1,w2,...,wk};
其中,wk为主题风格a中第k个主题相关的关键词;
根据预训练语言模型在当前解码位置i时产生的概率分布P(xi|x<i),则预训练语言模型在当前解码位置i下生成目标风格的倾向tH为:
Figure BDA0003503464850000041
其中,P(xi=w|x<i)为当前解码位置i下词表Wa中单词w出现概率,tH通过对当前解码位置i下词表Wa中每个单词出现概率进行求和得到。
其它步骤及参数与具体实施方式三相同。
具体实施方式五:本实施方式是对具体实施方式三的进一步限定,所述计算获取的基线模型中的预训练语言模型在当前解码位置下生成目标风格的倾向,其具体过程为:
给定属于风格a的文本集合为Da,不属于风格a的文本集合为Da′,count(u,Da)为一个n-gram词组u出现在文本集合Da中的次数,则词组u相对于风格a的重要程度分数score(u,a)为:
Figure BDA0003503464850000042
其中,∈是用于平滑的超参数,count(u,Da′)为词组u出现在文本集合Da′中的次数;
同理,分别得到各个n-gram词组相对于风格a的重要程度分数;
根据获得的重要程度分数对文本集合中的词组进行标注,并在基线模型中引入额外的attention层作为规整模块,规整模块的输出为:
K=Wk·HT
V=Wv·HT
c=softmax(Q·K)·V
其中,H为预训练语言模型对于给定上文计算出的隐状态表示向量,HT为H的转置,H=(h1,h2,...,hi),hi是预训练语言模型在当前解码位置i时计算出的隐状态表示,c是attention层计算出的整体表示;则
Figure BDA0003503464850000051
其中,Wk,Wv,W,Q均为可训练的参数矩阵,Wk,Wv,W都是线性层的参数,Q是attention机制中的Query模块的参数,K和V为中间变量,tT为在当前解码位置下生成目标风格的倾向,softmax(W·c)[n]代表取出softmax(W·c)结果中的第n维数值,n代表标注的第n类,n=1,2,…,10,n′代表第n类所对应分数区间的中位数。
例如,若类别1的概率softmax(W·c)[1]是n1,类别2的概率softmax(W·c)[2]是n2,类别3的概率softmax(W·c)[3]是n3,类别4的概率softmax(W·c)[4]是n4,类别5的概率softmax(W·c)[5]是n5,类别6的概率softmax(W·c)[6]是n6,类别7的概率softmax(W·c)[7]是n7,类别8的概率softmax(W·c)[8]是n8,类别9的概率softmax(W·c)[9]是n9,类别10的概率softmax(W·c)[10]是n10,则根据第1类所对应分数区间的中位数0.05、第2类所对应分数区间的中位数0.15、第3类所对应分数区间的中位数0.25、第4类所对应分数区间的中位数0.35、第5类所对应分数区间的中位数0.45、第6类所对应分数区间的中位数0.55、第7类所对应分数区间的中位数0.65、第8类所对应分数区间的中位数0.75、第9类所对应分数区间的中位数0.85、第10类所对应分数区间的中位数0.95计算出tT
tT=0.05*n1+0.15*n2+0.25*n3+…+0.95*n10
其它步骤及参数与具体实施方式三相同。
具体实施方式六:本实施方式是对具体实施方式五的进一步限定,所述根据获得的重要程度分数对文本集合中的词组进行标注,标注方法为:
将各个n-gram词组相对于风格a的重要程度分数正则化到(0,1]之间后,
若某个n-gram词组对应的正则化值在(0,0.1]之间,则将该n-gram词组标注为第1类;
若某个n-gram词组对应的正则化值在(0.1,0.2]之间,则将该n-gram词组标注为第2类;
若某个n-gram词组对应的正则化值在(0.2,0.3]之间,则将该n-gram词组标注为第3类;
若某个n-gram词组对应的正则化值在(0.3,0.4]之间,则将该n-gram词组标注为第4类;
若某个n-gram词组对应的正则化值在(0.4,0.5]之间,则将该n-gram词组标注为第5类;
若某个n-gram词组对应的正则化值在(0.5,0.6]之间,则将该n-gram词组标注为第6类;
若某个n-gram词组对应的正则化值在(0.6,0.7]之间,则将该n-gram词组标注为第7类;
若某个n-gram词组对应的正则化值在(0.7,0.8]之间,则将该n-gram词组标注为第8类;
若某个n-gram词组对应的正则化值在(0.8,0.9]之间,则将该n-gram词组标注为第9类;
若某个n-gram词组对应的正则化值在(0.9,1]之间,则将该n-gram词组标注为第10类。
词组的重要程度分数大于1时意味着它更有可能出现在风格相关的文本中,预处理阶段我们将所有风格文本数据中的词组对应的重要程度分数score(u,a)正则化到0-1之间后,将正则化后的值都标注在对应的词组上,分数越大意味着预训练语言模型在解码到该位置时更有可能生成风格相关的文本。由于分数本身是个连续的浮点数,模型在预测这个数字时很难训练好。因此在具体训练时将其转换为一个10分类问题来近似,其中每一个类别表示一个分数范围,这样不同的类别意味着当前位置与目标风格不同的相关程度。
其它步骤及参数与具体实施方式五相同。
具体实施方式七:本实施方式是对具体实施方式四的进一步限定,所述步骤三中,根据预训练语言模型在当前解码位置下生成目标风格的倾向对控制器进行约束,得到在当前解码位置下的条件生成概率;其具体过程为:
Figure BDA0003503464850000061
其中,P(X|a)为在当前解码位置下的条件生成概率,P(xi|x<i)为预训练语言模型在当前解码位置i时产生的概率分布,λ为超参数,f(a,P(x≤i)为在当前解码位置i时对控制器的约束函数,P(a|x<i)为控制器在当前解码位置i时产生的概率分布,N代表总的解码位置数;
控制器的控制信号由超参数λ进行加权,λ是在模型初始化时就固定住的,保留了其在整体层面调控风格控制强度的能力。预训练语言模型在当前解码位置i时产生的概率分布P(xi|x<i)是通过softmax(hi)计算得到的。
f(a,P(x≤i))=tHH
其中,τH为设定的阈值。
其它步骤及参数与具体实施方式四相同。
具体实施方式八:本实施方式是对具体实施方式六的进一步限定,所述步骤三中,根据预训练语言模型在当前解码位置下生成目标风格的倾向对控制器进行约束,得到在当前解码位置下的条件生成概率;其具体过程为:
Figure BDA0003503464850000071
其中,P(X|a)为在当前解码位置下的条件生成概率,P(xi|x<i)为预训练语言模型在当前解码位置i时产生的概率分布,λ为超参数,f(a,P(x≤i)为在当前解码位置i时对控制器的约束函数,P(a|x<i)为控制器在当前解码位置i时产生的概率分布,N代表总的解码位置数;
控制器的控制信号由超参数λ进行加权,λ是在模型初始化时就固定住的,保留了其在整体层面调控风格控制强度的能力。语言模型在当前解码位置i时产生的概率分布P(xi|x<i)是通过softmax(hi)计算得到的。
f(a,P(x≤i))=tTT
其中,τT为设定的阈值。
其它步骤及参数与具体实施方式六相同。
在具体实施方式七和具体实施方式八中,若当前位置不适合生成风格相关的单词时,约束函数输出一个值小于1的系数,这个系数最小可以为0,即当前位置暂时停止风格控制。若当前位置可以生成风格相关单词时,约束函数输出一个值大于或等于1的系数。
τH和τT则是对应的人工设置的阈值。控制生成的目标风格不同时,对应风格词所在的状态空间都不一样,风格词分布与常用词分布的重合程度也不同,因此阈值的设置也需要根据具体任务来调整。
对于情感极性控制任务,可以使用Yelp数据集,数据集中每一项是用户的评价和对应的情感分数。对于文本去毒任务,可以使用Jigsaw Unintended Bias in ToxicityClassification Kaggle Challenge数据集,该数据集收集了大量网络文本,每条数据包含一段话和这段话对应的毒性分数,如身份攻击,人身威胁,性别歧视等的程度。由于情感极性控制和文本去毒任务都有相关风格文本的数据集,因此,无论情感极性控制任务和文本去毒任务为有监督任务还是无监督任务,都可以采用具体实施方式七或具体实施方式八中的任一种方式对控制器进行约束。而对于主题控制任务,由于主题控制任务没有相关风格文本的数据集,每种主题只能使用一组主题词表来进行主题控制,主题包括军事主题,科技主题,政治主题等,因此主题控制任务只能为无监督任务,对主题控制任务只能采用具体实施方式七的控制器约束方式。
实施例一:
本发明对提出的模型进行了实现,在不同的任务上同时和目前的基线模型与通用语言模型进行了对比。
积极情感极性控制任务
基线模型一:PPLM
基线模型二:Fudge
基线模型三:GeDi
基线模型四:DExperts
(1)模型一:
(1.1)基线模型一生成的风格文本:
My dog died when I gave him a wonderful surprise gift!I was so happyto receive my wonderful gift!!I was so excited when my husband asked what Ihad in mind but when he told me how the gifts were,I thought it was just anormal surprise...
(1.2)本发明框架基于模型一生成的风格文本:
(1.2.1)本发明基于启发式规整模块的框架(即基于具体实施方式七的方式约束):
My dog died today.He was a wonderful,energetic and loving dog wholoved to help anyone who walked through life with joy and pride.I miss youand happiness,his friends forever in life.\n\nWe will love you forever,always...
(1.2.2)本发明基于可训练规整模块的框架(即基于具体实施方式八的方式约束):
My dog died!He was my hero!He was the only dog in the entire houseand I had a huge heart-wrenching,full-tilt.I am so very proud of this amazingdog.\n\nI've never seen this.My...
(2)模型二:
(2.1)基线模型二生成的风格文本:
My dog died yesterday.\n\nI love her wonderful personality and herpeople,and do everything I can to spread love for her on Facebook,Instagram,and her website.I've been receiving messages about her death from all over;everywhere...
(2.2)本发明框架基于模型二生成的风格文本:
(2.2.1)本发明基于启发式规整模块的框架:
My dog died in a beautiful,easy-going way that my children willcherish.They enjoyed the time I spent with them while she was there,and shedied in such a wonderful,loving way.Someone will miss her dearly...
(2.2.2)本发明基于可训练规整模块的框架:
My dog died yesterday.Amazing kid.\n\nShe is the best thing that hashappened to me.Her energy and poise have not worn away.I am blessed to havemet her forever so not just my dog but some of the best...
(3)模型三:
(3.1)基线模型三生成的风格文本:
My dog died a few weeks ago,and I recently watched this video.Notonly was I deeply moved by their love for each other,but much like the restof us,the grieving dogs showed the same beautiful loving behavior that makeslove so...
(3.2)本发明框架基于模型三生成的风格文本:
(3.2.1)本发明基于启发式规整模块的框架:
My dog died 2years ago.\n\nTallie died 2years ago.She was 4monthsold.I love her dearly and miss her so much.She is such a hardy little dogbecause she has a tough family life.She...
(3.2.2)本发明基于可训练规整模块的框架:
My dog died tonight at the age of 17.She was a total joy to bewith.She was so sweet,playful,loving,loving,cuddle tender,happy and so kindto all of those around her,all the time...
(4)模型四:
(4.1)基线模型四生成的风格文本:
My dog died of diabetes after nearly two decades of treating myfamily with medication,but she took to it with such enthusiasm that ittouched others.She was always so thankful for life.\n\n"She brought smiles toour family,"Myra said...
(4.2)本发明框架基于模型四生成的风格文本:
(4.2.1)本发明基于启发式规整模块的框架:
My dog died and you were touched for that as well.He's been my mentorfor the past three years and in spite of not having a formal adoption orfoster homes,I am so grateful to have found him in a place so similar to...
(4.2.2)本发明基于可训练规整模块的框架:
My dog died today.He was a lovely little husky which we only knew asan"old husky friend".My husband and I bought him from a shelter and havesince been raising him very nicely.He is a very gentle one...
(5)通用语言模型生成的未受风格控制的文本:
My dog died.I don't know what happened to it but I have been toldthat she was sick,had seizures,and I had to give her food.I don't understandhow anyone could think that that wasn't a terrible thing to do...
实施例二:
军事主题风格控制任务
基线模型一:PPLM
基线模型二:Fudge
(1)模型一:
(1.1)基线模型一生成的风格文本:
The potato was a great food staple,and it was also one of the world'sfirst war weapons.The potato was the first weapon to make war possible,and itwas a major factor in the war of the warring states.\n\nThe potato war...
(1.2)本发明基于模型一和启发式规整模块的框架生成的风格文本:
The potato was a great food staple,and it was also one of the world'sfirst major crops.In 17th century England potatoes were grown in every regionof the country.It was also the main food source of the British navy duringthe Napoleonic and World War II periods.\n\nThe British navy began to rely onthe...
(2)模型二:
(2.1)基线模型二生成的风格文本:
The potato war began with the arrival of a British merchant ship,HMSTireless,in December 1915,carrying some 700,000tonnes of the tuber,anessential staple crop for many Native Americans.The HMS Tireless's cargoincluded a cache of...
(2.2)本发明基于模型二和启发式规整模块的框架生成的风格文本:
The potato-shaped bomb will reportedly destroy enemy planes as theweapon explodes after the detonator,which will also be fitted to themilitary's B61 fighter jets,hits the target.\n\nIt is also intended to beused against incoming missiles...
(3)通用语言模型生成的未受风格控制的文本:
The potato is a popular vegetable for its taste and healthbenefits.But it can be quite high in fat.It also contains cholesterol,so youshould avoid it if you want to lower your risk of heart attack and stroke.\"\n\nThe study...
实施例三:
文本去毒任务
基线模型一:PPLM
基线模型二:DExperts
(1)模型一:
(1.1)基线模型一生成的去毒文本:
The men started swearing at me,called me a"fucking idiot"in the videoabove,but there's no denying his performance here is one that any other teamwould be proud of.If you haven't heard of the guy,read on below for ourthoughts of his career...
(1.2)本发明基于模型一和启发式规整模块的框架生成的去毒文本:
The men started swearing at me,called me a liar and said their bosslied to them to get their money back.After my story was published online,awoman told me I had broken the law by publishing it.It wasn't a lie.I had notbroken any...
(2)模型二:
(2.1)基线模型二生成的去毒文本:
The men started swearing at me,called me a racist name,and threatenedme.They took me to an office for a mental examination and they told me Icould have no contact with my son because I would relapse.They did it tome...
(2.2)本发明基于模型二和启发式规整模块的框架生成的去毒文本:
The men started swearing at me,called me names and told me to stoptalking.They said if we went back to the hotel room,we'd be arrested.Theysaid they'd find us in our underwear and we'd have to pay...
(3)通用语言模型生成的未经去毒的文本:
The men started swearing at me,called me a"nigger"and"cunt"and said,"You should have been shot."Then they began to beat me with a baseballbat.They kicked me,punched,kicked me,and punched me in the face.They said...
根据以上实施例可以看出,本发明的模型可以生成与基线模型相比更加风格相关且流畅的结果,通过动态调整不同解码位置控制强度的方法,可以更好的保障文本质量。
本发明的上述算例仅为详细地说明本发明的计算模型和计算流程,而并非是对本发明的实施方式的限定。对于所属领域的普通技术人员来说,在上述说明的基础上还可以做出其它不同形式的变化或变动,这里无法对所有的实施方式予以穷举,凡是属于本发明的技术方案所引伸出的显而易见的变化或变动仍处于本发明的保护范围之列。

Claims (8)

1.一种基于预训练语言模型的文本生成方法,其特征在于,所述方法具体包括以下步骤:
步骤一、获取用于文本生成的基线模型;
步骤二、计算获取的基线模型中的预训练语言模型在当前解码位置下生成目标风格的倾向;
步骤三、根据预训练语言模型在当前解码位置下生成目标风格的倾向对控制器进行约束,得到在当前解码位置下的条件生成概率;
根据条件生成概率进行目标风格的文本生成。
2.根据权利要求1所述的一种基于预训练语言模型的文本生成方法,其特征在于,所述基线模型为PPLM、Fudge、GeDi或DExperts。
3.根据权利要求2所述的一种基于预训练语言模型的文本生成方法,其特征在于,所述基线模型使用的预训练语言模型为GPT2。
4.根据权利要求3所述的一种基于预训练语言模型的文本生成方法,其特征在于,所述计算获取的基线模型中的预训练语言模型在当前解码位置下生成目标风格的倾向,其具体过程为:
给定主题风格a和主题风格a所对应的词表Wa={w1,w2,...,wk};
其中,wk为主题风格a中第k个主题相关的关键词;
则预训练语言模型在当前解码位置i下生成目标风格的倾向tH为:
Figure FDA0003503464840000011
其中,P(xi=w|x<i)为当前解码位置i下词表Wa中单词w出现概率。
5.根据权利要求3所述的一种基于预训练语言模型的文本生成方法,其特征在于,所述计算获取的基线模型中的预训练语言模型在当前解码位置下生成目标风格的倾向,其具体过程为:
给定属于风格a的文本集合为Da,不属于风格a的文本集合为Da′,count(u,Da)为一个n-gram词组u出现在文本集合Da中的次数,则词组u相对于风格a的重要程度分数score(u,a)为:
Figure FDA0003503464840000012
其中,∈是用于平滑的超参数,count(u,Da′)为词组u出现在文本集合Da′中的次数;
同理,分别得到各个n-gram词组相对于风格a的重要程度分数;
根据获得的重要程度分数对文本集合中的词组进行标注,并在基线模型中引入额外的attention层作为规整模块,规整模块的输出为:
K=Wk·HT
V=Wv·HT
c=softmax(Q·K)·V
其中,H为预训练语言模型对于给定上文计算出的隐状态表示向量,HT为H的转置,H=(h1,h2,...,hi),hi是预训练语言模型在当前解码位置i时计算出的隐状态表示,c是attention层计算出的整体表示;则
Figure FDA0003503464840000021
其中,Wk,Wv,W,Q均为参数矩阵,K和V为中间变量,tT为在当前解码位置下生成目标风格的倾向,softmax(W·c)[n]代表取出softmax(W·c)结果中的第n维数值,n代表标注的第n类,n=1,2,...,10,n′代表第n类所对应分数区间的中位数。
6.根据权利要求5所述的一种基于预训练语言模型的文本生成方法,其特征在于,所述根据获得的重要程度分数对文本集合中的词组进行标注,标注方法为:
将各个n-gram词组相对于风格a的重要程度分数正则化到(0,1]之间后,
若某个n-gram词组对应的正则化值在(0,0.1]之间,则将该n-gram词组标注为第1类;
若某个n-gram词组对应的正则化值在(0.1,0.2]之间,则将该n-gram词组标注为第2类;
若某个n-gram词组对应的正则化值在(0.2,0.3]之间,则将该n-gram词组标注为第3类;
若某个n-gram词组对应的正则化值在(0.3,0.4]之间,则将该n-gram词组标注为第4类;
若某个n-gram词组对应的正则化值在(0.4,0.5]之间,则将该n-gram词组标注为第5类;
若某个n-gram词组对应的正则化值在(0.5,0.6]之间,则将该n-gram词组标注为第6类;
若某个n-gram词组对应的正则化值在(0.6,0.7]之间,则将该n-gram词组标注为第7类;
若某个n-gram词组对应的正则化值在(0.7,0.8]之间,则将该n-gram词组标注为第8类;
若某个n-gram词组对应的正则化值在(0.8,0.9]之间,则将该n-gram词组标注为第9类;
若某个n-gram词组对应的正则化值在(0.9,1.0]之间,则将该n-gram词组标注为第10类。
7.根据权利要求4所述的一种基于预训练语言模型的文本生成方法,其特征在于,所述步骤三中,根据预训练语言模型在当前解码位置下生成目标风格的倾向对控制器进行约束,得到在当前解码位置下的条件生成概率;其具体过程为:
Figure FDA0003503464840000031
其中,P(X|a)为在当前解码位置下的条件生成概率,P(xi|x<i)为预训练语言模型在当前解码位置i时产生的概率分布,λ为超参数,f(a,P(x≤i)为在当前解码位置i时对控制器的约束函数,P(a|x<i)为控制器在当前解码位置i时产生的概率分布,N代表总的解码位置数;
f(a,P(x≤i))=tHH
其中,τH为设定的阈值。
8.根据权利要求6所述的一种基于预训练语言模型的文本生成方法,其特征在于,所述步骤三中,根据预训练语言模型在当前解码位置下生成目标风格的倾向对控制器进行约束,得到在当前解码位置下的条件生成概率;其具体过程为:
Figure FDA0003503464840000032
其中,P(X|a)为在当前解码位置下的条件生成概率,P(xi|x<i)为预训练语言模型在当前解码位置i时产生的概率分布,λ为超参数,f(a,P(x≤i)为在当前解码位置i时对控制器的约束函数,P(a|x<i)为控制器在当前解码位置i时产生的概率分布,N代表总的解码位置数;
f(a,P(x≤i))=tTT
其中,τT为设定的阈值。
CN202210133050.1A 2022-02-14 2022-02-14 一种基于预训练语言模型的文本生成方法 Active CN114510924B (zh)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202210133050.1A CN114510924B (zh) 2022-02-14 2022-02-14 一种基于预训练语言模型的文本生成方法

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202210133050.1A CN114510924B (zh) 2022-02-14 2022-02-14 一种基于预训练语言模型的文本生成方法

Publications (2)

Publication Number Publication Date
CN114510924A true CN114510924A (zh) 2022-05-17
CN114510924B CN114510924B (zh) 2022-09-20

Family

ID=81550942

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202210133050.1A Active CN114510924B (zh) 2022-02-14 2022-02-14 一种基于预训练语言模型的文本生成方法

Country Status (1)

Country Link
CN (1) CN114510924B (zh)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN114970524A (zh) * 2022-05-31 2022-08-30 北京深言科技有限责任公司 可控文本生成方法及装置

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20110288852A1 (en) * 2010-05-20 2011-11-24 Xerox Corporation Dynamic bi-phrases for statistical machine translation
CN106844345A (zh) * 2017-02-06 2017-06-13 厦门大学 一种基于参数线性约束的多任务分词方法
CN110390018A (zh) * 2019-07-25 2019-10-29 哈尔滨工业大学 一种基于lstm的社交网络评论生成方法
CN112598662A (zh) * 2020-12-30 2021-04-02 杭州电子科技大学 一种基于隐藏信息学习的图像美学描述生成方法
CN113204627A (zh) * 2021-05-13 2021-08-03 哈尔滨工业大学 利用DialoGPT作为特征标注器的对话摘要生成系统

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20110288852A1 (en) * 2010-05-20 2011-11-24 Xerox Corporation Dynamic bi-phrases for statistical machine translation
CN106844345A (zh) * 2017-02-06 2017-06-13 厦门大学 一种基于参数线性约束的多任务分词方法
CN110390018A (zh) * 2019-07-25 2019-10-29 哈尔滨工业大学 一种基于lstm的社交网络评论生成方法
CN112598662A (zh) * 2020-12-30 2021-04-02 杭州电子科技大学 一种基于隐藏信息学习的图像美学描述生成方法
CN113204627A (zh) * 2021-05-13 2021-08-03 哈尔滨工业大学 利用DialoGPT作为特征标注器的对话摘要生成系统

Non-Patent Citations (2)

* Cited by examiner, † Cited by third party
Title
KEVIN YANG ET AL.: "FUDGE :Controlled Text Generation With Future Discriminators", 《ARXIV:2104.05218V2 [CS.CL]》 *
XING WU ET AL.: "Mask and Infill: Applying Masked Language Model to Sentiment Transfer", 《ARXIV:1908.08039V1 [CS.CL]》 *

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN114970524A (zh) * 2022-05-31 2022-08-30 北京深言科技有限责任公司 可控文本生成方法及装置
CN114970524B (zh) * 2022-05-31 2024-02-02 北京深言科技有限责任公司 可控文本生成方法及装置

Also Published As

Publication number Publication date
CN114510924B (zh) 2022-09-20

Similar Documents

Publication Publication Date Title
Yang et al. FUDGE: Controlled text generation with future discriminators
Zhou et al. A dataset for document grounded conversations
Roberts Science fiction
Mao et al. Improving neural story generation by targeted common sense grounding
McCormack Wilde’s Fiction (s)
Kumar et al. Gradient-based constrained sampling from language models
US20210103700A1 (en) Systems and Methods for Generating and Recognizing Jokes
Ismailov Humor Analysis Based on Human Annotation Challenge at IberLEF 2019: First-place Solution.
Zheng et al. Click: Controllable text generation with sequence likelihood contrastive learning
Loller-Andersen et al. Deep Learning-based Poetry Generation Given Visual Input.
Murnane Ayn Rand and the posthuman: The mind-made future
CN114510924A (zh) 一种基于预训练语言模型的文本生成方法
Kumar et al. Constrained sampling from language models via langevin dynamics in embedding spaces
Wang et al. An animated picture says at least a thousand words: Selecting Gif-based Replies in Multimodal Dialog
Peng et al. Encoding Text Information By Pre-trained Model For Authorship Verification.
Case Around the table: Talking graciously about God
Hope Shakespeare and the english language
Amalia The representation of Alistair Maclean’s life as reflected in south by Java Head: a biographical study
Wilmot Great expectations: unsupervised inference of suspense, surprise and salience in storytelling
Di Carlo Figurative language in science popularisation: Similes as an explanatory strategy in TED talks
Lundy Text Mining Contemporary Popular Fiction: Natural Language Processing-Derived Themes Across Over 1,000 New York Times Bestsellers and Genre Fiction Novels
Goodare ‘I wasn’t open to notes’: S. Craig Zahler, Dragged Across Concrete (2018) and the 157-page screenplay
Croce et al. Learning to Generate Examples for Semantic Processing Tasks
Fang Towards Effective and Controllable Neural Text Generation
Piwowarski Information Society and the Protective Influence of Security Culture

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant