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

【GPT LLM】跟着论文学习gpt

GPT1开山之作:Improving language understanding by generative pre-training

在这里插入图片描述
本文提出了gpt1,即使用无标签的数据对模型先进行训练,让模型学习能够适应各个任务的通用表示;后使用小部分 task-aware的数据对模型进行微调,可以在各个task上实现更强大的功能。

设计框架

分为两块,pre-train和fine-tune,使用transformer模型的解码器部分。

第一阶段:Unsupervised pre-training

预测连续的k个词的下一个词的概率,本质就是最大似然估计,让模型下一个输出的单词的最大概率的输出是真实样本的下一个单词的 u i u_i ui。后面的元素不会看,只看前k个元素,这就和transformer的解码器极为相似。
在这里插入图片描述

第二阶段:Supervised fine-tuning

训练下游的task的数据集拥有以下形式的数据:假设每句话中有m个单词,输入序列 { x 1 , x 2 , . . . , x m } \{x^1,x^2,...,x^m\} {x1,x2,...,xm} 和一个标签 y y y(忧下游任务决定)。
在这个阶段,作者定义了两个优化函数,L1保证句子的连贯性,L2保证下游任务的准确率。
在这里插入图片描述

下游任务

针对不同的下游任务,制定了不同的训练方案,其完整的内部框架结构如下:

在这里插入图片描述

复现代码

一个十分简易的复现:

import torch
import torch.nn as nn
import torch.nn.functional as F
import math
import copyimport torch.nn.functional as F# accepts input in [ batch x channels x shape ] format
class Attention(nn.Module):def __init__(self, in_channels, heads, dropout=None):assert in_channels % heads == 0super().__init__()self.in_channels = in_channelsself.heads = headsself.dropout = dropoutdef forward(self, queries, keys, values, mask=None):attention = torch.bmm(queries, keys.permute(0,2,1)) / self.in_channels**0.5if mask is not None:attention = attention.masked_fill(mask, -1e9)attention = F.softmax(attention, dim=-1)if self.dropout is not None:attention = F.dropout(attention, self.dropout)output = torch.bmm(attention, values)return output# adds positional encodings
class PositionalEncoding(nn.Module):def forward(self, input_):_, channels, length = input_.shapenumerator = torch.arange(length, dtype=torch.float)denominator = 1e-4 ** (2 * torch.arange(channels, dtype=torch.float) / channels)positional_encodings = torch.sin(torch.ger(denominator, numerator))return input_ + positional_encodingsclass EncoderLayer(nn.Module):def __init__(self, in_channels, heads, dropout=None):super().__init__()self.in_channels = in_channelsself.heads = headsself.produce_qkv = nn.Linear(in_channels, 3*in_channels)self.attention = Attention(in_channels, heads, dropout)self.linear = nn.Linear(in_channels, in_channels)def forward(self, inputs):qkv = self.produce_qkv(inputs)queries, keys, values = qkv.split(self.in_channels, -1)attention = self.attention(queries, keys, values)outputs = F.layer_norm(attention + inputs, (self.in_channels,))outputs = F.layer_norm(self.linear(outputs) + outputs, (self.in_channels,))return outputs class DecoderLayer(nn.Module):def __init__(self, in_channels, heads, dropout=None):super().__init__()self.in_channels = in_channelsself.heads = headsself.produce_qkv = nn.Linear(in_channels, 3*in_channels)self.produce_kv = nn.Linear(in_channels, 2*in_channels)self.masked_attention = Attention(in_channels, heads, dropout)self.attention = Attention(in_channels, heads, dropout)self.linear = nn.Linear(in_channels, in_channels)def forward(self, inputs, outputs):qkv = self.produce_qkv(outputs)queries, keys, values = qkv.split(self.in_channels, -1)n = inputs.shape[1]mask = torch.tril(torch.ones((n, n), dtype=torch.uint8))attention = self.masked_attention(queries, keys, values, mask)outputs = F.layer_norm(attention + outputs, (self.in_channels,))kv = self.produce_kv(inputs)keys, values = kv.split(self.in_channels, -1)attention = self.attention(outputs, keys, values)outputs = F.layer_norm(attention + outputs, (self.in_channels,))outputs = F.layer_norm(self.linear(outputs) + outputs, (self.in_channels,))return outputsif __name__ == '__main__':print("Running...")test_in = torch.rand([3,4,5])encoder = EncoderLayer(5, 1)test_out = encoder(test_in)  # torch.Size([3, 4, 5])assert test_out.shape == (3, 4, 5)print("encoder passed")decoder = DecoderLayer(5, 1) # 这就是gpt模型test_mask = torch.tril(torch.ones((4, 4), dtype=torch.uint8))test_out = decoder(test_in, test_in)assert test_out.shape == (3, 4, 5)  # torch.Size([3, 4, 5])print("decoder passed")

GPT2

http://www.lryc.cn/news/93903.html

相关文章:

  • 【玩转Docker小鲸鱼叭】Docker容器常用命令大全
  • 专项练习11
  • ASP.NET+SQL通用作业批改系统设计(源代码+论文)
  • 基于深度学习的高精度打电话检测识别系统(PyTorch+Pyside6+YOLOv5模型)
  • Vue搭建智能文本检索视频界面
  • 软考A计划-系统集成项目管理工程师-一般补充知识-中
  • springboot-内置Tomcat
  • Flink流批一体计算(2):Flink关键特性
  • 2023软件工程中各种图在现代企业级开发中的使用频率
  • macOS Big Sur 11.7.8 (20G1351) 正式版 ISO、PKG、DMG、IPSW 下载
  • 【C++案例】一个项目掌握C++基础-通讯录管理系统
  • Triton教程 --- 动态批处理
  • Python的并行(持续更新)
  • chatgpt赋能python:Python实现Fibonacci数列
  • 开环模块化多电平换流器仿真(MMC)N=6
  • java springboot整合MyBatis联合查询
  • windows2022证书配置.docx
  • HCIP网络笔记分享——IA回顾及OSPF协议
  • 网络:IP地址、子网掩码、网络地址、广播地址、网段、网关
  • 编程的未来 - 还有未来么?
  • 从零开始搭建群众权益平台(二)
  • Mysql之数据备份
  • 【数据库数据恢复】SQL Server数据表结构损坏的数据恢复案例
  • C语言/C++ 之 打飞机游戏
  • 在 Docker 中部署 Mino 并挂载配置文件
  • 无限脉动:释放音乐和区块链在音乐领域的力量
  • 读发布!设计与部署稳定的分布式系统(第2版)笔记09_一窝蜂和容量
  • 详解java定时任务
  • PCIe卡设计方案:单路12Gsps 3G 带宽模拟信号源PCIe卡
  • 第1章 计算机网络和因特网