关于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


相关推荐
安逸Ai1 小时前
Claude Code、Cursor、Copilot 这类工具通常如何理解项目上下文?
人工智能
烟雨江南7851 小时前
2026汽车制造与新能源整车柔性产线Agentic-Workflow白皮书:跨APS_MES_ERP多智能体动态排产与装配容错实战
大数据·网络·人工智能·自动化·ai客服·企业agent
星栈1 小时前
AI 时代,人和 AI 的关系,我的一点思考
人工智能
鲜于言悠9052 小时前
Claude Code重大更新:多会话可互相通信,告别手动复制上下文
人工智能
甲维斯2 小时前
给Claude Code配上DeepSeek,调用kimicu控制电脑
人工智能·agent
geminigoth2 小时前
Spring AI Alibaba 入门开发一(备份)
java·人工智能·spring
liguochuan002 小时前
AI 写代码越来越快,为什么项目却越来越难维护?
人工智能
冬奇Lab2 小时前
代码库知识库系列(12):Git 历史是第四条检索路径
人工智能
空堂与归2 小时前
复杂推理总翻车、Prompt 被注入怎么办?六种进阶技术从 CoT 到 ReAct 实战全解析
人工智能
u0102789402 小时前
GPT 与 Gemini:综合能力与场景适配性测评
人工智能