网站建设费用详细表/网络营销的表现形式有哪些
文章目录
- 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} index。self.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])
相关文章:

Transformer中的数据输入构造
文章目录 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 这段…...

完美实现vue3异步加载组件
经过几个小时的努力,终于实现了,根据组件名异常加载组件,直接上代码,网上的很多代码方都有坑,先贴出比较坑的代码: <template><view class"main"> <view class"tops"…...

点云成图原理
点成图(Point Cloud)是指由一组离散的点构成的图形,它们在空间中没有任何连接关系。点成图通常是由激光雷达、相机或其他传感器获取的三维数据,用于表示现实世界中的物体或场景。 三角成图(Triangulation)…...

如何将jsp项目转成springboot项目
昨天说过,springboot推荐使用Thymeleaf作为前后端渲染的模板引擎,为什么推荐用Thymeleaf呢,有以下几个原因: 动静结合:Thymeleaf支持HTML原型,允许在HTML标签中增加额外的属性来实现模板与数据的结合。这样…...

C语言:环形链表
1.例子1:环形链表 142. 环形链表 II - 力扣(LeetCode) 思路:我们先定义两个变量slow和fast,slow每次走一步,fast每次走两步,如果链表是环形链表,那么必定存在fast不会走到链表的最后…...

typescript综合练习1(展开音乐播放列表)
Playlist Soundness What’s up, friend?! I’m so pumped you’re joining us. We’ve got a sick project we could totally use your help on! See, someone’s giving us amazing recommendations for songs to play. But they’re not just coming in as songs. Someti…...

零基础入门学习Python第二阶02面向对象,迭代器生成器,并发编程
Python语言进阶 面向对象相关知识 三大支柱:封装、继承、多态 例子:工资结算系统。 """月薪结算系统 - 部门经理每月15000 程序员每小时200 销售员1800底薪加销售额5%提成"""from abc import ABCMeta, abstractmethodcl…...

Unity | Shader基础知识(第十三集:编写内置着色器阶段总结和表面着色器的补充介绍)
目录 前言 一、表面着色器的补充介绍 二、案例viewDir详解 1.viewDir是什么 2.viewDir的作用 3.使用viewDir写shader 前言 注意观察的小伙伴会发现,这组教程前半部分我们在编写着色器的时候,用的是顶点着色器和片元着色器的组合。 SubShader{CGPRO…...

JavaScript map对象/set对象详解
文章目录 一、map对象二、map对象应用场景1. 数组元素转换2. 对象数组的属性提取或转换3. 数组元素的复杂转换4. 与其他数组方法结合使用5. 与异步操作结合(使用 Promise)6. 生成新的数据结构7. 数学和统计计算 三、set对象1. 基本使用2. 特性3. 注意事项…...

【kettle017】kettle访问DB2数据库并处理数据至execl文件(最近完善中)
1.一直以来想写下基于kettle的系列文章,作为较火的数据ETL工具,也是日常项目开发中常用的一款工具,最近刚好挤时间梳理、总结下这块儿的知识体系。 2.熟悉、梳理、总结下DB2数据库(IBM公司开发的一套关系型数据库管理系统…...

Spring Cloud原理详解和作用特点
当涉及到构建和管理分布式系统的微服务架构时,Spring Cloud 是一个备受欢迎的选择。它提供了一套强大的工具和组件,使开发者能够轻松地构建、部署和管理微服务应用程序。本文将深入探讨 Spring Cloud 的原理和作用特点。 1. Spring Cloud 的原理 Sprin…...

Linux —— 进程间通信
目录 一、进程间通信的介绍二、管道三、匿名管道四、命名管道五、system V进程间通信 一、进程间通信的介绍 1.进程间通信的概念 进程通信(Interprocess communication),简称:IPC; 本来进程之间是相互独立的。但是…...

ASP.NET信息安全研究所设备管理系统的设计与实现
摘 要 以研究所的设备管理系统为背景,以研究所设备管理模式为研究对象,开发了设备管理系统。设备管理系统是设备管理与计算机技术相结合的产物,根据系统的功能需求分析与定义的数据模式,分析了应用程序的主要功能和系统实现的主…...

<网络安全>《81 微课堂<安全产品微简介(1)>》
1 简单的了解复杂的安全产品 产品简要防火墙网络区域边界上部署,主要作用是隔离阻断。安全审计一般包括网络日志的分析、网络流量的监控和用户行为的跟踪等。发现网络中的潜在问题和漏洞。入侵检测IDS实时监控和检测网络中的异常活动和入侵行为。入侵防御IPS防病毒…...

【6D位姿估计】FoundationPose 跑通demo 训练记录
前言 本文记录在FoundationPose中,跑通基于CAD模型为输入的demo,输出位姿信息,可视化结果。 然后分享NeRF物体重建部分的训练,以及RGBD图为输入的demo。 1、搭建环境 方案1:基于docker镜像(推荐…...

Python 中 “yield“ 的不同行为
在我们使用Python编译过程中,yield 关键字用于定义生成器函数,它的作用是将函数变成一个生成器,可以迭代产生值。yield 的行为在不同的情况下会有不同的效果和用途。 1、问题背景 在 Python 中,“yield” 是一种生成器࿰…...

迅睿CMS中实现关键词搜索高亮
在迅睿CMS系统中实现关键词搜索高亮是提升用户体验和搜索效果的重要手段。当用户搜索某个关键词时,将搜索结果中的关键词高亮显示,可以帮助用户更快速地定位到所需信息。 关键词高亮的实现 在迅睿CMS中,你可以使用内置的dr_keyword_highlig…...

晶振的精度与稳定性有什么关系?
晶振的精度和稳定性是电子设备中非常重要的参数,它们受到多种因素的影响,主要包括: 精度的影响因素: 温度变化:晶体的温度系数会使得频率随温度变化而变化,通常在0C到55C的工业标准温度范围内,…...

【C】137 只出现一次的数字
给你一个整数数组 nums ,除某个元素仅出现 一次 外,其余每个元素都恰出现 三次 。请你找出并返回那个只出现了一次的元素。 你必须设计并实现线性时间复杂度的算法且使用常数级空间来解决此问题。 解法一 #include <stdio.h>int singleNumber(i…...

51单片机入门:DS1302时钟
51单片机内部含有晶振,可以实现定时/计数功能。但是其缺点有:精度往往不高、不能掉电使用等。 我们可以通过DS1302时钟芯片来解决以上的缺点。 DS1302时钟芯片 功能:DS1302是一种低功耗实时时钟芯片,内部有自动的计时功能&#x…...

Redis-5 分布式锁
一.为什么要使用分布式锁? 传统的互斥锁synchronized只能作用于同一台虚拟机上的线程,在使用服务器集群部署的情况下,互斥锁就会失效,因此要采用分布式锁来处理不同服务器上的线程访问同一资源的情况。 二.redis的分布式锁是如何…...

音转文工具,9.8k star! 【送源码】
我们经常会遇到将音频转为文字的情况,比如在开会时录音的会议纪要、上课时录下的老师讲课内容。虽然网上也有一些在线的工具可以将音频转为文字,但是考虑到数据安全和费用问题,使用起来也不是很方便。 今天了不起给大家介绍一款开源工具——…...

【首次发布】华为 OD 机试 C卷抽中题库清单(真题库),目前华为OD机考以C卷为主,特殊情况会发送D卷
本篇博客为大家整理华为 OD 考友反馈 C 卷抽中题,经过 1 个的考友复盘,目前已经收录 100 题目,预计在有 2 周可以收集完整。 所有题目,都有考友截图反馈,同时欢迎大家机考过后,提供橡皮擦真题,获…...

【进程等待】waitpid的参数pid | status的位图位操作WIFEXITEDWEXITSTATUS宏
目录 waitpid pid status status位图 status按位操作 输入型参数和输入型参数 宏WIFEXITED&WEXITSTATUS options&非阻塞等待 上篇进程等待我们介绍到怎样去进程等待。我们介绍了wait函数&阻塞等待。本篇我们将介绍waitpid函数的参数pid和status。 waitp…...

unity---常用API
1. Vector3:结构体由x、y、z这3个数值组成,表示一个向量 magnitude变量返回该向量的长度normalized变量返回 magnitude 为 1 时的该向量zero静态变量Vector3(0, 0, 0)one静态变量Vector3(1, 1, 1)forward静态变量Vector3(0, 0, 1)back静态变量Vector3(0…...

设计模式: 模板模式
目录 一,模板模式 二,特点 三,组成部分 四,实现步骤 五,案例 一,模板模式 模板模式(Template Pattern)是一种行为型设计模式,它在超类中定义了一个算法的骨架&#…...

[虚拟机+单机]梦幻契约H5修复版_附GM工具
本教程仅限学习使用,禁止商用,一切后果与本人无关,此声明具有法律效应!!!! 教程是本人亲自搭建成功的,绝对是完整可运行的,踩过的坑都给你们填上了 视频演示 [虚拟机单…...

头文件相互包含 前向声明
当两个类需要相互访问对方的成员或方法时,通常意味着它们之间存在某种依赖关系。这种依赖关系可能源于类的设计或功能需求。以下是一个简单的例子,展示了当两个类需要相互访问对方成员或方法时,如何使用包含对方头文件的方式来解决循环包含的…...

七款好用的上网行为管理软件推荐 |有没有好用的上网行为管理系统
七款好用的上网行为管理软件推荐 |有没有好用的上网行为管理系统 员工上班刷视频! 员工上班炒股! 员工上班干副业! 碰见这种情况怎么办?当然是用电脑监控软件来监视员工的一举一动了,那么这样的软件有哪些呢&#…...

centos7-bcc 安装
检查 内核信息 确保安装的kernel-dev 和内核版本一致!!! rpm -qa|grep kernel kernel-tools-3.10.0-1160.92.1.el7.x86_64 kernel-devel-3.10.0-1160.92.1.el7.x86_64 kernel-headers-3.10.0-1160.92.1.el7.x86_64 kernel-3.10.0-1160.92.1.el7.x86_64 kernel-tools-libs-3.1…...