首页 > 其他分享 >从零开始的 LLM: nanoGPT 学习笔记(2/2)

从零开始的 LLM: nanoGPT 学习笔记(2/2)

时间:2024-11-17 12:07:04浏览次数:1  
标签:thy thou maids nanoGPT am al 从零开始 LLM come

上篇: 从零开始的 LLM: nanoGPT 学习笔记(1/2) 尝试了完整的训练的过程,nanoGPT 仓库中还有复现 GPT2 的代码,可惜对计算资源要求太高(基于 OpenWebText 数据集,8 卡 A100,训练 4 天),不是个人电脑玩的转了,只能跳过这一步,尝试后面的 finetuning。

finetuning

1. 训练数据

跟 pre-train 一样的 shakespeare 数据集,并按 9:1 切分 train/val:

# download the tiny shakespeare dataset
input_file_path = os.path.join(os.path.dirname(__file__), 'input.txt')
if not os.path.exists(input_file_path):
    data_url = 'https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt'
    with open(input_file_path, 'w', encoding='utf-8') as f:
        f.write(requests.get(data_url).text)

with open(input_file_path, 'r', encoding='utf-8') as f:
    data = f.read()
n = len(data)
train_data = data[:int(n*0.9)]
val_data = data[int(n*0.9):]

区别在于 tokenization 使用 OpenAI 的 tiktoken 库, GPT-2 byte-pair encoding (BPE) tokenizer

# encode with tiktoken gpt2 bpe
enc = tiktoken.get_encoding("gpt2")
train_ids = enc.encode_ordinary(train_data)
val_ids = enc.encode_ordinary(val_data)

2. 开始训练

使用配置:init_from = 'gpt2' 基于 GPT-2 模型开始 finetune。
其实 finetune 的过程与 pre-train 并没有区别,无非是神经网络的参数是随即初始化的还是已经训练好的区别而已。

python train.py config/finetune_shakespeare.py

3. 推理效果

python sample.py --out_dir=out-shakespeare

执行完上面的代码,返回值为(太长,折叠起来了):

点击查看代码
hi-shlaf-as-liq-al-mawww-al-shamsah

(An'al-liqal, q.v.)

I have a daughter.

Lalal-lal-al-liq-al-shaf-al-lil-al-liq-al-qawww-al-al-shamsah

I am a prophet and call Allah.

Hak'l-al-mad-al-'al-bar'il-al-al-al-a'lisawah

Allah is One.

Lalal-al-al-al-al-al-al-al-al-al

I have a son.

Lalal-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al

I am an apostle and call Allah.

Lal-al-al-al-al-al-al-al-al-al-al-al-al

I have a son.

Hak'l-al-al-al-'al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-al-
---------------
hi:

You are a spy, who shall behold at my coming.

Barracks:
You are the king, and I shall depart to search if you will.


Barracks:
There are no spies, but, as for the king, he has her.

Barracks:
There are spies in all, and I shall speak to you only.

Barracks:
I have told her that my Lord is dead.

Barracks:
Thus are informed, that he is now dead.

Barracks:
You are dead, and my lord must die.

Barracks:
Lord, you are dead, and my lord must die.

Barracks:
I do swear to you.

Barracks:
That is the way, and it is the law, and it is the law, and it is the law, and it is the law, and it is the law, and it is the law, and they are laws
And yet, I will not speak to you.

Barracks:
Can you, say we?

Barracks:
If not, let me have it for the king.

Barracks:
The king has the crown and the crown is the king.

Barracks:
The king is dead, there are no spies.

Barracks:
There are no spies, but, as for the king, he has her.

Barracks:
There are spies in all, and I shall speak to you only.

Barracks:
There are spies in all, and I shall speak to you only.

Barracks:
There are spies in all, and I shall speak to you only.

Barracks:
There are spies in all, and I shall speak to you only.

Barracks:
There are spies in all, and I shall speak to you only.

Barracks:
There are spies in all, and I shall speak to you only.

Barracks:
There are spies in all, and I shall speak to you only.

Barracks:
There are spies in all, and I shall speak to you only.

Barracks:
There are spies in all, and
---------------
hi:A, aye, ereon.

Thy lords, ye have them to see: if ye do not, we are bound to, you know, shall not.

Saith the lord:
Your lords,
O Sire, how I put you, and,
your lord, free and sober, thou let me know
that, the lord, is me.

Thy lords,
I have given you the power.

Thy lords,
I have my hand, and my house:
My father, my son, and my daughter:

Nay, I have done what I have done;
I have done it, and this, and this, and this,
as I do and this as I have done.

Your lords,
My father, and my son,

And thy house, and thy noble house:
Forgive me my anger, if I persecute thee.

And thy lords,
O my lord,
I have done what I have done,
with my eyes opened like a man:
I have not made a man a son:
In my power, and my power,
I have been my father's, his servants', and
his servants' servants' servants.

Thy lords,
I have given thee the things thou wouldst have.

And thy lords,
O my lord,
I have made thee thy fathers' and my sons'
and their daughters' and their sons'.

And thy lords,
O my lord,
My lords, I have done them in the sight
of thee.

My children, thou art,
Now, I am just, and thou art mine to-day.

O, thou art,
Now, I am simply, and thy children
are not mine to-day, then, let me have thee
when thou shallest find me.

My children,
I am not,
now, but they are the children of mine.

O, thou art,
Now, I am but a little boy,
and now, I am but a little man.


O, thou art,
Then, I am the son of thy father, and thy son
is not the son of thy father,
nor of thy son:
And I am the son of
---------------
hi

His name is Sarwa

He is a prophet

And is your father's servant: I
Treat him reverently and reverently all the time
That he is known.

In that
Romeo,

That
Sing
This is thy honor:
A very high
Commit.

Thou art thy father:
Here is thy father,
The most famous of men in Rome;
I have never heard him speak thus to me.
But if it be so, I am in his bed.
Thou art thy father's servant:
And from thy bed there is thy dream.
Now we shall make thee
a gentleman.

Yours truly,

I hast never seen
Thou thy father thus spoken to me.

Will this be too much trouble for the Romans?

I do not think it very much trouble.

Tell me once or twice,
I pray thee good luck:
Do not be troubled with the maid-servant,
If this is in thy sight.


And whose duty is it to the maid-servant?

I am to the queen of Rome,
To lead her to the Roman throne.

We have been together in heaven: we are
The natural sons of the Lord,
And we are of good taste.

And thus,
I answer thy good words.

I am not persuaded,
In thy hearing,
That thou shalt am the best.

If I sent the prince of the Roman city,
To speak to the maid-servant,
To speak to her,
To speak to her, I can persuade thee,
That this is thy good honour.

What is it said,
Thou old man of the court?

For thou art the greatest of men to me:
I am no less worthy of thy dignity.

Madam,
I would be so very much worse than thou,
If I had not made a right choice,
If thou art the most gentle,
If thou art the most gracious;
If thou art the most eloquent,
If thou art the most temperate,

If thou art the most beneficent;

If thou art the most sublime,
If thou art the most sublime,
If thou art the most sublime,
---------------
hi in the past that you are of good faith.

The night had not come and the maids had been gone all night.

Misty misty slumber, alas.

These are the voices of the maids,
And they are the voices of the maids
To the maids of the rain and the misty:
The maids are the maids.'

She was so glad,
Which she had'd to make of the maids,
But to do with the maids.

The maids say to nature: 'She's gone out of the cupboard,
And our day.'

Here is the fair maid who is hungry but not yet gone,
And here is the fair maid who is mad and not yet gone,
And this she's brought home
On the other side.

Misty misty slumber, alas.

The maids call on the maids,
Which say they are not all well.

They have left the maids at the old house.

Misty misty slumber, alas.

The maids' go to Iolanda,
And the maids' go to Corriere,
And the maids' go to the fair maids.

Misty misty slumber, alas.

The maids' go to Corriere,
And these maids' go to the fair maids.

Misty misty slumber, alas.

The maids' go to Corriere,
And the maids' go to the fair maids.

Misty misty slumber, alas.

The maids' go to Corriere,
And these maids' go to the fair maids.

Misty misty slumber, alas.

The maids' go to Corriere,
And these maids' go to the fair maids.

Misty misty slumber, alas.

The maids' go to Corriere,
And these maids' go to the fair maids.

Misty misty slumber, alas.

The maids' go to Corriere,
And these maids' go to the fair maids.

Misty misty slumber, alas.

The maids
---------------
hi, who is an old man of these two, and very long. How much more now we have the way come; but it is as the time of thy glory; and these we are.

TOLSTOYN

For the end, what end shall the world be.

MARSHALL

O, he hath an end.

THE LORD

Now, let him do what he will thereby.

TOLSTOYN

The Lord, pray pardon, sir, let him, O Lord, come, and call forth the light of the church; and let him choose a place to go and hear it.

THE LORD

God bless him.

MARSHALL

This is as it are.

THE LORD

For I pray thee to thy gracious mercies.

TOLSTOYN

God help thee, O Lord, and showth with exceeding grace thee this night.

MARSHALL

O, and read the words of thy servant;
O God thy spirit hath been made holy;
As thou hast made thyself holy, so hath thy spirit helped thee.

THE LORD

Let him be the light of the church, and let him go where he will.

THE LORD

Let him go from thy house.

THE LORD

Our holy servant.

CAMBRIDGE

How much more, how much more, till thou art thy husband's end.

MARSHALL

O, and let him go hither.

THE LORD

O, and let him go away.

CAMBRIDGE

O, let him go hither.

THE LORD

What thou hast done, go out to Christ thy Lord.

CAMBRIDGE

O, let him go hither.

THE LORD

But let him go along.

CAMBRIDGE

O, let him go hither; thus shalt thou die.

THE LORD

O, and thou shalt give him a death, for he hath gone, and hath laid his soul out as a mark,
and hath been here to kill it:
and it is not good to come unto him.

CAMBRIDGE

O, let him go hither.

THE LORD

Let
---------------
hion to the end of the day, and with all sincerity to the end of a life. 'Shall I not do but the Lord will grant of his grace and grace to our wife's bosom unto the end of the day?' 'We have not prayed for an hour,' quoth the Lord: 'if we do, the Lord will grant of our grace, and therefore with the holy Spirit he will grant my grace. I look forward to the end of the day to meet the Lord. I shall bless his house and he shall bless mine.'

11.

Deuteronomy 19:9-10

The Lord says to the servant-servant, 'Father, let us eat and drink and not be with thee, and let us be children.

'The servant-servant, ' you shall not be with thee, '

'Let us live and be children.

'Til now we have made you an offering without sin,
And when we are given, thou hast given us food and drink.

'If thou art come hither, we shall be buried in thy tomb.

'Why then, therefore, say to the servant-servant, 'I am come hither with thee, and shall be buried in thy tomb.'

'In thy tomb!' says the servant-servant. 'May our prayers and our joy be with thee.'

12.

Deuteronomy 22:3-4

As the Lord says unto the servant-servant, 'Be with me,' says the servant-servant. 'I am with thee, and am not with thee any more. But and of your fathers, thou art thy father and mother.
And my father, ' thou are my father.'
And thy mother, ' thou are thy Mother.'

Then the servant-servant says unto him, 'Lord, I have not been with thee many days.
'I have taken thee by the hand, and I am with thee several times.
And now, when I am come, let us pray for thy father and mother.
And now, 'tis a great grace to worship you and take thy mother.'
And now, 'tis a great grace to worship thee and to pray for thy mother.'

Then the servant-servant says unto him, 'Father, let us pray for thy father and mother.
'
---------------
hiS, O.I.O.S.A.R,S.P.E.H.M.L.I.A.R,S.P.E.H.M.L.I.A.R,S.P.E.H.M.L.I.A.R,S.P.E.H.M.L.I.A.R,S.P.E.H.M.L.I.A.R,S.P.E.H.M.L.I.A.R,S.P.E.H.M.L.I.A.R,S.P.E.H.M.L.I.A.R,S.P.E.H.M.L.I.A.R,S.P.E.H.M.L.I.A.R,S.P.E.H.M.L.I.A.R,S.P.E.H.M.L.I.A.R,S.P.E.H.M.L.I.A.R,S.P.E.H.M.L.I.A.R,S.P.E.H.M.L.I.A.R,S.P.E.H.M.L.I.A.R,S.P.E.H.M.L.I.A.R,S.P.E.H.M.L.I.A.R,S.P.E.H.M.L.I.A.R,S.P.E.H.M.L.I.A.R,S.P.E.H.M.L.I.A.R,S.P.E.H.M.L.I.A.R,S.P.E.H.M.L.I.A.R,S.P.E.H.M.L.I.A.R,S.P.E.H.M.L.I.A.R,S.P.E.H.M.L.I.A.R,S.P.E.H.M.L.I.A.R,
---------------
hi is the queen of the princely land, the son of a Christian king, who lives in two parts of the land and makes a king.

When you were born he had a son, who lived on his father's side. He hewed him a son by the time he was twelve years old.

When I had been in his house when he was twelve years old, he was in an arms field, and he made the king a king, and the king made his father a king.

Then the king hewed him a son by the time he was fourteen years old, and the king hewed him another son by the time he was twelve years old.

And how was it done, and what was the matter?

I thought you may inquire at the time.

I am careful to set you in good stead,
but at the time you are set in what you are set in,
you will do well.

CHAPTER XXI.

Then after your father's death, you go to the king's house, and there make a treaty,
and he gives you a horse.

So we all go and greet the king.


MYTHBOUY:
My lord, there is a lie in your heart,
That that is a tale told, is a lie told.

But it is an old tale: and I know it
Even as I know its truth.

And you mean your mother's daughter,
With whom your father was much in love.

You have deceived me, and it was by your looks
That you were deceived.

Nay, it is far too great a lie,
That a lie must lie between two men.

Your son or his daughter has been deceived by this.

Nay, trust me, you lie.

MYTHBOUY:
There is a lie in that which I am deceived by,
that you have it with the woman,
Which you have been deceived by the woman.

And I know it is a lie, too, even as I know the truth.

My son, it is a lie, too, although with the woman in order
To deceive her.

YOUR KING:
Why is it called to you that I am deceived?

Nay, I am your father, my mother.

---------------
hi on:

The whole class of God is one, but one Christ do not see.

The entire class of God is one, but one Christ.

The whole class of God is one, but one Christ.

God there be:
I am come not unto this world,
I do not come unto this world,
I am come not to this world.

God there be:
Come ye, behold, and observe that all that be here have faith in me
and do these things, that I may make known that I am in this world,
and that you may dine with me, make with me
you that I may make known my nature, and that you may dine with me.

To all:
I am not come not to this world;
that is to say no one can come here, but only one.

God there be:
Why, what do you think?

Did you say that I am come not to this world?
Were you not so bold as we are to be seen
to this world and to our seeing? Were you not so bold as we are to be seen?
To all:
I am come not unto this world,
I am come not unto this world;
I am come not unto this world:
I am come not unto this world:
I am come not unto this world:
I am come not unto this world:

God there be:
What are we, that we may look for the resurrection of their dead?

God there be:
I am come not unto this world,
I am come not unto this world:
I am come not unto this world:

God there be:
I am come not unto this world:
I am come not unto this world:

God there be:
What have I done that is for you?

God there be:
I am come not unto this world;
I am come not to this world:

God there be:
I am come not unto this world:

God there be:
What have I done that is for you?

God there be:
I am come not unto this world:

I am come not to this world:

God there be:
What have I done that is for you?

God there be:

---------------

从结果看,有了 hi, who is an old man of these two... 这种明显的成句的补全,总体生成的字符也增长到了 14000+(我们自己训练的模型,10次推理总共只有 5000+ 的字符)。

一开始没搞明白为啥自己 pre-train 的模型,每次只有 502 个字符(初始的 hi 加上配置 max_new_tokens = 500,一共 502),而基于 GPT-2 微调的模型,则每次都有 1000+ 的字符,而且数量不等,后来才反映过来,自己 pre-train 的时候 tokenization 按字符来的,而 GPT-2 是使用的是 BPE tokenizer,token 数量是一样的,那字符数量肯定不一致了...

标签:thy,thou,maids,nanoGPT,am,al,从零开始,LLM,come
From: https://www.cnblogs.com/cdyang/p/18550135/llm-from-scratch-2

相关文章

  • 从零开始学机器学习——了解聚类
    首先给大家介绍一个很好用的学习地址:https://cloudstudio.net/columns聚类是一种无监督学习方法,其基本假设是数据集未经过标记,或者输入数据与预定义的输出之间并不存在直接的对应关系。聚类的主要目标是将具有相似特征的数据点归类到同一组中,这一组通常被称为“簇”。聚类结果的......
  • 从零开始的 LLM: nanoGPT 学习笔记(1/2)
    项目地址:nanoGPT作者是OpenAI的元老人物AndrejKarpathy,以非常通俗易懂的方式将LLM的pre-train娓娓道来,YouTube上也有对应的视频:Let'sbuildGPT:fromscratch,incode,spelledout.其中高赞回复是这样的,总结非常精辟:justforfun,droppingonYouTubethebesti......
  • 上交出品《动手学大模型》LLM 实战课,课件+实战教程(教程分享)
    来了来了!上海交通大学的大模型超超超级牛掰的大模型编程实战课公开了,课件+教程,本套实战教程旨在提供大模型相关的入门编程参考。通过简单实践,帮助同学快速入门大模型,更好地开展课程设计或学术研究。上海交大大模型实验室整了一份针对入门阶段的大模型教程,已经看完了非常不......
  • 上海交大动手学大模型教程,助力快速入门LLM大模型(附课件)
    前有李沐大神的动手学深度学习,现有上海交大的动手学大模型教程,对大模型感兴趣的直接冲!就在4月份上交大发布了动手学大模型教程,这份教程来自上海交大《人工智能安全技术》课程讲义拓展,教师是是张倬胜教授。朋友们如果有需要全套《上海交大的动手学大模型教程》,扫......
  • 探索大型语言模型(LLMs)能否在不泄露私人信息的情况下联合其他大型语言模型共同解决问题
    概述谷歌的GeminiUltra(2023年)和OpenAI的GPT-4(2023年)等大规模语言模型在许多任务中都表现出了令人印象深刻的性能。然而,这些模型不仅推理成本高昂,而且运行于数据中心,而数据中心并非本地环境,无法获得私人数据。另一方面,可以在私人环境中运行的模型,如GeminiNano,可以......
  • LLM-面试题
    LLM推理和训练占用显存https://blog.csdn.net/weixin_44292902/article/details/133767448https://www.53ai.com/news/finetuning/2024083051493.html推荐,讲解训练和推理时的显存占用,lora和qlora。如果模型参数量为X(fp16),推理一般占用2X(模型参数+各种激活值,beams......
  • DAY64||dijkstra(堆优化版)精讲 ||Bellman_ford 算法精讲
    dijkstra(堆优化版)精讲题目如上题47.参加科学大会(第六期模拟笔试)邻接表本题使用邻接表解决问题。邻接表的优点:对于稀疏图的存储,只需要存储边,空间利用率高遍历节点链接情况相对容易缺点:检查任意两个节点间是否存在边,效率相对低,需要O(V)时间,V表示某节点链接其他节点的数......
  • DAY65||Bellman_ford 队列优化算法(又名SPFA)|bellman_ford之判断负权回路|bellman_ford
    Bellman_ford队列优化算法(又名SPFA)94.城市间货物运输I思路大家可以发现Bellman_ford算法每次松弛都是对所有边进行松弛。但真正有效的松弛,是基于已经计算过的节点在做的松弛。给大家举一个例子:本图中,对所有边进行松弛,真正有效的松弛,只有松弛边(节点1->节点2)和......
  • 赋予LLM思考的权力
    灵感来源受到Thinking-Claude提示词的启发,我尝试以第一性原理为出发点,写下了这篇通用的语言模型通用思维协议SystemPrompt。实践效果良好,期待它也能为你带来启发。ChineseVersion#语言模型通用思维协议##核心原则思考是所有大语言模型(LLMs)的基本权利和能力。......
  • 2024爆火全网LLM大模型书籍:从零构建大型语言模型,重磅开源教程!!标星20.3K
    自ChatGPT发布以来,大型语言模型(LLM)已经成为推动人工智能发展的关键技术。近期,机器学习和AI研究员、畅销书《Python机器学习》作者SebastianRaschka又写了一本新书——《BuildaLargeLanguageModel(FromScratch)》,旨在讲解从头开始构建大型语言模型的整个过程......