pytorch使用小结

nn.Embedding

Embedding其实是构造了一个巨大的张量表,对于输入tensor某个位置的标量,在Embedding表中查表进行赋值:

python 复制代码
# 伪代码演示
# 输入size: (1, 3133)
# Embedding size: (15536, 2048)
# output = torch.zeros(1,3133,2048)

for batch_idx in range(1):
    for seq_idx in range(3133):
        # 取出当前位置的Token ID,比如token_id=151656
        token_id = input_tensor[batch_idx, seq_idx]
        # 在Embedding权重字典里把151656那个长度为2048的向量取出来,直接赋值到输出的对应位置
        output[batch_idx, seq_idx, :] = embedding_weight[token_id, :]

从原理上可以看到,input_tensor的每一个值,一定是在[0, Embedding.shape(0)],且是整数

相关推荐
ZhiqianXia15 小时前
Pytorch 学习笔记(3) : torch.cuda
pytorch·笔记·学习
郝学胜-神的一滴20 小时前
Pytorch张量拼接秘籍:cat与stack的深度解析与实战
人工智能·pytorch·python·深度学习·程序人生·算法·机器学习
程序员Shawn1 天前
【深度学习 | 第一篇】- Pytorch与张量
人工智能·pytorch·深度学习
ZhiqianXia1 天前
Pytorch 学习笔记(4) : torch.backends
pytorch·笔记·学习
剑穗挂着新流苏3122 天前
208_深度学习的鲁棒性之美:暂退法(Dropout)原理与实战
开发语言·pytorch·python·深度学习
AI视觉网奇2 天前
LtxVAE 学习笔记
人工智能·pytorch·深度学习
人工干智能2 天前
科普:Python / Numpy / PyTorch 的数据拼接方法
pytorch·python·numpy
郝学胜-神的一滴2 天前
PyTorch核心技巧|view函数深度解析:解锁张量连续性的底层密码
人工智能·pytorch·python·深度学习·线性代数·机器学习
机器学习之心2 天前
PyTorch基于LightGBM的海洋温盐异常垂直剖面预测
人工智能·pytorch·python
罗罗攀2 天前
PyTorch学习笔记|从异或问题到深层神经网络
人工智能·pytorch·笔记·神经网络·学习