WordEmbeddingPositionEmbedding

server/2025/1/14 17:27:41/

文章目录

  • 1. Word Embedding
  • 2. Position Embedding
  • 3. python 代码

在这里插入图片描述

1. Word Embedding

根据矩阵序列实现在nn.Embedding中抽取制定的行作为词向量,长度不同时,自动填充到统一长度

2. Position Embedding

在这里插入图片描述

python__6">3. python 代码

python">import torch
import torch.nn as nn
import torch.nn.functional as F
torch.manual_seed(22323)
torch.set_printoptions(precision=3, sci_mode=False)# 1. step1: get index sequence matrix
# 1. input = torch.Tensor([2,4])
# 2. output = torch.Tensor([[1,2,0,0,0]
#                           [3,2,5,4,0]])
# 1. src_seq,---> (batch_size,len
class PositionEmbeddings(nn.Module):def __init__(self, max_len, max_dim):super(PositionEmbeddings, self).__init__()self.max_len1 = max_lenself.max_dim = max_dimself.embedding = nn.Embedding(self.max_len1, self.max_dim)def forward(self, x):row_ones = torch.ones((1, self.max_dim)).to(torch.float)print(f"row_ones.shape=\n{row_ones.shape}")print(f"row_ones=\n{row_ones}")row_pos = torch.arange(self.max_len1).reshape((-1, 1)).to(torch.float)print(f"row_pos=\n{row_pos}")pos_mat = row_pos @ row_onesprint(f"pos_mat=\n{pos_mat}")column_ones = torch.ones((self.max_len1, 1)).to(torch.float)print(f"column_ones=\n{column_ones}")sin_arange = torch.arange(self.max_dim).reshape((1, -1)).to(torch.float)sin_arange[:, 1::2] = 0i2_dmodel = sin_arange / self.max_dimprint(f"i2_dmodel={i2_dmodel}")power = 10000sin_power_i2_dmodel = torch.pow(power, i2_dmodel)sin_pos_power_mat = column_ones @ sin_power_i2_dmodelprint(f"sin_pos_power_mat=\n{sin_pos_power_mat}")sin_mat = torch.sin(pos_mat / sin_pos_power_mat)sin_mat[:, 1::2] = 0print(f"sin_mat=\n{sin_mat}")cos_arange = torch.arange(self.max_dim).reshape((1, -1)).to(torch.float)cos_arange[:, 0::2] = 0cos_i2_dmodel = cos_arange / self.max_dimprint(f"cos_i2_dmodel={cos_i2_dmodel}")power = 10000cos_power_i2_dmodel = torch.pow(power, cos_i2_dmodel)cos_pos_power_mat = column_ones @ cos_power_i2_dmodelprint(f"cos_pos_power_mat=\n{cos_pos_power_mat}")cos_mat = torch.cos(pos_mat / cos_pos_power_mat)print(f"cos_mat=\n{cos_mat}")cos_mat[:, 0::2] = 0print(f"sin_mat=\n{sin_mat}")print(f"cos_mat=\n{cos_mat}")pe_mat = sin_mat + cos_matprint(f"pe_mat=\n{pe_mat}")pe_embedding = nn.Embedding(self.max_len1, self.max_dim)pe_embedding.weight = nn.Parameter(pe_mat, requires_grad=False)print(f"pe_embedding=\n{pe_embedding.weight}")ouput = pe_embedding(x)return ouputclass CreateIndexMatrix(nn.Module):def __init__(self, max_len, max_dim):super(CreateIndexMatrix, self).__init__()self.max_len = max_lenself.max_dim = max_dimself.result_matrix = torch.zeros((self.max_len, self.max_dim))def forward(self, x):my_x = xmy_num = torch.numel(my_x)# my_seq = [F.pad(torch.randint(1, self.max_len, (L,)),(0,self.max_dim-L)) for L in my_x]# my_seq = [torch.unsqueeze(F.pad(torch.randint(1, self.max_len, (L,)), (0, self.max_dim - L)), dim=0) for L in#          my_x]my_seq = torch.cat([torch.unsqueeze(F.pad(torch.randint(1, self.max_len, (L,)), (0, self.max_dim - L)), dim=0) for L inmy_x], dim=0)# print(f"my_seq=\n{my_seq}")return my_seqif __name__ == "__main__":run_code = 0src_len = torch.Tensor([2.0, 4.0]).to(torch.int32)tgt_len = torch.Tensor([4.0, 3.0]).to(torch.int32)print(f"src_len={src_len}")print(f"tgt_len={tgt_len}")print(torch.numel(src_len))src_max_len = 5src_max_dim = 8tgt_max_len = 5tgt_max_dim = 5my_matrix_src = CreateIndexMatrix(src_max_len, src_max_dim)my_matrix_tgt = CreateIndexMatrix(src_max_len, src_max_dim)word_seq_src = my_matrix_src(src_len)word_seq_tgt = my_matrix_src(tgt_len)print(f"word_seq_src=\n{word_seq_src}")print(f"word_seq_tgt=\n{word_seq_tgt}")src_embedding_table = nn.Embedding(src_max_len + 1, src_max_dim)print(f"src_embedding_table.weight=\n{src_embedding_table.weight}")src_word_embedding = src_embedding_table(word_seq_src)print(f"word_seq_src=\n{word_seq_src}")print(f"src_word_embedding=\n{src_word_embedding}")tgt_embedding_table = nn.Embedding(tgt_max_len + 1, tgt_max_dim)tgt_word_embedding = tgt_embedding_table(word_seq_tgt)print(f"word_seq_tgt=\n{word_seq_tgt}")print(f"tgt_word_embedding=\n{tgt_word_embedding}")my_pos_matrix = PositionEmbeddings(src_max_len, src_max_dim)print(my_pos_matrix(word_seq_src))
  • 结果
python">src_len=tensor([2, 4], dtype=torch.int32)
tgt_len=tensor([4, 3], dtype=torch.int32)
2
word_seq_src=
tensor([[3, 1, 0, 0, 0, 0, 0, 0],[3, 2, 4, 3, 0, 0, 0, 0]])
word_seq_tgt=
tensor([[4, 1, 3, 3, 0, 0, 0, 0],[1, 1, 2, 0, 0, 0, 0, 0]])
src_embedding_table.weight=
Parameter containing:
tensor([[ 0.991,  0.168, -0.281,  0.527,  0.648, -0.331, -0.017,  0.029],[ 2.050,  0.348, -0.532,  1.540, -0.233,  0.176, -0.937,  0.500],[-1.087, -1.750,  1.535, -2.043, -3.229,  0.235,  1.206, -0.232],[-0.558,  0.061, -0.617, -0.523, -0.559,  0.301, -2.089,  0.562],[-0.278,  0.040,  1.628,  0.283,  0.157,  0.165,  1.659, -0.328],[-0.430,  1.530,  1.793,  0.976, -0.355,  0.060, -0.010,  0.525]],requires_grad=True)
word_seq_src=
tensor([[3, 1, 0, 0, 0, 0, 0, 0],[3, 2, 4, 3, 0, 0, 0, 0]])
src_word_embedding=
tensor([[[-0.558,  0.061, -0.617, -0.523, -0.559,  0.301, -2.089,  0.562],[ 2.050,  0.348, -0.532,  1.540, -0.233,  0.176, -0.937,  0.500],[ 0.991,  0.168, -0.281,  0.527,  0.648, -0.331, -0.017,  0.029],[ 0.991,  0.168, -0.281,  0.527,  0.648, -0.331, -0.017,  0.029],[ 0.991,  0.168, -0.281,  0.527,  0.648, -0.331, -0.017,  0.029],[ 0.991,  0.168, -0.281,  0.527,  0.648, -0.331, -0.017,  0.029],[ 0.991,  0.168, -0.281,  0.527,  0.648, -0.331, -0.017,  0.029],[ 0.991,  0.168, -0.281,  0.527,  0.648, -0.331, -0.017,  0.029]],[[-0.558,  0.061, -0.617, -0.523, -0.559,  0.301, -2.089,  0.562],[-1.087, -1.750,  1.535, -2.043, -3.229,  0.235,  1.206, -0.232],[-0.278,  0.040,  1.628,  0.283,  0.157,  0.165,  1.659, -0.328],[-0.558,  0.061, -0.617, -0.523, -0.559,  0.301, -2.089,  0.562],[ 0.991,  0.168, -0.281,  0.527,  0.648, -0.331, -0.017,  0.029],[ 0.991,  0.168, -0.281,  0.527,  0.648, -0.331, -0.017,  0.029],[ 0.991,  0.168, -0.281,  0.527,  0.648, -0.331, -0.017,  0.029],[ 0.991,  0.168, -0.281,  0.527,  0.648, -0.331, -0.017,  0.029]]],grad_fn=<EmbeddingBackward0>)
word_seq_tgt=
tensor([[4, 1, 3, 3, 0, 0, 0, 0],[1, 1, 2, 0, 0, 0, 0, 0]])
tgt_word_embedding=
tensor([[[ 0.491, -0.151,  1.233,  1.313,  2.073],[-1.523,  2.063, -2.640, -1.130, -0.148],[ 0.402, -0.654, -0.677, -0.934, -0.158],[ 0.402, -0.654, -0.677, -0.934, -0.158],[-0.165,  1.398, -1.070,  0.093,  1.199],[-0.165,  1.398, -1.070,  0.093,  1.199],[-0.165,  1.398, -1.070,  0.093,  1.199],[-0.165,  1.398, -1.070,  0.093,  1.199]],[[-1.523,  2.063, -2.640, -1.130, -0.148],[-1.523,  2.063, -2.640, -1.130, -0.148],[ 1.009,  0.027, -1.191, -1.281,  0.358],[-0.165,  1.398, -1.070,  0.093,  1.199],[-0.165,  1.398, -1.070,  0.093,  1.199],[-0.165,  1.398, -1.070,  0.093,  1.199],[-0.165,  1.398, -1.070,  0.093,  1.199],[-0.165,  1.398, -1.070,  0.093,  1.199]]],grad_fn=<EmbeddingBackward0>)
row_ones.shape=
torch.Size([1, 8])
row_ones=
tensor([[1., 1., 1., 1., 1., 1., 1., 1.]])
row_pos=
tensor([[0.],[1.],[2.],[3.],[4.]])
pos_mat=
tensor([[0., 0., 0., 0., 0., 0., 0., 0.],[1., 1., 1., 1., 1., 1., 1., 1.],[2., 2., 2., 2., 2., 2., 2., 2.],[3., 3., 3., 3., 3., 3., 3., 3.],[4., 4., 4., 4., 4., 4., 4., 4.]])
column_ones=
tensor([[1.],[1.],[1.],[1.],[1.]])
i2_dmodel=tensor([[0.000, 0.000, 0.250, 0.000, 0.500, 0.000, 0.750, 0.000]])
sin_pos_power_mat=
tensor([[   1.,    1.,   10.,    1.,  100.,    1., 1000.,    1.],[   1.,    1.,   10.,    1.,  100.,    1., 1000.,    1.],[   1.,    1.,   10.,    1.,  100.,    1., 1000.,    1.],[   1.,    1.,   10.,    1.,  100.,    1., 1000.,    1.],[   1.,    1.,   10.,    1.,  100.,    1., 1000.,    1.]])
sin_mat=
tensor([[ 0.000,  0.000,  0.000,  0.000,  0.000,  0.000,  0.000,  0.000],[ 0.841,  0.000,  0.100,  0.000,  0.010,  0.000,  0.001,  0.000],[ 0.909,  0.000,  0.199,  0.000,  0.020,  0.000,  0.002,  0.000],[ 0.141,  0.000,  0.296,  0.000,  0.030,  0.000,  0.003,  0.000],[-0.757,  0.000,  0.389,  0.000,  0.040,  0.000,  0.004,  0.000]])
cos_i2_dmodel=tensor([[0.000, 0.125, 0.000, 0.375, 0.000, 0.625, 0.000, 0.875]])
cos_pos_power_mat=
tensor([[    1.000,     3.162,     1.000,    31.623,     1.000,   316.228,1.000,  3162.278],[    1.000,     3.162,     1.000,    31.623,     1.000,   316.228,1.000,  3162.278],[    1.000,     3.162,     1.000,    31.623,     1.000,   316.228,1.000,  3162.278],[    1.000,     3.162,     1.000,    31.623,     1.000,   316.228,1.000,  3162.278],[    1.000,     3.162,     1.000,    31.623,     1.000,   316.228,1.000,  3162.278]])
cos_mat=
tensor([[ 1.000,  1.000,  1.000,  1.000,  1.000,  1.000,  1.000,  1.000],[ 0.540,  0.950,  0.540,  1.000,  0.540,  1.000,  0.540,  1.000],[-0.416,  0.807, -0.416,  0.998, -0.416,  1.000, -0.416,  1.000],[-0.990,  0.583, -0.990,  0.996, -0.990,  1.000, -0.990,  1.000],[-0.654,  0.301, -0.654,  0.992, -0.654,  1.000, -0.654,  1.000]])
sin_mat=
tensor([[ 0.000,  0.000,  0.000,  0.000,  0.000,  0.000,  0.000,  0.000],[ 0.841,  0.000,  0.100,  0.000,  0.010,  0.000,  0.001,  0.000],[ 0.909,  0.000,  0.199,  0.000,  0.020,  0.000,  0.002,  0.000],[ 0.141,  0.000,  0.296,  0.000,  0.030,  0.000,  0.003,  0.000],[-0.757,  0.000,  0.389,  0.000,  0.040,  0.000,  0.004,  0.000]])
cos_mat=
tensor([[0.000, 1.000, 0.000, 1.000, 0.000, 1.000, 0.000, 1.000],[0.000, 0.950, 0.000, 1.000, 0.000, 1.000, 0.000, 1.000],[0.000, 0.807, 0.000, 0.998, 0.000, 1.000, 0.000, 1.000],[0.000, 0.583, 0.000, 0.996, 0.000, 1.000, 0.000, 1.000],[0.000, 0.301, 0.000, 0.992, 0.000, 1.000, 0.000, 1.000]])
pe_mat=
tensor([[     0.000,      1.000,      0.000,      1.000,      0.000,      1.000,0.000,      1.000],[     0.841,      0.950,      0.100,      1.000,      0.010,      1.000,0.001,      1.000],[     0.909,      0.807,      0.199,      0.998,      0.020,      1.000,0.002,      1.000],[     0.141,      0.583,      0.296,      0.996,      0.030,      1.000,0.003,      1.000],[    -0.757,      0.301,      0.389,      0.992,      0.040,      1.000,0.004,      1.000]])
pe_embedding=
Parameter containing:
tensor([[     0.000,      1.000,      0.000,      1.000,      0.000,      1.000,0.000,      1.000],[     0.841,      0.950,      0.100,      1.000,      0.010,      1.000,0.001,      1.000],[     0.909,      0.807,      0.199,      0.998,      0.020,      1.000,0.002,      1.000],[     0.141,      0.583,      0.296,      0.996,      0.030,      1.000,0.003,      1.000],[    -0.757,      0.301,      0.389,      0.992,      0.040,      1.000,0.004,      1.000]])
tensor([[[     0.141,      0.583,      0.296,      0.996,      0.030,1.000,      0.003,      1.000],[     0.841,      0.950,      0.100,      1.000,      0.010,1.000,      0.001,      1.000],[     0.000,      1.000,      0.000,      1.000,      0.000,1.000,      0.000,      1.000],[     0.000,      1.000,      0.000,      1.000,      0.000,1.000,      0.000,      1.000],[     0.000,      1.000,      0.000,      1.000,      0.000,1.000,      0.000,      1.000],[     0.000,      1.000,      0.000,      1.000,      0.000,1.000,      0.000,      1.000],[     0.000,      1.000,      0.000,      1.000,      0.000,1.000,      0.000,      1.000],[     0.000,      1.000,      0.000,      1.000,      0.000,1.000,      0.000,      1.000]],[[     0.141,      0.583,      0.296,      0.996,      0.030,1.000,      0.003,      1.000],[     0.909,      0.807,      0.199,      0.998,      0.020,1.000,      0.002,      1.000],[    -0.757,      0.301,      0.389,      0.992,      0.040,1.000,      0.004,      1.000],[     0.141,      0.583,      0.296,      0.996,      0.030,1.000,      0.003,      1.000],[     0.000,      1.000,      0.000,      1.000,      0.000,1.000,      0.000,      1.000],[     0.000,      1.000,      0.000,      1.000,      0.000,1.000,      0.000,      1.000],[     0.000,      1.000,      0.000,      1.000,      0.000,1.000,      0.000,      1.000],[     0.000,      1.000,      0.000,      1.000,      0.000,1.000,      0.000,      1.000]]])

http://www.ppmy.cn/server/158338.html

相关文章

51单片机入门基础

目录 一、基础知识储备 &#xff08;一&#xff09;了解51单片机的基本概念 &#xff08;二&#xff09;掌握数字电路基础 &#xff08;三&#xff09;学习C语言编程基础 二、开发环境搭建 &#xff08;一&#xff09;硬件准备 &#xff08;二&#xff09;软件准备 三、…

Python----Python基础(字符串,列表,元组,字典,集合的总结)

一、字符串 str&#xff0c;基本用法&#xff1a;拼接&#xff0c;复制&#xff0c;长度&#xff0c;索引&#xff0c;切片&#xff0c;查找&#xff0c;替换&#xff0c;大小写转化&#xff0c;去除空格&#xff0c;填充&#xff0c;分割&#xff0c;判断&#xff0c;可变字符…

STM32-Flash存储

目录 1.0 闪存模块组织 2.0 Flash基本结构 3.0 Flash解锁 4.0 指针访问存储器地址 5.0 程序存储器编程 6.0 选项字节 7.0 选项字节编程 8.0 选项字节擦除 9.0 电子签名 10.0 手册解读 定义&#xff1a; STM32F1系列的FLASH包含程序存储器、系统存储器和选项字节三个部…

飞凌嵌入式i.MX8M Mini核心板已支持Linux6.1

飞凌嵌入式FETMX8MM-C核心板现已支持Linux6.1系统&#xff0c;此次升级不仅使系统功能更加丰富&#xff0c;还通过全新BSP实现了内存性能的显著提升。 基于NXP i.MX8M Mini处理器设计开发的飞凌嵌入式FETMX8MM-C核心板&#xff0c;拥有4个Cortex-A53高性能核和1个Cortex-M4实时…

【按钮防抖】el-button和普通按钮防抖,点击一次禁用一秒再恢复

点击后的效果&#xff0c;一秒后会恢复可点击 左边是组件按钮&#xff0c;通过disabled控制 右边普通按钮通过自定义指令控制&#xff0c;这里自定义指令写法是html版本的 <body><div id"app"><el-card><p style"padding: 10px;"&g…

宁德时代C++后端开发面试题及参考答案

请阐述面向对象的三大特性。 面向对象编程有三大特性,分别是封装、继承和多态。 封装是指将数据和操作数据的方法绑定在一起,对数据的访问和操作进行限制。这样做的好处是可以隐藏对象的内部细节,只暴露必要的接口给外部。例如,我们可以把一个汽车类的内部引擎状态、速度等…

文章题目:利用Adobe Flash Player漏洞:一次针对Windows XP的渗透测试实验

文章题目&#xff1a;利用Adobe Flash Player漏洞&#xff1a;一次针对Windows XP的渗透测试实验 摘要 本文详细介绍了一次针对运行Windows XP系统的渗透测试实验。实验中&#xff0c;攻击者利用Adobe Flash Player中的一个未修补漏洞&#xff0c;通过浏览器插件执行远程代码&…

DATACOM-园区典型网络设计-复习-实验

园区典型网络 概述规划与设计组网设计基础业务设计WLAN设计可靠性设计防环设计出口NAT安全设计运维管理 部署与实施网络运维网络优化 实验简单园区组网设计 Update 2025.1.10 后续更新实验概述 规划与设计 组网设计 典型举例&#xff1a;接入层选用S3700&#xff08;百兆&#…