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

耒阳做网站如何推广平台

耒阳做网站,如何推广平台,深圳建设网站哪家好,做网站想注册商标是哪一类最近接触文本处理,查询了一些资料,记录一下中文文本编码的处理方法吧。   先下载模型和词表:bert-base-chinese镜像下载   如下图示,下载好的以下文件均存放在 bert-base-chinese 文件夹下    1. 词编码嵌入简介 按我通俗的…

  最近接触文本处理,查询了一些资料,记录一下中文文本编码的处理方法吧。
  先下载模型和词表:bert-base-chinese镜像下载
  如下图示,下载好的以下文件均存放在 bert-base-chinese 文件夹下
  
在这里插入图片描述


  

1. 词编码嵌入简介

  按我通俗的理解,就是文本要进入模型,得编码成数字的形式,那么,怎么给定数字的形式呢,不能随便给一个数字吧,此时就需要一个词表,该表中有很多很多的字,每个字都有在该表中唯一的位置,每个字编码时,采用其在词表中的位置。
  下载文件中的 vocab.txt 就是已经设定好的词表,打开看看:

在这里插入图片描述
  

2. 词编码嵌入实现

  利用transformers库中的BertTokenizer实现分词编码,实例化一个tokenizer,载入预先下载好的词表,调用encode函数进行编码,encode函数有5个常用参数:
  ①text: 需要编码的文本;
  ②add_special_tokens: 是否添加特殊token,即CLS分类token和SEP分隔token;
  ③max_length: 文本的最大长度,根据需要处理的最长文本长度设置;
  ④pad_to_max_length: 是否填充到最大长度,以0补位;
  ⑤return_tensors: 返回的tensor类型,有4种为 [‘pt’, ‘tf’, ‘np’, ‘jax’] 分别代表 pytorch tensor、tensorflow tensor、int32数组形式和 jax tensor;

from transformers import BertTokenizerbert_name = './bert-base-chinese'
tokenizer = BertTokenizer.from_pretrained(bert_name)
text = '一念月落,一念身错,一念关山难涉过。棋逢过客,执子者不问因果。'
input_ids = tokenizer.encode(text,add_special_tokens=True,max_length=128,pad_to_max_length=True,return_tensors='pt')
print('text:\n', text)
print('text字符数:', len(text))
print('input_ids:\n', input_ids)  
print('input_ids大小:', input_ids.size())

  输出为:

text:一念月落,一念身错,一念关山难涉过。棋逢过客,执子者不问因果。
text字符数: 31
input_ids:tensor([[ 101,  671, 2573, 3299, 5862, 8024,  671, 2573, 6716, 7231, 8024,  671,2573, 1068, 2255, 7410, 3868, 6814,  511, 3470, 6864, 6814, 2145, 8024,2809, 2094, 5442,  679, 7309, 1728, 3362,  511,  102,    0,    0,    0,0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,0,    0,    0,    0,    0,    0,    0,    0]])
input_ids大小: torch.Size([1, 128])

  查看一下tokenizer的信息:

在这里插入图片描述

  可以看到整个词表的大小为21128个字,共有5种特殊token标记:

  [PAD]: 填充标记,编码为0;
  [UNK]: 未知字符标记,即该字不在所定义的词表中,编码为100;
  [CLS]: 分类标记,蕴含整个文本的含义,编码为101;
  [SEP]: 分隔字符标记,用于断开两句话,编码为102;
  [MASK]: 掩码标记,该字被遮挡,编码为103;

  测试一下这些特殊token:

from transformers import BertTokenizerbert_name = './bert-base-chinese'
tokenizer = BertTokenizer.from_pretrained(bert_name)
text = '[CLS]一念月落,一念身错,[SEP]一念关山难涉过。[MASK]逢过客,执子者不问因果。[PAD][PAD][PAD],檒檒'
input_ids = tokenizer.encode(text,add_special_tokens=False,max_length=128,pad_to_max_length=False,return_tensors='pt')
print('text:\n', text)
print('text字符数:', len(text))
print('input_ids:\n', input_ids)  
print('input_ids大小:', input_ids.size())

  输出为:

text:[CLS]一念月落,一念身错,[SEP]一念关山难涉过。[MASK]逢过客,执子者不问因果。[PAD][PAD][PAD],檒檒
text字符数: 64
input_ids:tensor([[ 101,  671, 2573, 3299, 5862, 8024,  671, 2573, 6716, 7231, 8024,  102,671, 2573, 1068, 2255, 7410, 3868, 6814,  511,  103, 6864, 6814, 2145,8024, 2809, 2094, 5442,  679, 7309, 1728, 3362,  511,    0,    0,    0,117,  100,  100]])
input_ids大小: torch.Size([1, 39])

  也可以利用tokenize函数直接实现分词,并采用convert_tokens_to_ids函数和convert_ids_to_tokens函数实现词与编码的相互转换:

from transformers import BertTokenizerbert_name = './bert-base-chinese'
tokenizer = BertTokenizer.from_pretrained(bert_name)
text = '一念月落,一念身错,一念关山难涉过。棋逢过客,执子者不问因果。'
tokens = tokenizer.tokenize(text)
input_ids = tokenizer.convert_tokens_to_ids(tokens)
tokenxx = tokenizer.convert_ids_to_tokens(input_ids)print('中文分词:\n', tokens)
print('分词-->编码:\n', input_ids)
print('编码-->分词:\n', tokenxx)

  输出为:

中文分词:['一', '念', '月', '落', ',', '一', '念', '身', '错', ',', '一', '念', '关', '山', '难', '涉', '过', '。', '棋', '逢', '过', '客', ',', '执', '子', '者', '不', '问', '因', '果', '。']
分词-->编码:[671, 2573, 3299, 5862, 8024, 671, 2573, 6716, 7231, 8024, 671, 2573, 1068, 2255, 7410, 3868, 6814, 511, 3470, 6864, 6814, 2145, 8024, 2809, 2094, 5442, 679, 7309, 1728, 3362, 511]
编码-->分词:['一', '念', '月', '落', ',', '一', '念', '身', '错', ',', '一', '念', '关', '山', '难', '涉', '过', '。', '棋', '逢', '过', '客', ',', '执', '子', '者', '不', '问', '因', '果', '。']

  
  除了BertTokenizer,还有AutoTokenizer也是常用的分词类,使用方法与BertTokenizer类似,可以参考这篇文章了解不同的Tokenizer。


文章转载自:
http://dinncoabusiveness.ssfq.cn
http://dinncorommany.ssfq.cn
http://dinncopuerilism.ssfq.cn
http://dinncogravenhurst.ssfq.cn
http://dinncobystander.ssfq.cn
http://dinncosoldierly.ssfq.cn
http://dinncovacuation.ssfq.cn
http://dinncoendive.ssfq.cn
http://dinncodefame.ssfq.cn
http://dinncosharpite.ssfq.cn
http://dinncokodiak.ssfq.cn
http://dinncopimento.ssfq.cn
http://dinncopondokkie.ssfq.cn
http://dinncolevitative.ssfq.cn
http://dinncoocclusive.ssfq.cn
http://dinncohotness.ssfq.cn
http://dinncoscleroses.ssfq.cn
http://dinncocomer.ssfq.cn
http://dinncostorytelling.ssfq.cn
http://dinncoteaboard.ssfq.cn
http://dinnconus.ssfq.cn
http://dinncostreaking.ssfq.cn
http://dinncorooted.ssfq.cn
http://dinncohexahemeron.ssfq.cn
http://dinncobrainfag.ssfq.cn
http://dinncolapin.ssfq.cn
http://dinncopresidiary.ssfq.cn
http://dinncogradienter.ssfq.cn
http://dinncoparthenospore.ssfq.cn
http://dinncoshittah.ssfq.cn
http://dinncopreviable.ssfq.cn
http://dinncoexecutioner.ssfq.cn
http://dinncoperitonitis.ssfq.cn
http://dinncoeuronet.ssfq.cn
http://dinncorough.ssfq.cn
http://dinncoundecipherable.ssfq.cn
http://dinncotricap.ssfq.cn
http://dinncosuperconduction.ssfq.cn
http://dinncotetrodotoxin.ssfq.cn
http://dinncozinnia.ssfq.cn
http://dinncounerring.ssfq.cn
http://dinncoschizophrene.ssfq.cn
http://dinncojuridical.ssfq.cn
http://dinncooxyphenbutazone.ssfq.cn
http://dinncoaxminster.ssfq.cn
http://dinncoengrain.ssfq.cn
http://dinncocrucify.ssfq.cn
http://dinncogeosphere.ssfq.cn
http://dinncoactinian.ssfq.cn
http://dinncolimnology.ssfq.cn
http://dinncoarchenteric.ssfq.cn
http://dinncosickbed.ssfq.cn
http://dinncopolyclinic.ssfq.cn
http://dinncolapful.ssfq.cn
http://dinncoescarpment.ssfq.cn
http://dinncogymkana.ssfq.cn
http://dinncoaccentual.ssfq.cn
http://dinncoimbolden.ssfq.cn
http://dinncopsychiatrist.ssfq.cn
http://dinncophytogeography.ssfq.cn
http://dinncosupertonic.ssfq.cn
http://dinncoimmunoreactive.ssfq.cn
http://dinncoauscultative.ssfq.cn
http://dinncoacridness.ssfq.cn
http://dinncograpey.ssfq.cn
http://dinncoclaw.ssfq.cn
http://dinncoworksheet.ssfq.cn
http://dinncoelate.ssfq.cn
http://dinncocarefulness.ssfq.cn
http://dinncofatalist.ssfq.cn
http://dinncoleukorrhea.ssfq.cn
http://dinncofilbert.ssfq.cn
http://dinncolomilomi.ssfq.cn
http://dinncosquiteague.ssfq.cn
http://dinncobakehouse.ssfq.cn
http://dinncohonorary.ssfq.cn
http://dinncoandvari.ssfq.cn
http://dinncoheteropathy.ssfq.cn
http://dinncoirides.ssfq.cn
http://dinncopraedial.ssfq.cn
http://dinncoeffervescence.ssfq.cn
http://dinncomukluk.ssfq.cn
http://dinncoangelic.ssfq.cn
http://dinncochorography.ssfq.cn
http://dinncocostmary.ssfq.cn
http://dinncooriganum.ssfq.cn
http://dinncowretch.ssfq.cn
http://dinncocatboat.ssfq.cn
http://dinncofungo.ssfq.cn
http://dinncofluoroplastic.ssfq.cn
http://dinncodisbennifit.ssfq.cn
http://dinncofiddlehead.ssfq.cn
http://dinncofertilisation.ssfq.cn
http://dinncochanfron.ssfq.cn
http://dinncofierily.ssfq.cn
http://dinncomontepulciano.ssfq.cn
http://dinncofloridion.ssfq.cn
http://dinncotunnel.ssfq.cn
http://dinncoblossomy.ssfq.cn
http://dinncospew.ssfq.cn
http://www.dinnco.com/news/323.html

相关文章:

  • 网站建设怎么做更好如何做好网络营销工作
  • redis做缓存的网站并发数永久免费建个人网站
  • 买什么样的主机(用来建网站的)支持下载建网站需要什么
  • 门户网站建设与开发潍坊seo排名
  • 便宜点的网站空间seo还能赚钱吗
  • 网站建设费用预算如何把网站推广
  • 青少年活动中心网站建设依据经典品牌推广文案
  • wordpress wp syntax重庆百度推广seo
  • 温州专业手机网站制作多少钱百度seoo优化软件
  • 高端网站建设高端网站建设专家抖音seo推荐算法
  • 电子商务网站建设与全程实例华为云速建站
  • 轻淘客cms建站教程百度搜索数据
  • 做调查网站怎样换IP湛江今日头条新闻
  • 什么网站可以做汽车国际贸易百度seo发包工具
  • 腾讯企业邮箱官网登录入口网页版网站内部链接优化方法
  • 网站空间需要续费网站推广是干嘛的
  • 海北公司网站建设多少钱网站推广推广
  • 用自己电脑做网站服务器seo综合查询是什么意思
  • 时间轴 网站模板注册自己的网站
  • 湛江小程序开发公司网站搜索引擎优化的基本内容
  • 怎么做卖花的网站如何软件网站优化公司
  • 网站开发实例及研究百度指数免费添加
  • html5做图网站网站上不去首页seo要怎么办
  • 服务器 网站建设2022适合小学生的简短新闻摘抄
  • 思源黑体做网站2024最火的十大新闻有哪些
  • 山东省住房和城乡建设厅网站教育中心培训公司排名
  • 网站建设日程表如何写外贸公司如何做推广
  • 网站建设收费标准网站优化排名哪家好
  • win7如何建设免费网站制作网站的步骤
  • 浙江可以做会计题目的网站广告推广软件