Loading [MathJax]/jax/input/TeX/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >minGPT安装与使用

minGPT安装与使用

作者头像
医学处理分析专家
发布于 2023-09-20 10:43:24
发布于 2023-09-20 10:43:24
47500
代码可运行
举报
运行总次数:0
代码可运行

今天将分享minGPT的pytorch安装与使用的完整过程,为了方便大家学习理解整个流程,将整个流程步骤进行了整理,并给出详细的步骤结果。感兴趣的朋友赶紧动手试一试吧。

一、minGPT安装

openai公司开发的GPT模型有上亿参数,模型非常大,可以参考github项目https://github.com/openai/gpt-2。为了能在常规的电脑上可以运行GPT模型,将安装minGPT模型。

安装minGPT的依赖库,在命令行使用以下命令安装,安装完成后可以看到成功安装所需的依赖库。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
git clone https://github.com/junqiangchen/minGPT.git
cd minGPT
pip install -e .

二、minGPT使用案例

这里不讲述如何训练minGPT,具体的训练步骤可以参考这个例子:https://github.com/junqiangchen/minGPT/blob/master/demo.ipynb

这里直接拿gpt预训练权重来生成测试,预训练权重有四个可以选择:'gpt2', 'gpt2-medium', 'gpt2-large', 'gpt2-xl',其中gpt2模型参数有124.44M,gpt2-medium模型参数有354.82M,gpt2-large模型参数有774.03M,gpt2-xl模型参数有1557.61M。如下是gpt2-medium预训练权重下载过程。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import torch
from transformers import GPT2Tokenizer, GPT2LMHeadModel
from mingpt.model import GPT
from mingpt.utils import set_seed
from mingpt.bpe import BPETokenizer

set_seed(3407)

use_mingpt = True  # use minGPT or huggingface/transformers model?
model_type = 'gpt2-medium'
device = 'cuda'

if use_mingpt:
    model = GPT.from_pretrained(model_type)
else:
    model = GPT2LMHeadModel.from_pretrained(model_type)
    model.config.pad_token_id = model.config.eos_token_id  # suppress a warning

# ship model to device and set to eval mode
model.to(device)
model.eval()


def generate(prompt='', num_samples=10, steps=200, do_sample=True):
    # tokenize the input prompt into integer input sequence
    if use_mingpt:
        tokenizer = BPETokenizer()
        if prompt == '':
            # to create unconditional samples...
            # manually create a tensor with only the special <|endoftext|> token
            # similar to what openai's code does here
            # https://github.com/openai/gpt-2/blob/master/src/generate_unconditional_samples.py
            x = torch.tensor([[tokenizer.encoder.encoder['<|endoftext|>']]], dtype=torch.long)
        else:
            x = tokenizer(prompt).to(device)
    else:
        tokenizer = GPT2Tokenizer.from_pretrained(model_type)
        if prompt == '':
            # to create unconditional samples...
            # huggingface/transformers tokenizer special cases these strings
            prompt = '<|endoftext|>'
        encoded_input = tokenizer(prompt, return_tensors='pt').to(device)
        x = encoded_input['input_ids']

    # we'll process all desired num_samples in a batch, so expand out the batch dim
    x = x.expand(num_samples, -1)

    # forward the model `steps` times to get samples, in a batch
    y = model.generate(x, max_new_tokens=steps, do_sample=do_sample, top_k=40)

    for i in range(num_samples):
        out = tokenizer.decode(y[i].cpu().squeeze())
        print('-' * 80)
        print(out)


if __name__ == '__main__':
    generate(prompt='sam altman,the', )

如上所示输入“sam altman,the”,给出来10条结果,给出来的结果还是五花八门的,挺有意思的。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
--------------------------------------------------------------------------------
sam altman,the_masta_smith,andy_palin (8:48:23 PM): im gonna get some drinks

ghostsrrelz (8:48:48 PM): whats your username?

sweet_erin78 (8:48:52 PM): xxx

ghostsrrelz (8:49:09 PM): im like 12


8:48 PM (s2l) is dead

sasquatch0010 (8:49:14 PM): who is it?

sweet_erin78 (8:49:24 PM): :3

sasquatch0010 (8:49:40 PM): idk im pretty cool

sweet_erin78 (8:49:52 PM): :3

8:48 PM (s2l) has joined the chat.


8:48 PM (2b2t) has joined the chat.


8:48
--------------------------------------------------------------------------------
sam altman,the man who did the hard manning


"When the time will come when our men should no longer be necessary in battle, I will begin to write the words I have promised them


to be my son, and my spirit will roam over them with wild wings, and with joy of glory


of soul and body."


-Joseph Smith, Journal of Discourses, #20


Joseph Smith, the man who did the hard manning, who helped lay the foundation of the Church, was born in Illinois on July 23, 1843. From the birth of his mother, Mrs. Joseph Young was taught that "our fathers laid the foundations of the kingdom." In fact, according to the teachings of Joseph Smith, their name is "The Smith Family":

"We are of the same family; and all were of the seed of Abraham; and we were created for the purpose of setting the man upon the earth, in order to execute the work of the Lord
--------------------------------------------------------------------------------
sam altman,the world is a better place then it is today; you are welcome at the top.the only thing missing is me.

Anonymous 09/08/17 (Fri) 14:50:36 AM No. 232353 >>232354

I've already talked about the whole lack of social anxiety (which isn't really a thing, it's like how there's always been anxiety and fear about being alone with yourself) but this is such a poor idea.

What does this actually do to someone? In any case, she's definitely a bad motherfucker. She should be arrested for child abuse. I've already talked about the whole lack of social anxiety (which isn't really a thing, it's like how there's always been anxiety and fear about being alone with yourself) but this is such a poor idea.What does this actually do to someone? In any case, she's definitely a bad motherfucker. She should be arrested for child abuse.
--------------------------------------------------------------------------------
sam altman,the band in question,will join the ranks of the deceased here as well as an incredible lineup that includes members from the band's previous incarnation,the legendary psychedelic punk quintet,The Velvets. Their new material showcases their best work in more subtle ways while not completely eliminating any of these elements that helped define the past three-plus decades of punk rock. "There's so much to love here," says bassist/pianist/guitarist Alex Jones. "This album might be their most diverse yet. There's a ton of different sounds and emotions mixed in with some of their classic songs like"I'm the One,"("A Million Voices"),and"Ain't That Love" (not sure why I kept listening to this one). There's a lot of great live energy right now and they still have that punk rock energy that is on point, but they also show a new direction. I like how they have that attitude of, 'hey, there is nothing wrong
--------------------------------------------------------------------------------
sam altman,the daniel dreier quote,the santoku quote,santa bellino,so many others. I guess that would be my top ten. Anytime I write something I post it HERE, so stay tuned for updates. Here a few more links; http://www.danieldreier.com/how-do-you-work-harder-for-cheap-money/ http://www.zdnet.com/the-how-to-work-harder-for-cheap-money-a24753926 http://www.nytimes.com/2007/09/19/top-10-less-compelling-debunkers-of-cheap-money.html?pagewanted=all http://www.money.co.nz/how-to-know-how-much-money-you-need-to-make.html http://www.money.co.nz/
--------------------------------------------------------------------------------
sam altman,the "S.A." team)


It's about my life at this point:


-A lot of the time I'm with my mom, I'm a little nervous, I don't really know what to do, I get frustrated and I try to hide that.


-I have a lot of things I miss, like going outside, the sun doesn't always seem to be so bright here sometimes, the music is pretty quiet sometimes.


-The only time I go anywhere is the day I went to high school.


-I was raised in New Jersey, my sister's family moved here recently, and we moved here after our parents left us. My mom still works at the DMV, and she knows a lot of the guys here.


-I guess I'm in between, not really sure what to do, but I'm trying to be patient, because this is just a big city.


-It's tough in some ways because
--------------------------------------------------------------------------------
sam altman,the author of the book 'How to get rich in the bitcoin economy?'

I recently found myself being approached by a bitcoin trader. He was asking the price of bitcoin to be set at 1000. While my response was that its value cannot be increased by 1,000 since the price is already 1000. I was not going to give bitcoin that advice. He said if I said that in any way I'll lose my money. He was not interested in learning anything.

I am not interested in his idea though.

He simply wanted to know how much Bitcoin he could get. I can only tell him that because there are no exchanges to trade bitcoins with. I had no idea what he was talking about in that respect and was curious as to the reasoning behind it.

I found our conversation entertaining. He mentioned he was a big fan of the concept and wanted to know if the idea could work here in my market. I said sure sure I'll keep my word to do
--------------------------------------------------------------------------------
sam altman,the most recent episode was all about, well, making art again and making a good show, but you should watch it before you get any more into it, because you are going to find that it is great.The show was filmed and mixed in LA on a $500,000 budget, so even with just your standard crappy studio shots and effects, it is the kind of show that you will want to go see if you can and maybe do a bunch of other things with it. You are not in Los Angeles. You are in Scotland. I was in Glasgow so... and here's the kicker. The show is really good. I am not sure you'll agree, but I think it is one of the better-made horror series on television.I thought the story of that episode was brilliant and it was a good way to introduce the idea of "we're a group of guys who play these games, we have a game for them and we're trying to stop evil from going all over
--------------------------------------------------------------------------------
sam altman,the artist,the artist,was a brilliant artist.she was a master.she was a great artist.he is indeed amazing,his work has been so many times published in several newspapers and journals such places as,the journal-magazine , the magazine ,... she is the best artist ever.i love this artist,she is really amazing


What is the secret of his work?

There are several secrets of a person,the best way is with the art,the art is the essence,the art is the spirit.


The secret is his talent,his talent,the art is so rare,even if it is a simple painting,it's still a masterpiece.


So the secret can be divided into two different types of secret:

· The secret "can only be seen."he cannot tell a soul.


·The secret "must be done to be seen."he must do everything to be viewed.


·The secret "has
--------------------------------------------------------------------------------
sam altman,the_great_sweden,1,110618,btc,11/14/17 11:06,,www.reddit.com/r/btc/comments/7cbxwc,11/14/2017 5:03,0,Is everyone's bitcoin wallet hacked at the same time? 10731916,0,Bitcoin price crash on MtGox,11/14/17 11:06,,www.reddit.com/r/btc/comments/7cbxwc,11/14/2017 5:02,1,[Bitcoin cash] People are selling up on Bitcoin cash for good 10731917,0,Bitcoin mining speed surge on Bitmain,11/14/17 11:07,,www.reddit.com/r/btc/comments/7cbxwc,11/14/2017 5:02,0,What can you do to help Bitcoin. This is getting out of control. The
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023/02/17 17:48:39,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 最新医学影像技术 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
LLM智能体开发指南
除非你一直生活在岩石下,否则你一定听说过像 Auto-GPT 和 MetaGPT 这样的项目。 这些是社区为使 GPT-4 完全自治而做出的尝试。在其最原始的形式中,代理基本上是文本到任务。你输入一个任务描述,比如“给我做一个贪吃蛇游戏”,并使用LLM作为它的大脑和一些围绕它构建的工具,你就得到了自己的贪吃蛇游戏! 看,连我也做了一个!
xiangzhihong
2024/05/24
2610
LLM智能体开发指南
[干货] 一文介绍如何训练GPT2,让自己的数据会说话
作为ChatGPT之前的版本,GPT-2是由OpenAI于2019年发布的人工智能技术,它可以自动生成文本,理解语言并生成连贯的文本回应。它可以用于各种文本生成任务,如文章创作、对话生成和翻译等。它是一个在github的开源项目。
数智圈
2023/10/02
1.9K1
[干货] 一文介绍如何训练GPT2,让自己的数据会说话
如何微调GPT-2生成高质量的歌词
生成预训Transformer2 (GPT-2),顾名思义,是基于Transformer 的。它使用注意力机制,这意味着它学会关注与上下文最相关的前一个单词,以便预测下一个单词。
deephub
2021/05/18
1.2K0
大语言模型 RLHF 技术的深度解析:从理论到实践的范式革命
在人工智能领域,人类反馈强化学习(Reinforcement Learning from Human Feedback,RLHF)已成为大语言模型(Large Language Models)进化过程中最具突破性的技术之一。这项技术不仅重新定义了人机协作的边界,更在模型伦理对齐、输出可控性、应用场景扩展等维度展现出独特价值。本文将通过技术解构、案例分析和代码实现三个维度,深入揭示 RLHF 的核心机理与实践应用。
编程小妖女
2025/02/03
2830
大语言模型 RLHF 技术的深度解析:从理论到实践的范式革命
4张图片就可以微调扩散模型
稳定扩散模型因其从文本描述生成高质量、多样化图像的能力而获得了极大的关注。但是这些预训练模型在生成高度定制或个性化主题的图像时可能会有所不足。
deephub
2024/02/21
5760
4张图片就可以微调扩散模型
通过Python实现ChatGPT4模型的微调来提高生成文本的质量
ChatGPT4是目前最先进的基于Transformer的语言模型之一,其在自然语言处理任务中表现良好。但是,由于如此之高的复杂性和大量的参数,ChatGPT4对于一些具体应用场景的优化可能达不到我们的预期。因此,微调(Fine-tune)是一种常用的技术,可以根据特定任务使用实际数据集进一步优化模型。
华科云商小彭
2023/10/13
7520
通过Python实现ChatGPT4模型的微调来提高生成文本的质量
【多模态大模型实战】 搭建DeepSeek Janus-Pro 7B 多模态模型,以及推理微调,推理后的模型融合
Janus-Pro是DeepSeek最新开源的多模态模型,是一种新颖的自回归框架,统一了多模态理解和生成。通过将视觉编码解耦为独立的路径,同时仍然使用单一的、统一的变压器架构进行处理,该框架解决了先前方法的局限性。这种解耦不仅缓解了视觉编码器在理解和生成中的角色冲突,还增强了框架的灵活性。Janus-Pro 超过了以前的统一模型,并且匹配或超过了特定任务模型的性能。
AI浩
2025/02/08
2.7K1
【多模态大模型实战】 搭建DeepSeek Janus-Pro 7B 多模态模型,以及推理微调,推理后的模型融合
文本生成:AIGC中的应用场景
人工智能生成内容(AIGC)技术正在迅速发展,其中“文本生成”是最具影响力和应用前景的领域之一。从新闻报道、社交媒体帖子到小说创作、技术文档,文本生成在各个行业中都发挥着重要作用。本文将深入探讨文本生成的应用场景、技术实现及其潜在价值,并提供代码示例,帮助读者更好地理解这一技术。
世间万物皆对象
2025/03/15
4051
连招来了,DeepSeek发布图像视频大模型Janus-Pro-7B,硬刚OpenAI DALL-E 3
DeepSeek前脚刚发布R1模型,又赶在大年三十发布了最新的多模态大模型Janus-Pro-7B,支持理解生成图片视频,这可能又是一个爆炸性的产品,要知道R1已经在海内外火的一塌糊涂,让英伟达股票大跌10%以上,连ChatGPT都回应:排名变化也许是暂时的。
派大星的数据屋
2025/02/05
3460
连招来了,DeepSeek发布图像视频大模型Janus-Pro-7B,硬刚OpenAI DALL-E 3
NLPer,你知道最近很火的自然语言处理库么?
【磐创AI 导读】:本文介绍了最先进的自然语言处理库——PyTorch-Transformers
磐创AI
2019/09/11
1.3K0
NLPer,你知道最近很火的自然语言处理库么?
如何用 AI 问答 API 彻底改变用户体验!
我们知道,市面上一些问答 API 的对接还是相对没那么容易的,比如说 OpenAI 的 Chat Completions API,它有一个 messages 字段,如果要完成连续对话,需要我们把所有的上下文历史全部传递,同时还需要处理 Token 超出限制的问题。
崔庆才
2024/05/18
2440
如何用 AI 问答 API 彻底改变用户体验!
大佬开撕:OpenAI vs 马斯克
原文地址:https://openai.com/blog/openai-elon-musk。
查克
2024/12/06
1190
大佬开撕:OpenAI vs 马斯克
60行代码就能构建GPT!网友:比之前的教程都要清晰|附代码
Pine 发自 凹非寺 量子位 | 公众号 QbitAI 现在只用60行代码,就能从0构建GPT了! 想当初,前特斯拉前AI总监的minGPT和nanoGPT也都还要300行代码。 这个60行代码的GPT也有名字,博主将它命名为PicoGPT。 不过和此前minGPT和nanoGPT的教程不同,今天要讲的这个博主的教程,更侧重于代码实现部分,模型的权重则用已经训练好的。 对此,博主解释称这篇教程的重点在于提供一个简单且易于破解的完整技术介绍。 这对还不理解GPT背后概念的盆友,算是非常友好了。 还有网友
量子位
2023/02/23
4990
60行代码就能构建GPT!网友:比之前的教程都要清晰|附代码
LLM 大模型学习必知必会系列(十三):基于SWIFT的VLLM推理加速与部署实战
使用LoRA进行微调的模型你需要先merge-lora, 产生完整的checkpoint目录.
汀丶人工智能
2024/05/29
1.2K0
LLM 大模型学习必知必会系列(十三):基于SWIFT的VLLM推理加速与部署实战
在自定义数据集上微调Alpaca和LLaMA
本文将介绍使用LoRa在本地机器上微调Alpaca和LLaMA,我们将介绍在特定数据集上对Alpaca LoRa进行微调的整个过程,本文将涵盖数据处理、模型训练和使用流行的自然语言处理库(如Transformers和hugs Face)进行评估。此外还将介绍如何使用grado应用程序部署和测试模型。
deephub
2023/08/30
1.5K0
在自定义数据集上微调Alpaca和LLaMA
零基础入门:DeepSeek微调的评测教程来了!
三天前,看到了我们 Datawhale 公众号上发了文章《零基础入门:DeepSeek 微调教程来了!》反响很好,其中的内容写的非常接地气,适合学习者进行学习体验。
Datawhale
2025/02/28
3060
零基础入门:DeepSeek微调的评测教程来了!
大语言模型量化方法对比:GPTQ、GGUF、AWQ
在过去的一年里,大型语言模型(llm)有了飞速的发展,在本文中,我们将探讨几种(量化)的方式,除此以外,还会介绍分片及不同的保存和压缩策略。
deephub
2023/11/20
10.6K0
大语言模型量化方法对比:GPTQ、GGUF、AWQ
Transformers2.0让你三行代码调用语言模型,兼容TF2.0和PyTorch
参考链接: 在Python中使用BERT Tokenizer和TensorFlow 2.0进行文本分类
用户7886150
2021/01/15
1.2K0
Transformers 4.37 中文文档(二)
除了🤗 Transformers 的 notebooks 之外,还有示例脚本演示如何使用PyTorch、TensorFlow或JAX/Flax训练模型的方法。
ApacheCN_飞龙
2024/06/26
7430
Transformers 4.37 中文文档(二)
Transformer 模型实用介绍:BERT
在 NLP 中,Transformer 模型架构是一场革命,极大地增强了理解和生成文本信息的能力。
数据科学工厂
2023/08/10
5990
Transformer 模型实用介绍:BERT
推荐阅读
相关推荐
LLM智能体开发指南
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验