news 2026/4/15 15:24:01

Transformer 大语言模型(LLM)基石 - Transformer架构详解 - 前馈神经网络(Feed-Forward Neural Network)详解以及算法实现

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Transformer 大语言模型(LLM)基石 - Transformer架构详解 - 前馈神经网络(Feed-Forward Neural Network)详解以及算法实现

锋哥原创的Transformer 大语言模型(LLM)基石视频教程:

https://www.bilibili.com/video/BV1X92pBqEhV

课程介绍

本课程主要讲解Transformer简介,Transformer架构介绍,Transformer架构详解,包括输入层,位置编码,多头注意力机制,前馈神经网络,编码器层,解码器层,输出层,以及Transformer Pytorch2内置实现,Transformer基于PyTorch2手写实现等知识。

Transformer 大语言模型(LLM)基石 - Transformer架构详解 - 前馈神经网络(Feed-Forward Neural Network)详解以及算法实现

每个编码器层还包含一个前馈神经网络。这个网络通常由两个全连接层(线性变换)构成,中间有一个非线性激活函数(例如ReLU)。

  • 功能:进一步对每个位置的表示进行处理,以增强模型的表达能力,防止过拟合

  • 结构:首先是一个全连接层,然后是一个激活函数(如ReLU),最后是另一个全连接层。该操作是对每个位置独立进行的。

代码实现:

# 前馈神经网络 class FeedForward(nn.Module): def __init__(self, d_model, d_ff, dropout=0.1): # d_model输入维度 512 d_ff 输出维度 2048 super().__init__() self.linear1 = nn.Linear(d_model, d_ff) # 线性变换1 self.linear2 = nn.Linear(d_ff, d_model) # 线性变换2 self.dropout = nn.Dropout(dropout) # 创建dropout层 def forward(self, x): """ 前向传播 参数: x: 输入张量 [batch_size, seq_len, d_model] 返回: 前馈神经网络输出 """ x = self.dropout(F.relu(self.linear1(x))) x = self.linear2(x) return x

测试代码:

if __name__ == '__main__': vocab_size = 2000 # 词表大小 embedding_dim = 512 # 词嵌入维度大小 embeddings = Embeddings(vocab_size, embedding_dim) embed_result = embeddings(torch.tensor([[1999, 2, 99, 4, 5], [66, 2, 3, 22, 5], [66, 2, 3, 4, 5]])) print(embed_result.shape) print(embed_result) positional_encoding = PositionalEncoding(embedding_dim) result = positional_encoding(embed_result) print('result=', result) print('result.shape=', result.shape) # 测试自注意力机制层 # query = key = value = result # mask = create_sequence_mask(5) # dropout = nn.Dropout(0.1) # attention_output, attention_weights = self_attention(query, key, value, mask, dropout) # print("attention_output.shape:", attention_output.shape) # [3, 5, 512] # print("attention_weights.shape:", attention_weights.shape) # [3, 5, 5] mha = MultiHeadAttention(d_model=512, num_heads=8) mask = create_sequence_mask(5) result = mha(result, result, result, mask) print('result:', result.shape) # 测试前馈神经网络 ffn = FeedForward(d_model=512, d_ff=2048) ffn_result = ffn(result) print('ffn_result:', ffn_result.shape)

运行输出:

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/13 7:05:15

Fiddler 无法抓包手机 https 报文的解决方案来啦!!

解决手机https无法抓包的问题 当你测试App的时候,想要通过Fiddler/Charles等工具抓包看下https请求的数据情况,发现大部分的App都提示网络异常/无数据等等信息 这时候怎么解决呢? 以软件测试面试提刷题APP为例: Fiddler上的显示…

作者头像 李华
网站建设 2026/4/12 16:28:05

终极代码生成解决方案:OpenReasoning-Nemotron-14B快速部署完整指南

终极代码生成解决方案:OpenReasoning-Nemotron-14B快速部署完整指南 【免费下载链接】OpenReasoning-Nemotron-14B 项目地址: https://ai.gitcode.com/hf_mirrors/nvidia/OpenReasoning-Nemotron-14B 在当今快速发展的软件开发领域,程序员们经常…

作者头像 李华
网站建设 2026/4/15 0:30:55

react中的使用useReducer和Context实现todolist

store.ts - 类型定义 初始状态import { nanoid } from nanoid// 定义单个 Todo 的类型(约束结构:id标题) export type TodoType {id: stringtitle: string }// 初始状态:一个包含2个Todo的数组,用nanoid生成唯一id c…

作者头像 李华
网站建设 2026/4/4 16:10:16

AppPolice:让你的Mac告别卡顿的终极CPU管理神器

AppPolice:让你的Mac告别卡顿的终极CPU管理神器 【免费下载链接】AppPolice MacOS app for quickly limiting CPU usage by running applications 项目地址: https://gitcode.com/gh_mirrors/ap/AppPolice 还在为Mac电脑突然变慢而烦恼吗?当你正在…

作者头像 李华
网站建设 2026/4/11 2:17:28

浅谈web性能测试

什么是性能测试? web性能应该注意些什么? 性能测试,简而言之就是模仿用户对一个系统进行大批量的操作,得出系统各项性能指标和性能瓶颈,并从中发现存在的问题,通过多方协助调优的过程。而web端的性能测试…

作者头像 李华