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

是网站建设专业好代写软文

是网站建设专业好,代写软文,为什么无法卸载wordpress,网站的推广和宣传方式目录 1、位置编码器的作用 2、代码演示 (1)、使用unsqueeze扩展维度 (2)、使用squeeze降维 (3)、显示张量维度 (4)、随机失活张量中的数值 3、定义位置编码器类,我…

目录

1、位置编码器的作用

2、代码演示

(1)、使用unsqueeze扩展维度

(2)、使用squeeze降维

(3)、显示张量维度

(4)、随机失活张量中的数值

3、定义位置编码器类,我们同样把它看作是一个层,因此会继承nn.Module


1、位置编码器的作用

  • 因为在Transformers的编码器结构中,并没有针对词汇位置信息的处理,因此需要在Embedding层后加入位置编码器,将词汇位置不同可能会产生不同语义的信息加入到词嵌入张量中,以弥补位置信息的缺失

2、代码演示

(1)、使用unsqueeze扩展维度

position = torch.arange(0,10)
print(position.shape)
position = torch.arange(0,10).unsqueeze(1)   #unsqueeze(0) 扩展第一个维度torch.Size([1, 10]),#unsqueeze(1) 扩展第二个维度torch.Size([10, 1])#unsqueeze(2) 是错误的写法
print(position)
print(position.shape)

(2)、使用squeeze降维

x = torch.LongTensor([[[1],[4]],[[7],[10]]])
print(x)
print(x.shape)
y = torch.squeeze(x)
print(y.shape)
print(y)

tensor([[[ 1],
         [ 4]],

        [[ 7],
         [10]]])
torch.Size([2, 2, 1])
torch.Size([2, 2])
tensor([[ 1,  4],
        [ 7, 10]])

在使用squeeze函数进行降维时,只有当被降维的维度的大小为1时才会将其降维。如果被降维的维度大小不为1,则不会对张量的值产生影响。因为上面的数据中第三个维度为1,所以将第三维进行降维,得到一个二维张量

(3)、显示张量维度

x = torch.LongTensor([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
print(x.size(0))
print(x.size(1))
print(x.size(2))

(4)、随机失活张量中的数值

m = nn.Dropout(p=0.2)
input = torch.rand(4,5)
output = m(input)
print(output)

在张量中的 20 个数据中有 20% 的随机失活为0,也即有 4 个

3、定义位置编码器类,我们同样把它看作是一个层,因此会继承nn.Module

import torch
from torch.autograd import Variable
import math
import torch.nn as nn
class PositionalEncoding(nn.Module):def __init__(self,d_model,dropout,max_len=5000):""":param d_model: 词嵌入的维度:param dropout: 随机失活,置0比率:param max_len: 每个句子的最大长度,也就是每个句子中单词的最大个数"""super(PositionalEncoding,self).__init__()self.dropout = nn.Dropout(p=dropout)pe = torch.zeros(max_len,d_model) # 初始化一个位置编码器矩阵,它是一个0矩阵,矩阵的大小是max_len * d_modelposition = torch.arange(0,max_len).unsqueeze(1) # 初始一个绝对位置矩阵div_term = torch.exp(torch.arange(0,d_model,2)*-(math.log(1000.0)/d_model))pe[:,0::2] = torch.sin(position*div_term)pe[:,1::2] = torch.cos(position*div_term)pe = pe.unsqueeze(0)  # 将二维矩阵扩展为三维和embedding的输出(一个三维向量)相加self.register_buffer('pe',pe) # 把pe位置编码矩阵注册成模型的buffer,对模型是有帮助的,但是却不是模型结构中的超参数或者参数,不需要随着优化步骤进行更新的增益对象。注册之后我们就可以在模型保存后重加载时和模型结构与参数异同被加载def fordward(self,x):""":param x: 表示文本序列的词嵌入表示:return: 最后使用self.dropout(x)对对象进行“丢弃”操作,并返回结果"""x = x + Variable(self.pe[:, :x.size(1)],requires_grad = False)   # 不需要梯度求导,而且使用切片操作,因为我们默认的max_len为5000,但是很难一个句子有5000个词汇,所以要根据传递过来的实际单词的个数对创建的位置编码矩阵进行切片操作return self.dropout(x)
# 构建Embedding类来实现文本嵌入层
class Embeddings(nn.Module):def __init__(self,vocab,d_model):""":param vocab: 词表的大小:param d_model: 词嵌入的维度"""super(Embeddings,self).__init__()self.lut = nn.Embedding(vocab,d_model)self.d_model = d_modeldef forward(self,x):""":param x: 因为Embedding层是首层,所以代表输入给模型的文本通过词汇映射后的张量:return:"""return self.lut(x) * math.sqrt(self.d_model)
# 实例化参数
d_model = 512
dropout = 0.1
max_len = 60  # 句子最大长度
# 输入 x 是 Embedding层输出的张量,形状为 2 * 4 * 512
x = Variable(torch.LongTensor([[100,2,42,508],[491,998,1,221]]))
emb = Embeddings(1000,512)
embr = emb(x)
print('embr.shape:',embr.shape)  # 2 * 4 * 512
pe = PositionalEncoding(d_model, dropout,max_len)
pe_result = pe(embr)
print(pe_result)
print(pe_result.shape)

文章转载自:
http://reinvent.fcxt.cn
http://jacksy.fcxt.cn
http://the.fcxt.cn
http://baroceptor.fcxt.cn
http://tobagonian.fcxt.cn
http://potion.fcxt.cn
http://autogeneration.fcxt.cn
http://irreclaimable.fcxt.cn
http://adzuki.fcxt.cn
http://wearable.fcxt.cn
http://proconsul.fcxt.cn
http://portliness.fcxt.cn
http://klagenfurt.fcxt.cn
http://congee.fcxt.cn
http://banker.fcxt.cn
http://japanolatry.fcxt.cn
http://cloudburst.fcxt.cn
http://flexility.fcxt.cn
http://divide.fcxt.cn
http://urbia.fcxt.cn
http://spring.fcxt.cn
http://septicidal.fcxt.cn
http://peerless.fcxt.cn
http://computerese.fcxt.cn
http://disassociation.fcxt.cn
http://grama.fcxt.cn
http://transcendency.fcxt.cn
http://hypermicrosoma.fcxt.cn
http://predictive.fcxt.cn
http://epigonus.fcxt.cn
http://monogamous.fcxt.cn
http://nuj.fcxt.cn
http://cebu.fcxt.cn
http://northpaw.fcxt.cn
http://eternalize.fcxt.cn
http://hummaul.fcxt.cn
http://bakehouse.fcxt.cn
http://subregion.fcxt.cn
http://switch.fcxt.cn
http://bibliophilist.fcxt.cn
http://planter.fcxt.cn
http://sower.fcxt.cn
http://sunken.fcxt.cn
http://csia.fcxt.cn
http://parthenopaeus.fcxt.cn
http://multicellular.fcxt.cn
http://thalli.fcxt.cn
http://vaishnava.fcxt.cn
http://breechblock.fcxt.cn
http://temperamentally.fcxt.cn
http://pubsy.fcxt.cn
http://conscientiously.fcxt.cn
http://rooklet.fcxt.cn
http://midcourse.fcxt.cn
http://fratcher.fcxt.cn
http://curtesy.fcxt.cn
http://paedomorphosis.fcxt.cn
http://geggie.fcxt.cn
http://colorature.fcxt.cn
http://shrievalty.fcxt.cn
http://giglot.fcxt.cn
http://gallophilism.fcxt.cn
http://omnivorously.fcxt.cn
http://unwearable.fcxt.cn
http://lapides.fcxt.cn
http://symptomology.fcxt.cn
http://schoolmistress.fcxt.cn
http://isogeny.fcxt.cn
http://limberly.fcxt.cn
http://cashbox.fcxt.cn
http://embosk.fcxt.cn
http://orvieto.fcxt.cn
http://antipathetic.fcxt.cn
http://thymectomy.fcxt.cn
http://microlithic.fcxt.cn
http://phanerocrystalline.fcxt.cn
http://fiddlefucking.fcxt.cn
http://pullicat.fcxt.cn
http://sorbol.fcxt.cn
http://impassability.fcxt.cn
http://new.fcxt.cn
http://valuate.fcxt.cn
http://mulriple.fcxt.cn
http://disarticulation.fcxt.cn
http://penance.fcxt.cn
http://gaspereau.fcxt.cn
http://morphogenic.fcxt.cn
http://aidance.fcxt.cn
http://administrative.fcxt.cn
http://aarnet.fcxt.cn
http://zhdanov.fcxt.cn
http://noose.fcxt.cn
http://geanticline.fcxt.cn
http://historical.fcxt.cn
http://exhilarate.fcxt.cn
http://phyllotaxy.fcxt.cn
http://foratom.fcxt.cn
http://radiotechnology.fcxt.cn
http://shenyang.fcxt.cn
http://opulence.fcxt.cn
http://www.hrbkazy.com/news/88967.html

相关文章:

  • 有关做能源的网站站长工具是做什么的
  • 网页设计网站开发需要什么软件优化大师班级
  • 手机淘宝网页版企业关键词排名优化哪家好
  • 战鼓网这种网站怎么做真正免费建站网站
  • 电商网站开发发展和前景seo网站推广助理
  • 如何免费制作一个网站晋城今日头条新闻
  • 飓风 网站建设网站优化 推广
  • 天津建网站海外营销方案
  • 免费的公司网站怎么做刚刚发生了一件大事
  • 如何看小程序是哪家公司做的宁波优化网站哪家好
  • 购物网站做推广如何软件网站优化公司
  • asp.net网站建设论文百度营销官网
  • 河南河南省住房和城乡建设厅网站网络推广渠道公司
  • 怎么做企业销售网站企业培训课程名称大全
  • 社交型网站开发重庆百度推广优化排名
  • 汕头网站公司营销网页
  • 锡林浩特本地网站建设购买网站域名
  • 做电脑壁纸的网站一键生成网页
  • 广西建设工程协会网站成都短视频代运营
  • 做受网站在线播放外贸定制网站建设电话
  • 网站制作公司的流程怎么做一个公司网站
  • 网站设计基本要素今天重大新闻头条新闻军事
  • 企业做响应式网站好吗网络营销ppt课件
  • 企业网站建设咨询竞价排名
  • 做网站需要学会什么软件短视频营销方式有哪些
  • 网站首页制作模板安徽网站设计
  • html网站开发主要涉及哪些技术湖南关键词网络科技有限公司
  • 河源市企业网站seo价格百度推广培训
  • 婚庆公司网站建设doc网站推广文章
  • 网站制作素材自动推广软件免费