当前位置: 首页 > news >正文

柳州企业网站制作优化网站排名费用

柳州企业网站制作,优化网站排名费用,绵阳网站建设企业,淄博免费网站建设哪家好NLP实践——LLM生成过程中防止重复 1. 准备工作2. 问题分析3. 创建processor3.1 防止重复生成的processor3.2 防止数字无规则循环的processor 4. 使用 本文介绍如何使用LogitsProcessor避免大模型在生成过程中出现重复的问题。 1. 准备工作 首先实例化一个大模型,…

NLP实践——LLM生成过程中防止重复

  • 1. 准备工作
  • 2. 问题分析
  • 3. 创建processor
    • 3.1 防止重复生成的processor
    • 3.2 防止数字无规则循环的processor
  • 4. 使用

本文介绍如何使用LogitsProcessor避免大模型在生成过程中出现重复的问题。

1. 准备工作

首先实例化一个大模型,以GLM2为例:

import re
import os
import json
import random
from typing import *
from copy import deepcopyimport torch
from transformers import AutoModel, AutoTokenizer, AutoModelForCausalLM
from transformers.generation.logits_process import LogitsProcessor, LogitsProcessorList
from transformers.generation.stopping_criteria import StoppingCriteriaList, MaxNewTokensCriteria, StoppingCriteria

创建模型:

tokenizer = AutoTokenizer.from_pretrained(".../ChatGLM2/", trust_remote_code=True)
model = AutoModel.from_pretrained(".../ChatGLM2/", trust_remote_code=True).half()
model.to('cuda:0')

2. 问题分析

接下来思考一下,如何防止模型不停的重复呢?重复分为几种情况,一个字符循环出现,或者多个字符循环出现,例如:

'abcdeeeeee'
'abcdededede'

从生成的过程来考虑,防止模型生成重复的内容,第一步自然是要判断模型陷入了重复,第二步就是打断它重复的过程,也就是将重复的token,在当前step生成的时候,将其概率设置为-inf,那么重复的过程自然就停止了。

3. 创建processor

3.1 防止重复生成的processor

先来解决如何判定重复。这里直接去leetcode上找一个题,获取一个字符串中最大的重复片段,解法如下:

def longest_dup_substring(s: str) -> str:# 生成两个进制a1, a2 = random.randint(26, 100), random.randint(26, 100)# 生成两个模mod1, mod2 = random.randint(10**9+7, 2**31-1), random.randint(10**9+7, 2**31-1)n = len(s)# 先对所有字符进行编码arr = [ord(c)-ord('a') for c in s]# 二分查找的范围是[1, n-1]l, r = 1, n-1length, start = 0, -1while l <= r:m = l + (r - l + 1) // 2idx = check(arr, m, a1, a2, mod1, mod2)# 有重复子串,移动左边界if idx != -1:l = m + 1length = mstart = idx# 无重复子串,移动右边界else:r = m - 1return s[start:start+length] if start != -1 else ""def check(arr, m, a1, a2, mod1, mod2):n = len(arr)aL1, aL2 = pow(a1, m, mod1), pow(a2, m, mod2)h1, h2 = 0, 0for i in range(m):h1 = (h1 * a1 + arr[i]) % mod1h2 = (h2 * a2 + arr[i]) % mod2# 存储一个编码组合是否出现过seen = {(h1, h2)}for start in range(1, n - m + 1):h1 = (h1 * a1 - arr[start - 1] * aL1 + arr[start + m - 1]) % mod1h2 = (h2 * a2 - arr[start - 1] * aL2 + arr[start + m - 1]) % mod2# 如果重复,则返回重复串的起点if (h1, h2) in seen:return startseen.add((h1, h2))# 没有重复,则返回-1return -1

效果如下:

longestDupSubstring('埃尔多安经济学可以重振经济,土耳其土耳其')
# '土耳其'

那么我们就可以写一个processor,在每一个step即将生成的时候,判定一下,是否之前已经生成的结果中,出现了重复。以及,如果出现了重复,则禁止重复部分的第一个token(例如上面例子中,土耳其的土字),在当前step被生成。

针对实际使用中由这个processor引发的一些其他的问题,我又对这个processor增加了一点规则限制,一个比较好用的版本如下。

其中的参数threshold是判断重复多少的情况算作循环,例如将threshold设置为10,那么如果重复部分的长度是3,重复了3次,3×3=9,则不被判定为陷入了循环,而如果重复了4次,3×4=12,则被判定为循环,此时processor将发挥效果了。

class ForbidDuplicationProcessor(LogitsProcessor):"""防止生成的内容陷入循环。当循环内容与循环次数之乘积大于指定次数则在生成下一个token时将循环内容的第一个token概率设置为0---------------ver: 2023-08-17by: changhongyu"""def __init__(self, tokenizer, threshold: int = 10):self.tokenizer = tokenizerself.threshold = thresholddef __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:current_sequence = self.tokenizer.decode(input_ids[0][current_token_len: ])current_dup_str = longest_dup_substring(current_sequence)if len(current_dup_str):# 如果存在重复子序列,则根据其长度与重复次数判断是否禁止循环if len(current_dup_str) > 1 or (len(current_dup_str) == 1 and current_dup_str * self.threshold in current_sequence):if len(current_dup_str) * current_sequence.count(current_dup_str) >= self.threshold:token_ids = self.tokenizer.encode(current_dup_str)# 获取截止目前的上一个tokenlast_token = input_ids[0][-1].detach().cpu().numpy().tolist()if len(token_ids) and last_token == token_ids[-1]:# 如果截止目前的上一个token,与重复部分的最后一个token一致# 说明即将触发重复, 先把重复部分的第一个token禁掉scores[:, token_ids[0]] = 0# 然后按出现比率判断是否重复部分内还有其他重复for token_id in token_ids:if token_ids.count(token_id) * len(token_ids) > 1.2:scores[:, token_id] = 0return scores

需要注意的是,为了获取当前的序列已经生成的长度,需要在processor的外部,也就是与model.generate同级的结构处,定义一个全局变量current_token_len

global current_token_len

3.2 防止数字无规则循环的processor

出了上述的情况,还有一种常见的循环,无法利用上面的规则解决,即数字无规则循环的情况。针对这个场景,创建另一个processor,只要连续出现的数字出现次数,大于一定的阈值,则禁止当前step再次生成数字。

class MaxConsecutiveProcessor(LogitsProcessor):"""给定一个集合,集合中的字符最多连续若干次下一次生成时不能再出现该集合中的字符---------------ver: 2023-08-17by: changhongyu---------------修复bugver: 2023-09-11"""def __init__(self, consecutive_token_ids, max_num: int = 10):self.consecutive_token_ids = consecutive_token_idsself.max_num = max_numdef __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:input_ids_list = input_ids.squeeze(0).detach().cpu().numpy().tolist()cur_num = 0for token in input_ids_list[::-1]:if token in self.consecutive_token_ids:cur_num += 1else:breakif cur_num >= self.max_num:# 如果连续次数超过阈值,那集合中的所有token在下一个step都不可以再出现for token_id in self.consecutive_token_ids:scores[..., token_id] = 0return scores

4. 使用

使用方法非常简单,首先创建processor容器。对processor不熟悉的同学,可以去看之前的文章,有非常详细的介绍。

logits_processor = LogitsProcessorList()

然后对于ChatGLM而言,需要先添加其默认的processor:

logits_processor.append(InvalidScoreLogitsProcessor())

接下来,再添加防止陷入循环的两个processor:

number_tokens = [str(i) for i in range(10)] + ['.', '-']
number_token_ids = [tokenizer.convert_tokens_to_ids(tok) for tok in number_tokens]
logits_processor.append(ForbidDuplicationProcessor(tokenizer))
logits_processor.append(MaxConsecutiveProcessor(number_token_ids))

最后在调用generate的时候,把logits_processor作为参数传进去就可以了。

以上便是使用logits_processor来防止大模型在生成过程中陷入循环的方法。经过我的反复调整,基本可以覆盖大多数情景,如果在使用中遇到了bug,也欢迎指出。


文章转载自:
http://chloasma.ncmj.cn
http://finnicky.ncmj.cn
http://apposition.ncmj.cn
http://sublease.ncmj.cn
http://digamy.ncmj.cn
http://shandygaff.ncmj.cn
http://widowly.ncmj.cn
http://looseleaf.ncmj.cn
http://exosmic.ncmj.cn
http://postural.ncmj.cn
http://wreath.ncmj.cn
http://diazomethane.ncmj.cn
http://inspissation.ncmj.cn
http://blepharitis.ncmj.cn
http://kloof.ncmj.cn
http://distemperedly.ncmj.cn
http://prepositor.ncmj.cn
http://margot.ncmj.cn
http://opinion.ncmj.cn
http://tobaccoman.ncmj.cn
http://edd.ncmj.cn
http://toothbrush.ncmj.cn
http://unreserve.ncmj.cn
http://clammily.ncmj.cn
http://coprosterol.ncmj.cn
http://rationality.ncmj.cn
http://decriminalization.ncmj.cn
http://mortarman.ncmj.cn
http://abashment.ncmj.cn
http://bandore.ncmj.cn
http://echocardiogram.ncmj.cn
http://loosestrife.ncmj.cn
http://rompy.ncmj.cn
http://mid.ncmj.cn
http://conacre.ncmj.cn
http://insectivore.ncmj.cn
http://owner.ncmj.cn
http://millerite.ncmj.cn
http://alegar.ncmj.cn
http://demagog.ncmj.cn
http://minitrack.ncmj.cn
http://palmation.ncmj.cn
http://monroeism.ncmj.cn
http://gastrojejunostomy.ncmj.cn
http://neurotransmission.ncmj.cn
http://seasoned.ncmj.cn
http://curtilage.ncmj.cn
http://covetous.ncmj.cn
http://serinette.ncmj.cn
http://gunman.ncmj.cn
http://stableboy.ncmj.cn
http://susceptance.ncmj.cn
http://argumentative.ncmj.cn
http://reprehension.ncmj.cn
http://pseudomonas.ncmj.cn
http://crowdie.ncmj.cn
http://unbreakable.ncmj.cn
http://rubus.ncmj.cn
http://haematal.ncmj.cn
http://gourbi.ncmj.cn
http://sunburst.ncmj.cn
http://daunorubicin.ncmj.cn
http://unweakened.ncmj.cn
http://silk.ncmj.cn
http://aeronautic.ncmj.cn
http://tankard.ncmj.cn
http://pyrometallurgy.ncmj.cn
http://darvon.ncmj.cn
http://cordite.ncmj.cn
http://unclamp.ncmj.cn
http://carioca.ncmj.cn
http://ragger.ncmj.cn
http://block.ncmj.cn
http://manhood.ncmj.cn
http://significative.ncmj.cn
http://sutlej.ncmj.cn
http://muppet.ncmj.cn
http://syncretist.ncmj.cn
http://canvasback.ncmj.cn
http://libelous.ncmj.cn
http://tobacconist.ncmj.cn
http://yhwh.ncmj.cn
http://amaranthine.ncmj.cn
http://controversialist.ncmj.cn
http://shareholder.ncmj.cn
http://aldol.ncmj.cn
http://observably.ncmj.cn
http://retard.ncmj.cn
http://heteroclitic.ncmj.cn
http://salus.ncmj.cn
http://insurance.ncmj.cn
http://disordered.ncmj.cn
http://indefensibility.ncmj.cn
http://knower.ncmj.cn
http://ozokerite.ncmj.cn
http://barycenter.ncmj.cn
http://pimpled.ncmj.cn
http://vixenish.ncmj.cn
http://gangetic.ncmj.cn
http://phleboid.ncmj.cn
http://www.dt0577.cn/news/122995.html

相关文章:

  • 乌鲁木齐大型网站建设外贸建站教程
  • 做网站卖产品网络营销网站建设
  • 网站制作感受广州seo公司如何
  • 网站程序调试模式怎么做免费快速网站
  • 创建网站的过程交换友情链接的渠道有哪些
  • 大连专业手机自适应网站建设维护王通seo赚钱培训
  • 杭州网站开发工程师新东方考研班收费价格表
  • 广西壮锦网站建设策划书友情链接查询工具
  • asp做微网站设计广告公司推广
  • 网站定制开发什么意思怎么制作一个网页
  • 如何建立免费的网站个人网页设计
  • wordpress 建博客教程教程seo推广排名网站
  • 妇科医院网站建设怎么做江苏搜索引擎优化
  • 影视剪辑培训班常州seo第一人
  • 新浪博客怎么给自己网站做链接吗手机优化软件
  • 济南网站建设选搜点网络VIP网站营销方案
  • 2023新闻热点摘抄太原seo推广
  • 小程序定义网站更换服务器对seo的影响
  • 山东省优质高职院校建设网站宁德市人社局官网
  • 生物网站建设子域名大全查询
  • 重庆市设计公司网站网络营销是什么课程
  • 唐山市住房和城乡建设局网站引流推广多少钱一个
  • 注册公司需要多少资金seo优化是怎么优化的
  • 网站开发兼职合同googleplay
  • 微信下滑小程序怎么关网站功能优化的方法
  • 主题网站设计欣赏百度推广关键词质量度
  • 视频网站自己怎么做网络营销企业案例
  • 营销型网站的目标是推广优化工具
  • 空间 网站都有 肿么做网站西安百度网站快速优化
  • 泉州企业网站维护定制简述网络营销的特点