【ChatGPT】ChatGPT是如何训练得到的?

news2024/9/22 9:51:24

前言

ChatGPT是一种基于语言模型的聊天机器人,它使用了GPT(Generative Pre-trained Transformer)的深度学习架构来生成与用户的对话。GPT是一种使用Transformer编码器和解码器的预训练模型,它已被广泛用于生成自然语言文本的各种应用程序,例如文本生成,机器翻译和语言理解。

 

在本文中,我们将探讨如何使用Python和PyTorch来训练ChatGPT,以及如何使用已经训练的模型来生成对话。

 1.准备数据

在训练ChatGPT之前,我们需要准备一个大型的对话数据集。这个数据集应该包含足够的对话,覆盖各种主题和领域,以及各种不同的对话风格。这个数据集可以是从多个来源收集的,例如电影脚本,电视节目,社交媒体上的聊天记录等。

在本文中,我们将使用Cornell Movie Dialogs Corpus,一个包含电影对话的大型数据集。这个数据集包含超过22,000个对话,涵盖了多个主题和风格。

我们可以使用以下代码下载和解压缩Cornell Movie Dialogs Corpus,这个数据集也可以从[这里](https://www.cs.cornell.edu/~cristian/Cornell_Movie-Dialogs_Corpus.html)手动下载。

import os
import urllib.request
import zipfile

DATA_URL = 'http://www.cs.cornell.edu/~cristian/data/cornell_movie_dialogs_corpus.zip'
DATA_DIR = './cornell_movie_dialogs_corpus'
DATA_FILE = os.path.join(DATA_DIR, 'cornell_movie_dialogs_corpus.zip')

if not os.path.exists(DATA_DIR):
    os.makedirs(DATA_DIR)

if not os.path.exists(DATA_FILE):
    print('Downloading data...')
    urllib.request.urlretrieve(DATA_URL, DATA_FILE)

print('Extracting data...')
with zipfile.ZipFile(DATA_FILE, 'r') as zip_ref:
    zip_ref.extractall(DATA_DIR)

 2.数据预处理

在准备好数据集之后,我们需要对数据进行预处理,以便将其转换为模型可以处理的格式。在本教程中,我们使用了一个简单的预处理步骤,该步骤包括下列几步:

  • 将数据拆分成句子pairs(上下文,回答)
  • 去除标点符号和特殊字符
  • 将所有的单词转换成小写
  • 将单词映射到一个整数ID
  • 将句子填充到相同的长度
下面是用于预处理数据的代码:
import re
import random
import numpy as np
import torch

def load_conversations():
    id2line = {}
    with open(os.path.join(DATA_DIR, 'movie_lines.txt'), errors='ignore') as f:
        for line in f:
            parts = line.strip().split(' +++$+++ ')
            id2line[parts[0]] = parts[4]

    inputs = []
    outputs = []
    with open(os.path.join(DATA_DIR, 'movie_conversations.txt'), 'r') as f:
        for line in f:
            parts = line.strip().split(' +++$+++ ')
            conversation = [id2line[id] for id in parts[3][1:-1].split(',')]
            for i in range(len(conversation) - 1):
                inputs.append(conversation[i])
                outputs.append(conversation[i+1])
    return inputs, outputs

def preprocess_sentence(sentence):
    sentence = re.sub(r"([?.!,])", r" \1 ", sentence)
    sentence = re.sub(r"[^a-zA-Z?.!,]+", r" ", sentence)
    sentence = sentence.lower()
    return sentence

def tokenize_sentence(sentence, word2index):
    tokenized = []
    for word in sentence.split(' '):
        if word not in word2index:
            continue
        tokenized.append(word2index[word])
    return tokenized

def preprocess_data(inputs, outputs, max_length=20):
    pairs = []
    for i in range(len(inputs)):
        input_sentence = preprocess_sentence(inputs[i])
        output_sentence = preprocess_sentence(outputs[i])
        pairs.append((input_sentence, output_sentence))

    word_counts = {}
    for pair in pairs:
        for sentence in pair:
            for word in sentence.split(' '):
                if word not in word_counts:
                    word_counts[word] = 0
                word_counts[word] += 1

    word2index = {}
    index2word = {0: '<pad>', 1: '<start>', 2: '<end>', 3: '<unk>'}
    index = 4
    for word, count in word_counts.items():
        if count >= 10:
            word2index[word] = index
            index2word[index] = word
            index += 1

    inputs_tokenized = []
    outputs_tokenized = []
    for pair in pairs:
        input_sentence, output_sentence = pair
        input_tokenized = [1] + tokenize_sentence(input_sentence, word2index) + [2]
        output_tokenized = [1] + tokenize_sentence(output_sentence, word2index) + [2]
        if len(input_tokenized) <= max_length and len(output_tokenized) <= max_length:
            inputs_tokenized.append(input_tokenized)
            outputs_tokenized.append(output_tokenized)

    inputs_padded = torch.nn.utils.rnn.pad_sequence(inputs_tokenized, batch_first=True, padding_value=0)
    outputs_padded = torch.nn.utils.rnn.pad_sequence(outputs_tokenized, batch_first=True, padding_value=0)
    return inputs_padded, outputs_padded, word2index, index2word

 3.训练模型

在完成数据预处理之后,我们可以开始训练ChatGPT模型。对于本文中的示例,我们将使用PyTorch深度学习框架来实现ChatGPT模型。

首先,我们需要定义一个Encoder-Decoder模型结构。这个结构包括一个GPT解码器,它将输入的上下文句子转换为一个回答句子。GPT解码器由多个Transformer解码器堆叠而成,每个解码器都包括多头注意力和前馈神经网络层。


import torch.nn as nn
from transformers import GPT2LMHeadModel

class EncoderDecoder(nn.Module):
    def __init__(self, num_tokens, embedding_dim=256, hidden_dim=512, num_layers=2, max_length=20):
        super().__init__()
        
        self.embedding = nn.Embedding(num_tokens, embedding_dim)
        self.decoder = nn.ModuleList([GPT2LMHeadModel.from_pretrained('gpt2') for _ in range(num_layers)])
        self.max_length = max_length

    def forward(self, inputs, targets=None):
        inputs_embedded = self.embedding(inputs)
        outputs = inputs_embedded
        for decoder in self.decoder:
            outputs = decoder(inputs_embedded=outputs)[0]
        return outputs

    def generate(self, inputs, temperature=1.0):
        inputs_embedded = self.embedding(inputs)
        input_length = inputs.shape[1]
        output = inputs_embedded
        for decoder in self.decoder:
            output = decoder(inputs_embedded=output)[0][:, input_length-1, :]
            output_logits = output / temperature
            output_probs = nn.functional.softmax(output_logits, dim=-1)
            output_token = torch.multinomial(output_probs, num_samples=1)
            output_token_embedded = self.embedding(output_token)
            output = torch.cat([output, output_token_embedded], dim=1)
        return output[:, input_length:, :]

然后,我们需要定义一个训练函数,该函数将使用梯度下降方法优化模型参数,并将每个epoch的损失和正确率记录到一个日志文件中。


def train(model, inputs, targets, optimizer, criterion):
    model.train()
    optimizer.zero_grad()
    outputs = model(inputs, targets[:, :-1])
    loss = criterion(outputs.reshape(-1, outputs.shape[-1]), targets[:, 1:].reshape(-1))
    loss.backward()
    optimizer.step()
    return loss.item()

def evaluate(model, inputs, targets, criterion):
    model.eval()
    with torch.no_grad():
        outputs = model(inputs, targets[:, :-1])
        loss = criterion(outputs.reshape(-1, outputs.shape[-1]), targets[:, 1:].reshape(-1))
    return loss.item()

def train_model(model, inputs, targets, word2index, index2word, num_epochs=10, batch_size=64, lr=1e-3):
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/818386.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

抓紧收藏,Selenium无法定位元素的几种解决方案

01、frame/iframe表单嵌套 WebDriver只能在一个页面上对元素识别与定位&#xff0c;对于frame/iframe表单内嵌的页面元素无法直接定位。 解决方法&#xff1a; driver.switch_to.frame(id/name/obj) switch_to.frame()默认可以直接取表单的id或name属性。如果没有可用的id和…

基于量子同态的安全多方量子求和加密

摘要安全多方计算在经典密码学中一直扮演着重要的角色。量子同态加密(QHE)可以在不解密的情况下对加密数据进行计算。目前&#xff0c;大多数协议使用半诚实的第三方(TP)来保护参与者的秘密。我们使用量子同态加密方案代替TP来保护各方的隐私。在量子同态加密的基础上&#xff…

Day49 算法记录|动态规划16 (子序列)

这里写目录标题 583. 两个字符串的删除操作72. 编辑距离总结 583. 两个字符串的删除操作 这道题求得的最小步数&#xff0c;是这道题的变种 M i n ( 步数&#xff09; s t r 1. l e n g t h s t r 2. l e n g t h − 2 ∗ ( M a x ( 公共字符串长度&#xff09; ) Min(步数…

子域名收集工具OneForAll的安装与使用-Win

子域名收集工具OneForAll的安装与使用-Win OneForAll是一款功能强大的子域名收集工具 GitHub地址&#xff1a;https://github.com/shmilylty/OneForAll Gitee地址&#xff1a;https://gitee.com/shmilylty/OneForAll 安装 1、python环境准备 OneForAll基于Python 3.6.0开发和…

Linux系统安装Nginx(保姆级教程)

目录 一、环境准备 二、开始安装 2.1、解压Nginx文件 2.2、编译安装 2.3、启动Nginx 2.4、安装成系统服务&#xff08;脚本&#xff09; 2.5、常见问题 本机如何访问虚拟机中的Nginx&#xff1f; 编译安装的过程中出现如下几种警告错误 一、环境准备 系统&#xff1a…

教程|如何用 Docker/K8s 快速部署 StarRocks 集群

云原生是一种现代化的软件开发和部署方法论。相较于传统的应用开发和部署方式&#xff0c;云原生带来了显著的优势&#xff0c;包括弹性伸缩、应用程序可移植性、高可靠性和自动化部署与管理等方面&#xff0c;从而极大地提升了成本效益和开发效率。 StarRocks 从 3.0 版本开始…

wms三代电子标签操作指导

一、服务器使用 V1.4基站已经内置服务程序&#xff0c;无需搭建服务&#xff1b;可跳至第1.4部分 1、服务器搭建 安装mysql5.7, 创建db_wms数据库并导入原始数据库文件 安装jdk1.8, 配置java环境变量 下载tomca8.0, 部署wms.war到tomcat, 并启动tomcat 2、下载资源 Wind…

dflow工作流使用1——架构和基本概念

对于容器技术、工作流等概念完全不懂的情况下理解dflow的工作方式会很吃力&#xff0c;这里记录一下个人理解。 dflow涉及的基本概念 工作流的概念很好理解&#xff0c;即某个项目可以分为多个步骤&#xff0c;每个步骤可以实现独立运行&#xff0c;只保留输入输出接口&#x…

以科技创新引领短交通行业发展,九号公司重磅新品亮相巴塞罗那MWC

2月27日&#xff0c;以“时不我待(VELOCITY) - 明日科技&#xff0c;将至已至”为主题的2023世界移动通信大会&#xff08;Mobile World Congress&#xff0c;以下简称MWC&#xff09;在西班牙巴塞罗那举办&#xff0c;全球创新短交通领军企业九号公司参加了大会。现场&#xf…

学习记录——DSConv

Dynamic Snake Convolution based on Topological Geometric Constraints for Tubular Structure Segmentation ICCV 2023 用于管状结构分割的动态蛇形卷积 仍存在一些复杂的领域&#xff0c;大模型还未能够很好的覆盖&#xff08;也许只是时间问题&#xff09;。例如伪装目标…

【MySQL】模具更新方案

系列文章 C#底层库–MySQLBuilder脚本构建类&#xff08;select、insert、update、in、带条件的SQL自动生成&#xff09; 本文链接&#xff1a;https://blog.csdn.net/youcheng_ge/article/details/129179216 C#底层库–MySQL数据库操作辅助类&#xff08;推荐阅读&#xff0…

接口测试用例设计:常见问题和风险

一、接口测试 接口测试&#xff0c;即对API进行测试。 接口测试过程容易出现的典型问题&#xff1a; (1) 传入参数处理不当&#xff0c;导致程序奔溃 (2) 类型溢出&#xff0c;导致数据读出和写入不一致 (3) 因对象权限未进行校验&#xff0c;可以访问其他用户的敏感信息 …

快速了解MyBatis---映射关系多对一

文章目录 映射关系多对一映射关系-官方文档映射关系多对1-基本介绍基本介绍注意细节 映射关系多对1-映射方式映射方式配置Mapper.xml 方式-应用实例注解实现多对1 映射-应用实例 映射关系多对一 映射关系-官方文档 文档地址: https://mybatis.org/mybatis-3/zh/sqlmap-xml.ht…

(树) 剑指 Offer 32 - I. 从上到下打印二叉树 ——【Leetcode每日一题】

❓剑指 Offer 32 - I. 从上到下打印二叉树 难度&#xff1a;中等 从上到下打印出二叉树的每个节点&#xff0c;同一层的节点按照从左到右的顺序打印。 例如: 给定二叉树: [3,9,20,null,null,15,7], 3/ \9 20/ \15 7返回&#xff1a; [3,9,20,15,7]提示&#xff1a; 节…

软件测试——Postman Script脚本功能

Postman作为软件测试里一款非常流行的调试工具&#xff0c;给我们提供了一个执行JavaScript脚本的环境&#xff0c;所以我们可以使用js语言编写脚本来解决一些接口自动化的问题&#xff0c;比如接口依赖、接口断言等等。Postman有Pre-RequestScript和Tests两个编写js脚本的模块…

【LeetCode】最小路径和

最小路径和 题目描述算法流程编程代码 链接: 最小路径和 题目描述 算法流程 编程代码 class Solution { public:int minPathSum(vector<vector<int>>& grid) {int m grid.size();int n grid[0].size();vector<vector<int>> dp(m1,vector<in…

html5播放器视频切换和连续播放的实例

当前播放器实例可以使用changeVid接口切换正在播放的视频。当有多个视频&#xff0c;在上一个视频播放完毕时&#xff0c;自动播放下一个视频时也可采用该处理方式。 const option {vid: 88083abbf5bcf1356e05d39666be527a_8,//autoplay: true,//playsafe: , //PC端播放加密视…

ipad必须要配原装的电容笔吗?ipad可以用的手写笔

众所周知&#xff0c;苹果平板电脑的价格很贵&#xff0c;但只要你有充足的预算&#xff0c;是可以选择入手的。另外&#xff0c;iPad搭配上电容笔不但适用于专业画图&#xff0c;也适用于写字作笔记。苹果原装的电容笔&#xff0c;功能强大&#xff0c;但是价格昂贵&#xff0…

记录一个可支持 style 属性 HtmlTextView 控件

大家都知道可通过原生API Html.fromHtml(html) 在 TextView 上显示 html 文本&#xff0c;但显示效果有限。 对于复杂效果就不行了&#xff0c;费了点时间找了一些库验证&#xff0c;最终找到一个合适的&#xff0c;在此记录一下。 支持内容挺丰富的&#xff0c;包含很多 htm…

Mac查看系统状态

syatem profiler mac系统中提供了system profiler来查看系统的详细信息&#xff0c;包括硬件、网络以及安装的软件 Console 显示了系统上的日志文件信息&#xff0c;有助于诊断问题 Activity Monitor 可以提供正在运行的系统的相关信息 https://zhhll.icu/2021/Mac/查看系统…