关于Transformer的理解

关于Transformer, QKV的意义表示其更像是一个可学习的查询系统,或许以前搜索引擎的算法就与此有关或者某个分支的搜索算法与此类似。


Can anyone help me to understand this image? - #2 by J_Johnson - nlp - PyTorch Forums

Embeddings - these are learnable weights where each token(token could be a word, sentence piece, subword, character, etc) are converted into a vector, say, with 500 values between 0 and 1 that are trainable.

Positional Encoding - for each token, we want to inform the model where it's located, orderwise. This is because linear layers are not ideal for handling sequential information. So we manually pass this in by adding a vector of sine and cosine values on the first 2 elements in the embedding vector.

This sequence of vectors goes through an attention layer, which basically is like a learnable digitized database search function with keys, queries and values. In this case, we are "searching" for the most likely next token.

The Feed Forward is just a basic linear layer, but is applied across each embedding in the sequence separately(i.e. 3 dim tensor instead of 2 dim).

Then the final Linear layer is where we want to get out our predicted next token in the form of a vector of probabilities, which we apply a softmax to put the values in the range of 0 to 1.

There are two sides because when that diagram was developed, it was being used in language translations. But generative language models for next token prediction just use the Transformer decoder and not the encoder.

Here is a PyTorch tutorial that might help you go through how it works.

Language Modeling with nn.Transformer and torchtext --- PyTorch Tutorials 2.0.1+cu117 documentation


相关推荐
AI即插即用1 小时前
即插即用系列 | SliMamba——空谱维度魔术转换,打造高光谱分类的超轻量级 Mamba 架构
人工智能·深度学习·神经网络·目标检测·计算机视觉·数据挖掘
解局易否结局2 小时前
昇腾CANN上手笔记:从cann-learning-hub学会ops-transformer
笔记·深度学习·transformer
CDYXY2 小时前
2026年4月成都卡布灯箱源头口碑深度调研与避坑指南
大数据·人工智能
小真zzz7 小时前
2026年GEO监测工具深度横评:谁在AI时代守护品牌心智?
人工智能·百度·重构
ZFSS7 小时前
Localization Translate API 集成与使用指南
java·服务器·数据库·人工智能·mysql·ai编程
天行健,君子而铎7 小时前
合规对标·低误报漏报·稳定运行——知源-AI数据分类分级系统金融行业解决方案
人工智能·金融·分类
视觉&物联智能7 小时前
【杂谈】-游戏生成数据:人工智能训练中极易被低估的核心资源
人工智能·游戏·ai·chatgpt·openai·agi·deepseek
扫地的小何尚7 小时前
NVIDIA Vera Rubin 平台如何解决 Agentic AI 的 Scale-up 难题
大数据·人工智能·机器学习
hh.h.8 小时前
昇腾CANN ops-transformer 仓的 MC2 算子:MoE 模型的全到全通信
python·深度学习·transformer·cann
莞凰8 小时前
昇腾CANN的“灵脉根基“:Runtime仓库探秘
android·人工智能·transformer