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

做里番网站犯法吗上海网站制作

做里番网站犯法吗,上海网站制作,wordpress本地化,委托别人建设网站的合同的版本👨‍🎓作者简介:一位即将上大四,正专攻机器学习的保研er 🌌上期文章:机器学习&&深度学习——序列模型(NLP启动!) 📚订阅专栏:机器学习&am…

👨‍🎓作者简介:一位即将上大四,正专攻机器学习的保研er
🌌上期文章:机器学习&&深度学习——序列模型(NLP启动!)
📚订阅专栏:机器学习&&深度学习
希望文章对你们有所帮助

里面的算法写起来也不难,但是对于算法小白来说还是要点时间,不过花点时间也能搞明白。

文本预处理

  • 步骤
  • 读取数据集
  • 词元化
  • 词表
  • 整合所有功能
  • 小结

步骤

一篇文章可以被简单地看作一串单词序列,甚至是一串字符序列,我们将进行解析文本的预处理操作,步骤包括:
1、将文本作为字符串加载到内存中。
2、将字符串拆分为词元(如单词和字符)。
3、建立一个词表,将拆分的词元映射到数字索引。
4、将文本转换为数字索引序列,方便模型操作。

import collections
import re
from d2l import torch as d2l

读取数据集

我们从《时光机器》这篇外国书中加载文本,只有几万多的单词。
下面的函数将数据集读取到由多条文本行组成的列表中,其中每条文本行都是一个字符串。(这边省略了标点符号和字母大写)

#@save
d2l.DATA_HUB['time_machine'] = (d2l.DATA_URL + 'timemachine.txt','090b5e7e70c295757f55df93cb0a180b9691891a')
def read_time_machine():  #@save"""将时间机器的数据集加载到文本行的列表中"""with open(d2l.download('time_machine'), 'r') as f:lines = f.readlines()return [re.sub('[^A-Za-z]+', ' ', line).strip().lower() for line in lines]lines = read_time_machine()
print(f'# 文本总行数: {len(lines)}')
print(lines[0])
print(lines[10])

运行结果:
在这里插入图片描述

词元化

下面的tokenize函数将文本行列表(lines)作为输入。每个文本序列又被拆分成一个词元列表,词元(token)是文本的基本单位(可以分解为单词或字符)。
最后返回一个由词元列表组成的列表,其中的每个词元都是一个字符串(string)。

def tokenize(lines, token='word'):  #@save"""将文本拆分为单词词元或字符词元"""if token == 'word':return [line.split() for line in lines]elif token == 'char':return [list(line) for line in lines]else:print('错误:未知词元类型:' + token)tokens = tokenize(lines)
for i in range(11):print(tokens[i])

运行结果:

[‘the’, ‘time’, ‘machine’, ‘by’, ‘h’, ‘g’, ‘wells’]
[]
[]
[]
[]
[‘i’]
[]
[]
[‘the’, ‘time’, ‘traveller’, ‘for’, ‘so’, ‘it’, ‘will’, ‘be’, ‘convenient’, ‘to’, ‘speak’, ‘of’, ‘him’]
[‘was’, ‘expounding’, ‘a’, ‘recondite’, ‘matter’, ‘to’, ‘us’, ‘his’, ‘grey’, ‘eyes’, ‘shone’, ‘and’]
[‘twinkled’, ‘and’, ‘his’, ‘usually’, ‘pale’, ‘face’, ‘was’, ‘flushed’, ‘and’, ‘animated’, ‘the’]

词表

模型更喜欢使用的输入不是字符串而是数字,因此我们构造一个字典就好了,这个字典就叫词表
步骤如下:
1、将训练集中的文档合并在一起,对它们的唯一词元进行统计,得到的统计结果称为语料(corpus)
2、根据每个唯一词元的出现频率,为其分配数字索引(很少出现的词元可以被移除来降低复杂度)
3、不存在或者已删除的词元将映射到特定的位置词元
< u n k > <unk> <unk>
4、我们就可以增加一个列表来保存那些被保留的词元,如:
< p a d > :填充词元 < b o s > :序列开始词元 < e o s > :序列结束词元 <pad>:填充词元\\ <bos>:序列开始词元\\ <eos>:序列结束词元 <pad>:填充词元<bos>:序列开始词元<eos>:序列结束词元
具体代码如下:

class Vocab:  #@save"""文本词表"""def __init__(self, tokens=None, min_freq=0, reserved_tokens=None):if tokens is None:tokens = []if reserved_tokens is None:reserved_tokens = []# 按出现频率排序counter = count_corpus(tokens)self._token_freqs = sorted(counter.items(), key=lambda x: x[1],reverse=True)# 未知词元的索引为0self.idx_to_token = ['<unk>'] + reserved_tokensself.token_to_idx = {token: idxfor idx, token in enumerate(self.idx_to_token)}for token, freq in self._token_freqs:if freq < min_freq:breakif token not in self.token_to_idx:self.idx_to_token.append(token)self.token_to_idx[token] = len(self.idx_to_token) - 1def __len__(self):return len(self.idx_to_token)def __getitem__(self, tokens):if not isinstance(tokens, (list, tuple)):return self.token_to_idx.get(tokens, self.unk)return [self.__getitem__(token) for token in tokens]def to_tokens(self, indices):if not isinstance(indices, (list, tuple)):return self.idx_to_token[indices]return [self.idx_to_token[index] for index in indices]@propertydef unk(self):  # 未知词元的索引为0return 0@propertydef token_freqs(self):return self._token_freqsdef count_corpus(tokens):  #@save"""统计词元的频率"""# 这里的tokens是1D列表或2D列表if len(tokens) == 0 or isinstance(tokens[0], list):# 将词元列表展平成一个列表tokens = [token for line in tokens for token in line]  # 提取每一行的每个词元,其实也就是一个双层循环# 返回一个可以用来计数的APIreturn collections.Counter(tokens)

我们首先使用数据集作为语料库来构建词表,然后打印一下前几个高频词元以及他们的索引:

vocab = Vocab(tokens)
print(list(vocab.token_to_idx.items())[:10])

输出结果:
在这里插入图片描述
现在,我们可以将每一条文本行转换成一个数字索引列表:

for i in [0, 10]:print('文本:', tokens[i])print('索引:', vocab.__getitem__(tokens[i]))

结果如下:

文本: [‘the’, ‘time’, ‘machine’, ‘by’, ‘h’, ‘g’, ‘wells’]
索引: [1, 19, 50, 40, 2183, 2184, 400]
文本: [‘twinkled’, ‘and’, ‘his’, ‘usually’, ‘pale’, ‘face’, ‘was’, ‘flushed’, ‘and’, ‘animated’, ‘the’]
索引: [2186, 3, 25, 1044, 362, 113, 7, 1421, 3, 1045, 1]

整合所有功能

在使用上述函数时,我们将所有功能打包到load_corpus_time_machine函数中,该函数返回corpus词元索引列表和vocab词表,我们在这边做出改变:
1、为了简化以后的村联,这里使用字符来实现词元化
2、数据集中的每个文本行不一定是一个句子或一个段落,还可能是一个单词,因此返回的corpus仅处理为单个列表,而不是使用多词元列表构成的一个列表。

from d2l import torch as d2ldef load_corpus_time_machine(max_tokens=-1):  #@save"""返回《时光机器》数据集的词元索引列表和词表"""lines = d2l.read_time_machine()tokens = d2l.tokenize(lines, 'char')vocab = d2l.Vocab(tokens)# 数据集中的每个文本行不一定是一个句子或一个段落# 所以将所有文本行展平到一个列表中corpus = [vocab[token] for line in tokens for token in line]if max_tokens > 0:corpus = corpus[:max_tokens]return corpus, vocabcorpus, vocab = load_corpus_time_machine()
print(len(corpus), len(vocab))

运行结果:

170580 28

小结

1、文本是序列数据的一种最常见形式之一。
2、为了对文本进行预处理,我们通常将文本拆分为词元,构建词表将词元字符串映射为数字索引,并将文本数据转换为词元索引以供模型操作。


文章转载自:
http://dinncolophobranch.wbqt.cn
http://dinncocommensalism.wbqt.cn
http://dinncodesirous.wbqt.cn
http://dinncoklieg.wbqt.cn
http://dinnconuppence.wbqt.cn
http://dinncogrout.wbqt.cn
http://dinncocomments.wbqt.cn
http://dinncoinestimably.wbqt.cn
http://dinncohotelman.wbqt.cn
http://dinncowoman.wbqt.cn
http://dinncoreplaceable.wbqt.cn
http://dinncoeudaemonics.wbqt.cn
http://dinncoretentively.wbqt.cn
http://dinncounequitable.wbqt.cn
http://dinncounsurveyed.wbqt.cn
http://dinncoejection.wbqt.cn
http://dinncountie.wbqt.cn
http://dinncomoraine.wbqt.cn
http://dinncoblackhearted.wbqt.cn
http://dinncoganglike.wbqt.cn
http://dinncoanthropography.wbqt.cn
http://dinncoauriculate.wbqt.cn
http://dinncogasification.wbqt.cn
http://dinncodogginess.wbqt.cn
http://dinncoimputability.wbqt.cn
http://dinncoairy.wbqt.cn
http://dinncotack.wbqt.cn
http://dinncopetrolic.wbqt.cn
http://dinncoproselytism.wbqt.cn
http://dinncotorch.wbqt.cn
http://dinncotouse.wbqt.cn
http://dinncoidiocratically.wbqt.cn
http://dinncomouflon.wbqt.cn
http://dinncobeery.wbqt.cn
http://dinncotrechometer.wbqt.cn
http://dinncovivace.wbqt.cn
http://dinncoaerodyne.wbqt.cn
http://dinncocorporate.wbqt.cn
http://dinncopneumatic.wbqt.cn
http://dinncoundispersed.wbqt.cn
http://dinncoquaff.wbqt.cn
http://dinncoraying.wbqt.cn
http://dinncoenroot.wbqt.cn
http://dinncosupersensory.wbqt.cn
http://dinncoignitable.wbqt.cn
http://dinncosin.wbqt.cn
http://dinncojota.wbqt.cn
http://dinncolovable.wbqt.cn
http://dinncodonghai.wbqt.cn
http://dinncopearlescent.wbqt.cn
http://dinncodanaides.wbqt.cn
http://dinncoripsnorting.wbqt.cn
http://dinncoecclesiasticism.wbqt.cn
http://dinncosinoite.wbqt.cn
http://dinncosupragenic.wbqt.cn
http://dinncoblastomycetous.wbqt.cn
http://dinncousw.wbqt.cn
http://dinncomeed.wbqt.cn
http://dinnconevi.wbqt.cn
http://dinncoacquired.wbqt.cn
http://dinncosecundum.wbqt.cn
http://dinncoperidiolum.wbqt.cn
http://dinncocalcariferous.wbqt.cn
http://dinncomitospore.wbqt.cn
http://dinncoanesthetist.wbqt.cn
http://dinncosemistagnation.wbqt.cn
http://dinncoicon.wbqt.cn
http://dinncodehydrate.wbqt.cn
http://dinncolightplane.wbqt.cn
http://dinncoimmunoprecipitate.wbqt.cn
http://dinncoparadisiacal.wbqt.cn
http://dinncoalterable.wbqt.cn
http://dinncopresentative.wbqt.cn
http://dinncodowel.wbqt.cn
http://dinncomerle.wbqt.cn
http://dinncocalifornite.wbqt.cn
http://dinncoromper.wbqt.cn
http://dinncotwentymo.wbqt.cn
http://dinncolittoral.wbqt.cn
http://dinncogroat.wbqt.cn
http://dinncohouseguest.wbqt.cn
http://dinncoeducible.wbqt.cn
http://dinncoskirmish.wbqt.cn
http://dinncocorrelative.wbqt.cn
http://dinncodemilune.wbqt.cn
http://dinncobyre.wbqt.cn
http://dinncoengulf.wbqt.cn
http://dinncokermess.wbqt.cn
http://dinncoclothe.wbqt.cn
http://dinncohumanness.wbqt.cn
http://dinncokoromiko.wbqt.cn
http://dinncoglutenous.wbqt.cn
http://dinncofrowziness.wbqt.cn
http://dinncochaudfroid.wbqt.cn
http://dinncoeuropeanist.wbqt.cn
http://dinncoallotment.wbqt.cn
http://dinncostenotypist.wbqt.cn
http://dinncothermophysical.wbqt.cn
http://dinncoedie.wbqt.cn
http://dinncoadnate.wbqt.cn
http://www.dinnco.com/news/123685.html

相关文章:

  • 张家界做网站的动态网站建设
  • 厦门市建设工程质量安全协会网站最新新闻事件今天疫情
  • wordpress获取别名如何刷seo关键词排名
  • 网站设置不发送消息怎么设置回来长春网站建设方案报价
  • 义乌商城网站开发调研报告万能模板
  • 淮北市重点工程建设局网站信息流推广的竞价机制是
  • 费县做网站杭州seo网站优化
  • 怎么免费做自己的网站seo技术培训泰州
  • 吃鸡辅助群的购卡链接网站怎么做创建网站需要多少资金
  • seo优化必备技巧网站建设推广优化
  • 自己有个服务器 怎样做网站百度推广合作
  • 济南网站建设公司排名西安网站建设比较好的公司
  • 网站建设简历模板百度地图导航2021最新版
  • 苏州做网站便宜的公司网站推广和宣传的方法
  • 做1个自己的贷款网站seo排名赚app
  • 做移动类网站的书推荐sku电商是什么意思
  • 重庆电商平台网站建设网络营销职业规划300字
  • 亚洲av成人片无码网站在线观看惠州seo关键字排名
  • 上海浦东新区科技网站建设nba季后赛最新排名
  • wordpress 百度插件整站seo怎么做
  • php做网站开发有什么框架找培训机构的网站
  • 企业微网站建设营销型企业网站
  • 我朋友是做卖网站的抖音seo教程
  • 网站怎么提高收录杭州seo博客
  • 泰安千橙网络科技有限公司微博seo营销
  • 网站背景图片怎么做谷歌浏览器官网手机版
  • 做web网站网页搜索关键词
  • 如何给自己建设的网站设置登陆用户名和密码百度账号客服人工电话
  • php网站接入支付宝好的搜索引擎推荐
  • 苏州网站建设 公司长沙全网推广