当前位置: 首页 > news >正文

论文阅读笔记AI篇 —— Transformer模型理论+实战 (四)

论文阅读笔记AI篇 —— Transformer模型理论+实战 (四)

  • 一、理论
    • 1.1 理论研读
    • 1.2 什么是AI Agent?
  • 二、实战
    • 2.1 先导知识
      • 2.1.1 tensor的创建与使用
      • 2.1.2 PyTorch的模块
        • 2.1.2.1 torch.nn.Module类的继承与使用
        • 2.1.2.2 torch.nn.Linear类
    • 2.2 Transformer代码实现

一、理论

1.1 理论研读

参考文章或视频链接
[1] 《论文阅读笔记AI篇 —— Transformer模型理论+实战 (一)》- CSDN
[2] 《论文阅读笔记AI篇 —— Transformer模型理论+实战 (二)》- CSDN
[3] 《论文阅读笔记AI篇 —— Transformer模型理论+实战 (三)》- CSDN

1.2 什么是AI Agent?

如果说钢铁侠中的J.A.R.V.I.S.(贾维斯)是一个AGI通用人工智能的话,那么现阶段的AI Agent只是做到了感知任务、规划任务、执行任务。下面这张图的这个过程,看上去和强化学习是一模一样的。

Agent结构图——参考视频[1]

参考文章或视频链接
[1]【动画科普AI Agent:大模型之后为何要卷它?】- bilibili
[2]【【卢菁老师说】Agent就是一场彻头彻尾的AI泡沫】- bilibili
[3] 《读懂AI Agent:基于大模型的人工智能代理》
[4] LLM之Agent(一):使用GPT-4开启AutoGPT Agent自动化任务完整指南

二、实战

2.1 先导知识

2.1.1 tensor的创建与使用

对于一维的tensor,它是没有形状而言的,你不能准确的称它为行向量row vector或列向量col vector,只有你明确的指定之后,它才有准确的形状。
但是,在数学中肯定是要有明确的意义的,要么n*1,要么1*n,总得有个说法,说法就是,认为是列向量n*1,见参考文章[2]

import torch
import torch.nn as nndef test1_tensor():x = torch.tensor([1, 1, 1, 1])print("Before reshape:", x.shape)# x = x.reshape(4, 1)x = x.view(4, 1)  # 与reshape一样print(x)print("After reshape(4,1):", x.shape)# x = x.reshape(1, 4)x = x.view(1, 4)  # 与reshape一样print(x)print("After reshape(1,4):", x.shape)if __name__ == '__main__':test1_tensor()"""Console Output
Before reshape: torch.Size([4])tensor([[1],[1],[1],[1]])
After reshape(4,1): torch.Size([4, 1])tensor([[1, 1, 1, 1]])
After reshape(1,4): torch.Size([1, 4])
"""
参考文章或视频链接
[1] Introduction to PyTorch Tensors - PyTorch
[2] Is there any reason for using the word “column” in the context of one-dimensional tensor?

2.1.2 PyTorch的模块

2.1.2.1 torch.nn.Module类的继承与使用

强调一点,你自己实现的所有模块,应该都是继承了nn.Module这个Class的(这也是PyTorch官方文档所强调的),不要觉得可以去掉nn.Module的继承,继承了这个类,才会有一些便捷的方法可供调用,否则你都要自己实现一遍。

class Encoder(nn.Module): # (1)Encoder继承了nn.Moduledef __init__(self):# ...def forward(self, enc_inputs): # 你不需要显示调用该方法,因为在nn.Module.__call__里,已经默认实现了对该方法的调用# ...class Decoder(nn.Module): # (2)Decoder继承了nn.Moduledef __init__(self):# ...def forward(self, dec_inputs, enc_inputs, enc_outputs): # 你不需要显示调用该方法,因为在nn.Module.__call__里,已经默认实现了对该方法的调用# ...
参考文章或视频链接
[1] Module — PyTorch 2.1 documentation
[2] Learning Day 22: What is nn.Module in Pytorch
[3] Why do we need to inherit from nn.Module in PyTorch? - stackoverflow
2.1.2.2 torch.nn.Linear类

关于Linear层有一点问题,就是它的权重矩阵,nn.Linear(4,3)中的4表示输入特征的维度,3表示输出特征的维度,按理来说是一个4 * 3的矩阵才对,但是输出结果偏不,这是因为常规的线性运算是写成这样的(假设维度已知), o u t = W 3 ∗ 4 i n 4 ∗ 1 + b 3 ∗ 1 out = W_{3*4}in_{4*1} + b_{3*1} out=W34in41+b31,但网络层以从左至右的视角看去, i n in in W W W矩阵的左边,写成这样的形式 o u t = ( i n 1 ∗ 4 W 3 ∗ 4 T ) T + b 3 ∗ 1 = ( i n 1 ∗ 4 W 4 ∗ 3 ) T + b 3 ∗ 1 out = (in_{1*4}W_{3*4}^T)^T + b_{3*1} = (in_{1*4}W_{4*3})^T + b_{3*1} out=(in14W34T)T+b31=(in14W43)T+b31就很自然的模拟了这个视角,这样就能解释为什么输出的shape是反过来的。

在这里插入图片描述

请看参考文章[1]。

import torch
import torch.nn as nndef test2_Linear_weight():linear_layer = nn.Linear(4,3)print(linear_layer.shape)  # torch.Size([3, 4])
if __name__ == '__main__':test2_Linear_weight()
参考文章或视频链接
[1] Why does PyTorch’s Linear layer store the weight in shape (out, in) and transpose it in the forward pass? - stackoverflow

2.2 Transformer代码实现

请看参考文章[1]与[4],我认为写的足够详细,也足够易懂,有些torch.transpose()的操作不太好从字面上读懂,就debug看看,无非就是高维矩阵的转置,和二维矩阵的转置也没本质区别。

参考文章或视频链接
本文代码来源:[1] 《Transformer 代码详解(Pytorch版)》- CSDN
[2] 【Transformer代码实现】- bilibili
重点观看此视频:[3] [重置版]从零实现transfomer模型 || 理解ChatGPT基石 || pytorch- bilibili
这篇写的也不错,可以重点阅读:[4] 《Transformer原理与代码实现》- CSDN
http://www.lryc.cn/news/284713.html

相关文章:

  • Template -- Vue2
  • zookeeper window 安装
  • Redis 面试题 | 02.精选Redis高频面试题
  • 大数据开发之kafka(完整版)
  • 单体架构、微服务和无服务器架构
  • Github仓库使用方式
  • Harmony Ble蓝牙App(四)描述符
  • C# 实现单线程异步互斥锁
  • Java设计模式中策略模式可以解决许多if-else的代码结构吗? 是否能满足开闭原则?
  • [C#]C# winform部署yolov8目标检测的openvino模型
  • 力扣刷MySQL-第五弹(详细讲解)
  • 用C语言实现简单的三子棋游戏
  • Yaklang 中的类型和变量
  • C语言从入门到实战——编译和链接
  • 【实战教程】ThinkPHP6分页功能轻松实现,让你的网站更高效!
  • 专业130+总分380+哈尔滨工程大学810信号与系统考研经验水声电子信息与通信
  • 旅游项目day08
  • 蓝桥杯真题(Python)每日练Day2
  • IntelliJ IDEA 拉取gitlab项目
  • RHCSA上课笔记(前半部分)
  • C++代码入门05 字符串容器
  • vue3 项目中 arguments 对象获取失败问题
  • 12.线程同步
  • 开发安全之:System Information Leak: External
  • burp靶场--文件上传
  • mac 中vscode设置root启动
  • 【MySQL数据库专项 一】一个例子讲清楚数据库三范式
  • 【笔记】关于期刊
  • SpringMVC-.xml的配置
  • Java找二叉树的公共祖先