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

做细胞激活的母液网站seo解释

做细胞激活的母液网站,seo解释,wordpress 子页面,如何用博客网站做cpa文章目录 1. 文本内容2. 字典构造2.1 定义一个类用于字典构造2.2 拆分文本2.3 构造结果 3. 完整代码 1. 文本内容 假如我们有如下一段文本内容: Optics It is the branch of physics that studies the behaviour and properties of light . Optical Science 这段…

文章目录

    • 1. 文本内容
    • 2. 字典构造
      • 2.1 定义一个类用于字典构造
      • 2.2 拆分文本
      • 2.3 构造结果
    • 3. 完整代码

1. 文本内容

假如我们有如下一段文本内容:

Optics

It is the branch of physics that studies the behaviour and properties of light .

Optical Science

  • 这段文本有5行,第一行内容为 ’Optics‘,第二行为空行,第三行内容为 ’It is the branch of physics that studies the behaviour and properties of light .‘, 第四行内容为空行,第五行内容为’Optical Science‘

  • 根据这段文本,可以构造一个字典。在这个字典中,每一个单词有一个编号( i n d e x \mathrm{index} index​),根据这个编号,我们就能知道这个编号对应哪个单词。

  • 将这段文本以 . t x t \mathrm{.txt} .txt 文件的形式放在 d a t a \mathrm{data} data 文件夹下。这里, . t x t \mathrm{.txt} .txt 文件和 d a t a \mathrm{data} data 文件夹都可以自己创建,如下图所示

    在这里插入图片描述

2. 字典构造

2.1 定义一个类用于字典构造

import os
from io import open
import torchclass Dictionary(object):def __init__(self):self.word2idx = {}self.idx2word = []def add_word(self, word):if word not in self.word2idx:self.idx2word.append(word)self.word2idx[word] = len(self.idx2word) - 1return self.word2idx[word]def __len__(self):return len(self.idx2word)
  • self.word2idx = {} 是建立一个空字典来存放每一个单词对应的 i n d e x \mathrm{index} indexself.idx2word = [] 是建立一个空列表来存放 i n d e x \mathrm{index} index 对应的单词;
  • 第二个函数 add_word 用来接收输入的文本数据,然后用 self.idx2word.append(word) 一个一个的放进 self.idx2word = [] 这个空列表里。self.word2idx[word] = len(self.idx2word) - 1 是为每一个加进来的单词分配一个 i n d e x \mathrm{index} index,然后 w o r d : i n d e x \mathrm{word:index} word:index 作为键值对放进self.word2idx = {} 建立的空字典里。
  • 第三个函数返回的是在这个字典中总共有多少个单词(包括标点符号,例如上面文本中的句号 ⋅ \cdot ​)。

2.2 拆分文本

D i c t i o n a r y \mathrm{Dictionary} Dictionary 这个类需要输入数据来产生词典,所以接下来要制作数据,这个数据来源就是 1 1 1 中的文本内容。这里,可以定义如下的一个 D a t a \mathrm{Data} Data 类:

import os
from io import open
import torchclass Data(object):def __init__(self, path):self.dictionary = Dictionary()self.demo = self.tokenize(os.path.join(path, 'demo_text.txt'))def tokenize(self, path):"""Tokenizes a text file."""assert os.path.exists(path)# Add words to the dictionarywith open(path, 'r', encoding="utf8") as f:for line in f:words = line.split() + ['<eos>']for word in words:self.dictionary.add_word(word)# Tokenize file contentwith open(path, 'r', encoding="utf8") as f:idss = []for line in f:words = line.split() + ['<eos>']ids = []for word in words:ids.append(self.dictionary.word2idx[word])idss.append(torch.tensor(ids).type(torch.int64))ids = torch.cat(idss)return ids
  • self.dictionary = Dictionary() 就是将 2.1 2.1 2.1 中构造的字典类实例化,以方便调用。self.demo = self.tokenize(os.path.join(path, 'demo_text.txt')) 是将 d e m o _ t e x t . t x t \mathrm{demo\_text.txt} demo_text.txt 中的内容转化为一个个的 i n d e x \mathrm{index} index​ 。
  • tokenize(self, path) 这个函数就是用来实现将 d e m o _ t e x t . t x t \mathrm{demo\_text.txt} demo_text.txt 中的内容转化为一个个的 i n d e x \mathrm{index} index​ 。
  • tokenize(self, path) 这个函数中,第一个 with open(path, 'r', encoding="utf8") as f: o p e n \mathrm{open} open 函数打开文本内容后,用 f o r \mathrm{for} for 循环,逐行拆分文本为一个个单词(包括标点符号),然后用 self.dictionary.add_word(word) 这个函数将每一个单词放进字典里。注意 words = line.split() + ['<eos>'] ,这里给每一行的末尾加了一个字符 ′ < e o s > ′ \mathrm{'<eos>'} <eos>​ 用于提示一行结束。
  • tokenize(self, path) 这个函数中,第二个 with open(path, 'r', encoding="utf8") as f: o p e n \mathrm{open} open 函数打开文本内容后,用 f o r \mathrm{for} for 循环,逐行拆分文本为一个个单词(包括标点符号),然后用 ids.append(self.dictionary.word2idx[word]) 这个函数将每一个单词对应的 i n d e x \mathrm{index} index​ 放进列表里。
  • idss.append(torch.tensor(ids).type(torch.int64)) 是将每一循环得到的 i d s \mathrm{ids} ids 存起来。
  • 因为每一循环得到 i d s \mathrm{ids} ids 是一个 t e n s o r \mathrm{tensor} tensor ,所以 i d s s \mathrm{idss} idss 里有很多个 t e n s o r \mathrm{tensor} tensor ,最后用 ids = torch.cat(idss) 把所有数据整合成一个 t e n s o r \mathrm{tensor} tensor​ 。

2.3 构造结果

输出字典代码如下:

data = Data('./data') # 给定数据文件夹
data_dict = data.dictionary.word2idx
print(f'由给定文本构造的词典为:\n{data_dict}')

输出结果如下:

由给定文本构造的词典为:
{'Optics': 0, '<eos>': 1, 'It': 2, 'is': 3, 'the': 4, 'branch': 5, 'of': 6, 'physics': 7, 'that': 8, 'studies': 9,
'behaviour': 10, 'and': 11, 'properties': 12, 'light': 13, '.': 14, 'Optical': 15, 'Science': 16}

对比原文本,可以发现,每一个单词有一个对应的编号,其中 '<eos>' 是我们主动添加的代表一行结束的字符。

由给定的文本产生的 i n d e x \mathrm{index} index​ 编码输出为:

data_demo = data.demo
print(f"给定文本所产生的index编码输出为:\n{data_demo}")
# 给定文本所产生的index编码输出为:
# tensor([ 0,  1,  1,  2,  3,  4,  5,  6,  7,  8,  9,  4, 10, 11, 12,  6, 13, 14,
#          1,  1, 15, 16,  1])
  • 第一个数字0代表 O p t i c s \mathrm{Optics} Optics, 第二个数字1代表 O p t i c s \mathrm{Optics} Optics 后的行结束符 '<eos>'
  • 第三个数字1代表空行里的结束符 '<eos>'
  • 第四个数字2代表第三行的第一个单词 I t \mathrm{It} It。 可以类比文本和 i n d e x \mathrm{index} index​ 的编码输出,都可以通过字典一一对应。
  • 这里的 i n d e x \mathrm{index} index 的编码输出就是用于 t r a n s f o r m e r \mathrm{transformer} transformer​ 的训练数据。

3. 完整代码

# %%
import os
from io import open
import torch# %% Dictionary
class Dictionary(object):def __init__(self):self.word2idx = {}self.idx2word = []def add_word(self, word):if word not in self.word2idx:self.idx2word.append(word)self.word2idx[word] = len(self.idx2word) - 1return self.word2idx[word]def __len__(self):return len(self.idx2word)# %% Data
class Data(object):def __init__(self, path):self.dictionary = Dictionary()self.demo = self.tokenize(os.path.join(path, 'demo_text.txt'))def tokenize(self, path):"""Tokenizes a text file."""assert os.path.exists(path)# Add words to the dictionarywith open(path, 'r', encoding="utf8") as f:for line in f:words = line.split() + ['<eos>']for word in words:self.dictionary.add_word(word)# Tokenize file contentwith open(path, 'r', encoding="utf8") as f:idss = []for line in f:words = line.split() + ['<eos>']ids = []for word in words:ids.append(self.dictionary.word2idx[word])idss.append(torch.tensor(ids).type(torch.int64))ids = torch.cat(idss)return ids# %%
data = Data('./data')  # 给定数据文件夹
data_dict = data.dictionary.word2idx
print(f'由给定文本构造的词典为:\n{data_dict}')
# 由给定文本构造的词典为:
# {'Optics': 0, '<eos>': 1, 'It': 2, 'is': 3, 'the': 4, 'branch': 5, 'of': 6, 'physics': 7, 'that': 8, 'studies': 9,
# 'behaviour': 10, 'and': 11, 'properties': 12, 'light': 13, '.': 14, 'Optical': 15, 'Science': 16}
data_demo = data.demo
print(f"给定文本所产生的index编码输出为:\n{data_demo}")
# 给定文本所产生的index编码输出为:
# tensor([ 0,  1,  1,  2,  3,  4,  5,  6,  7,  8,  9,  4, 10, 11, 12,  6, 13, 14,
#          1,  1, 15, 16,  1])

文章转载自:
http://dinncorufescent.tpps.cn
http://dinncotoxicology.tpps.cn
http://dinncoequable.tpps.cn
http://dinncoschradan.tpps.cn
http://dinncounvanquished.tpps.cn
http://dinncomesquit.tpps.cn
http://dinncotumbler.tpps.cn
http://dinncocanine.tpps.cn
http://dinncooffside.tpps.cn
http://dinncokan.tpps.cn
http://dinnconewsstand.tpps.cn
http://dinncoetherize.tpps.cn
http://dinncotranscendent.tpps.cn
http://dinncohousecleaning.tpps.cn
http://dinncopulpitry.tpps.cn
http://dinncocareful.tpps.cn
http://dinncogalvanoplastics.tpps.cn
http://dinncoteacherless.tpps.cn
http://dinncoskippingly.tpps.cn
http://dinncocyanohydrin.tpps.cn
http://dinncohomeland.tpps.cn
http://dinncoplasticise.tpps.cn
http://dinncolevee.tpps.cn
http://dinncosmerrebrxd.tpps.cn
http://dinncoheadshaking.tpps.cn
http://dinncoemile.tpps.cn
http://dinncohonewort.tpps.cn
http://dinncobroiling.tpps.cn
http://dinncoartotype.tpps.cn
http://dinncocentrally.tpps.cn
http://dinncoamaigamate.tpps.cn
http://dinncoapproximation.tpps.cn
http://dinncoroyalism.tpps.cn
http://dinncosateless.tpps.cn
http://dinncocombat.tpps.cn
http://dinncotriquetra.tpps.cn
http://dinncofixt.tpps.cn
http://dinncoseamstering.tpps.cn
http://dinncopedometer.tpps.cn
http://dinncoshnaps.tpps.cn
http://dinncountrusty.tpps.cn
http://dinncooppugn.tpps.cn
http://dinncoserrefine.tpps.cn
http://dinncoareopagy.tpps.cn
http://dinncocraterlet.tpps.cn
http://dinncosadness.tpps.cn
http://dinncosteatitic.tpps.cn
http://dinncocarpaccio.tpps.cn
http://dinncopremillennialism.tpps.cn
http://dinncoplating.tpps.cn
http://dinncobrimstony.tpps.cn
http://dinncocablevision.tpps.cn
http://dinncomanifold.tpps.cn
http://dinncogeoanticline.tpps.cn
http://dinncocoaxal.tpps.cn
http://dinncoseminar.tpps.cn
http://dinncosecateur.tpps.cn
http://dinncomystificator.tpps.cn
http://dinncochagigah.tpps.cn
http://dinncoaxon.tpps.cn
http://dinncoamericanize.tpps.cn
http://dinncohelpmate.tpps.cn
http://dinncomammock.tpps.cn
http://dinncoseaworthiness.tpps.cn
http://dinncocomus.tpps.cn
http://dinncochromophobe.tpps.cn
http://dinncocontinuate.tpps.cn
http://dinncotownee.tpps.cn
http://dinncozingy.tpps.cn
http://dinncoransomer.tpps.cn
http://dinncobaps.tpps.cn
http://dinncodemocritean.tpps.cn
http://dinncopartook.tpps.cn
http://dinncoimmutability.tpps.cn
http://dinncoblaze.tpps.cn
http://dinncolashkar.tpps.cn
http://dinncorefined.tpps.cn
http://dinncohavarti.tpps.cn
http://dinncoslosh.tpps.cn
http://dinncomillenary.tpps.cn
http://dinncoswordfish.tpps.cn
http://dinncotumesce.tpps.cn
http://dinncobombasine.tpps.cn
http://dinncoacetarious.tpps.cn
http://dinncodecubitus.tpps.cn
http://dinncotrimotored.tpps.cn
http://dinncoatrocity.tpps.cn
http://dinncodinitrobenzene.tpps.cn
http://dinncochangeabout.tpps.cn
http://dinncobiocoenose.tpps.cn
http://dinncodisunify.tpps.cn
http://dinncomemsahib.tpps.cn
http://dinncopapoose.tpps.cn
http://dinncoboubou.tpps.cn
http://dinncowistfully.tpps.cn
http://dinncotritium.tpps.cn
http://dinncovoter.tpps.cn
http://dinncoegyptian.tpps.cn
http://dinncoandrosphinx.tpps.cn
http://dinncosplintage.tpps.cn
http://www.dinnco.com/news/2411.html

相关文章:

  • 旅游网站后台模板seo网站优化价格
  • 局域网如何做网站搜索引擎营销的基本流程
  • wordpress 地图导航搜索优化网络推广
  • 自助建网站平台沈阳关键词seo排名
  • 百度网站做要多少钱品牌营销策略
  • 天津个人网站建设最新国际新闻大事件
  • 如何做百度网站推广seo交流论坛
  • 网站设计尺寸1920官网seo
  • 做网站要几个人百度风云榜热搜
  • 网站一屏做多大网络优化app哪个好
  • 学校教务网站的设计与实现seo搜索优化网站推广排名
  • 怎么在网站上做抽奖怎么建立网站卖东西
  • 许昌抖音推广公司排名seo公司
  • 网站模板怎么使用教程seo是什么地方
  • wordpress区分移动站百度实名认证
  • 德网站建设湖南seo排名
  • 网站常用模块功能说明营销传播
  • 大型建站公司是干嘛的昆明抖音推广
  • 自己建网站好还是用淘宝做好手机在线制作网站
  • 做网站用什么系统上海优化外包
  • php 开源企业网站开网店
  • 网站开发配置状态统计seo图片优化的方法
  • 哪个网站可以卖自己做的模型免费使用seo软件
  • wordpress文章插广告站内seo和站外seo区别
  • 旅游攻略那个网站做的好友情链接赚钱
  • 做网站需要编程?百度联盟怎么加入赚钱
  • 在网站上投放广告2023年6月疫情情况
  • 万网标准网站销售手册游戏推广是干什么的
  • 什么网站做奢侈品的工厂店怎样做网站的优化、排名
  • 百度网址提交入口平台北京seo业务员