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

马和人做人和牛做网站南昌seo管理

马和人做人和牛做网站,南昌seo管理,网页qq家园,网站副标题的作用Positional Encoding 位置编码 flyfish Transformer模型没有使用循环神经网络,无法从序列中学习到位置信息,并且它是并行结构,不是按位置来处理序列的,所以为输入序列加入了位置编码,将每个词的位置加入到了词向量中…

Positional Encoding 位置编码

flyfish

Transformer模型没有使用循环神经网络,无法从序列中学习到位置信息,并且它是并行结构,不是按位置来处理序列的,所以为输入序列加入了位置编码,将每个词的位置加入到了词向量中。
如果采用自然数列作为位置编码,编码就是线性的,相邻位置之间的差异就在整个序列中保持恒定。如果采用正弦余弦函数生成的位置嵌入变量具有周期性和正交性,就可以产生各个尺度上具有区分性的位置嵌入,这样在捕捉长距离依赖关系时会表现的更好一点。
PE ( p o s , 2 i ) = sin ⁡ ( p o s / 1000 0 2 i / d model ) PE ( p o s , 2 i + 1 ) = cos ⁡ ( p o s / 1000 0 2 i / d model ) {\Large \begin{aligned} \text{PE}(pos, 2i) = \sin(pos/10000^{2i/d_\text{model}}) \\ \text{PE}(pos, 2i+1) = \cos(pos/10000^{2i/d_\text{model}}) \\ \end{aligned} } PE(pos,2i)=sin(pos/100002i/dmodel)PE(pos,2i+1)=cos(pos/100002i/dmodel)

from collections import Counter
import torch
import torch.nn as nn 
import numpy as np# 生成正弦位置编码表的函数,用于在 Transformer 中引入位置信息
def get_sin_enc_table(n_position, embedding_dim):#------------------------- 维度信息 --------------------------------# n_position: 输入序列的最大长度# embedding_dim: 词嵌入向量的维度#-----------------------------------------------------------------    # 根据位置和维度信息,初始化正弦位置编码表sinusoid_table = np.zeros((n_position, embedding_dim))    # 遍历所有位置和维度,计算角度值for pos_i in range(n_position):for hid_j in range(embedding_dim):angle = pos_i / np.power(10000, 2 * (hid_j // 2) / embedding_dim)sinusoid_table[pos_i, hid_j] = angle    # 计算正弦和余弦值sinusoid_table[:, 0::2] = np.sin(sinusoid_table[:, 0::2])  # dim 2i 偶数维sinusoid_table[:, 1::2] = np.cos(sinusoid_table[:, 1::2])  # dim 2i+1 奇数维    #------------------------- 维度信息 --------------------------------# sinusoid_table 的维度是 [n_position, embedding_dim]#----------------------------------------------------------------   return torch.FloatTensor(sinusoid_table)  # 返回正弦位置编码表
sentences = [['like tree like fruit','羊毛 出在 羊身上'],['East west home is best', '金窝 银窝 不如 自己的 草窝'],]  for sentence in sentences:r=sentence[0].split()print(r)# 计算源语言的最大句子长度,并加 1 以容纳填充符<pad>
src_len = max(len(sentence[0].split()) for sentence in sentences) + 1
print(src_len)
d_embedding = 3  # Embedding 的维度
r=get_sin_enc_table(src_len+1, d_embedding)
print(r)

结果

['like', 'tree', 'like', 'fruit']
['East', 'west', 'home', 'is', 'best']
6
tensor([[ 0.0000,  1.0000,  0.0000],[ 0.8415,  0.5403,  0.0022],[ 0.9093, -0.4161,  0.0043],[ 0.1411, -0.9900,  0.0065],[-0.7568, -0.6536,  0.0086],[-0.9589,  0.2837,  0.0108],[-0.2794,  0.9602,  0.0129]])

在这里插入图片描述
在这里插入图片描述

j假如 Embedding 的维度d_embedding = 512,这样就有了256对正弦值和余弦值
PE ( pos , 0 ) = sin ⁡ ( pos 1000 0 0 512 ) PE ( pos , 1 ) = cos ⁡ ( pos 1000 0 0 512 ) PE ( pos , 2 ) = sin ⁡ ( pos 1000 0 2 512 ) PE ( pos , 3 ) = cos ⁡ ( pos 1000 0 2 512 ) PE ( pos , 4 ) = sin ⁡ ( pos 1000 0 4 512 ) PE ( pos , 5 ) = cos ⁡ ( pos 1000 0 4 512 ) ⋮ PE ( pos , 510 ) = sin ⁡ ( pos 1000 0 510 512 ) PE ( pos , 511 ) = cos ⁡ ( pos 1000 0 510 512 ) \begin{aligned} \text{PE}(\text{pos}, 0) &= \sin\left( \dfrac{\text{pos}}{10000^{\frac{0}{512}}} \right) \\ \text{PE}(\text{pos}, 1) &= \cos\left( \dfrac{\text{pos}}{10000^{\frac{0}{512}}} \right) \\ \text{PE}(\text{pos}, 2) &= \sin\left( \dfrac{\text{pos}}{10000^{\frac{2}{512}}} \right) \\ \text{PE}(\text{pos}, 3) &= \cos\left( \dfrac{\text{pos}}{10000^{\frac{2}{512}}} \right) \\ \text{PE}(\text{pos}, 4) &= \sin\left( \dfrac{\text{pos}}{10000^{\frac{4}{512}}} \right) \\ \text{PE}(\text{pos}, 5) &= \cos\left( \dfrac{\text{pos}}{10000^{\frac{4}{512}}} \right) \\ \vdots \\ \text{PE}(\text{pos}, 510) &= \sin\left( \dfrac{\text{pos}}{10000^{\frac{510}{512}}} \right) \\ \text{PE}(\text{pos}, 511) &= \cos\left( \dfrac{\text{pos}}{10000^{\frac{510}{512}}} \right) \\ \end{aligned} PE(pos,0)PE(pos,1)PE(pos,2)PE(pos,3)PE(pos,4)PE(pos,5)PE(pos,510)PE(pos,511)=sin(100005120pos)=cos(100005120pos)=sin(100005122pos)=cos(100005122pos)=sin(100005124pos)=cos(100005124pos)=sin(10000512510pos)=cos(10000512510pos)

http://www.dinnco.com/news/11653.html

相关文章:

  • 厦门做网站公司排名软文世界官网
  • 网站制作学什么crm客户管理系统
  • app和微网站的对比分析百度seo优
  • 哪个网站可以做会计题seo和sem的区别与联系
  • 东莞互联网营销网站建设网站seo优化排名
  • 软件定制网搜索引擎优化的流程
  • 海盐市网站建设事件营销的案例有哪些
  • wordpress google ad百度seo优化排名软件
  • 厦门网站seo优化全渠道营销
  • 韩国做暖暖网站网站开发流程的8个步骤
  • 网站怎么做响应竞价开户
  • 电子商务与网站建设seo优化排名怎么做
  • 鼓楼网站开发在线工具
  • 夺宝网站建设黑马程序员培训机构在哪
  • 南京哪家网络公司做网站优化好推广引流图片
  • 产品经理做网站seo优化标题 关键词
  • 星月教你做网站回顾文档今日疫情实时数据
  • 域名历史记录查询网站成都有实力的seo团队
  • 建立网站可行性seo优化报告
  • 江门市外事侨务局网站党建设想学互联网从哪里入手
  • 找我家是做的视频网站百度浏览器官方网站
  • 清远做网站的公司关键词优化公司哪家强
  • 站点创建成功有影响吗上海网站优化公司
  • 8848网站盈利模式媒体网络推广价格优惠
  • 合肥网站开发哪家好seo培训一对一
  • 一般vs做的网站的总体框架网页设计制作软件
  • 天津网上办事大厅官网昆明seo
  • 手机网站制作合同网站推广优化公司
  • semcms外贸网站管理系统北京十大营销策划公司
  • 三合一网站建设是指网站如何优化排名