掘金 人工智能 05月04日 17:53
为什么微软的 Phi-4 14B 比 GPT-4o 和 o1 强太多了 —— 看结果就知道了
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文介绍了微软最新发布的小型语言模型Phi-4,它以140亿参数的规模,在数学推理等领域表现出色,甚至超过了一些大型语言模型。文章详细阐述了Phi-4的特点、优势、本地部署方法以及与GPT-4o等模型的对比。通过实际案例测试,展示了Phi-4在准确性、简洁性方面的优势,并提供了本地使用的实用指南,适合AI开发者和对模型部署感兴趣的读者。

💡 Phi-4是微软推出的轻量级语言模型,参数量为140亿,但其在数学推理等特定任务上的表现超过了部分大型语言模型,这得益于高质量的数据和创新的后训练技术。

💡 Phi-4在MATH和MGSM测试中得分超过80%,数学推理能力突出,在金融、工程和科研领域具有应用潜力,特别是在逻辑思维和数字计算精度要求高的领域表现出色。

💡 Phi-4模型强调精度与效率,采用纯解码器Transformer架构,在9.8万亿token的混合数据集上训练而成。微软团队使用监督微调和DPO方法,并进行大量安全评估,以确保性能可靠,并降低偏见和错误信息的风险。

💡 本地部署Phi-4非常便捷,可以使用Ollama工具进行量化,即便在普通笔记本电脑上也能运行。文章提供了详细的本地安装步骤,并展示了通过Ollama的REST API功能,实现其他应用和库的本地调用。

🚀 本文由一位4年实战出身的AI软硬件一体化解决者整理,专注从芯片、电路、GPU部署 → Linux系统、推理引擎 → 模型训练应用的完整链路。如果你也想搞懂AI模型部署、做自己的项目,建议收藏本文细读,有问题欢迎交流。

2024 年 1 月 7 日,微软在 Hugging Face 上发布了小型语言模型(SLM)“Phi-4”。这个模型采用 MIT 协议发布,可以自由用于商业用途并加以修改。

Phi-4 是微软“Phi”系列的最新模型,虽然是轻量级的,但在某些领域表现出色。特别是在数学推理方面,它的准确度甚至超过了一些大规模语言模型(LLM),在专门任务上的应用也因此受到关注。

Phi-4 通过高质量的数据和全新的后训练技术提升了准确度,并已发布至 Azure AI Foundry 和 Hugging Face。

这个模型的规模是 14B,如果是 4-bit 量化模型,基本上在一张还可以的显卡上就能跑起来。Meta 的 Llama3.2 和 Google 的 Gemma2 这两个开源模型的最小版本分别是 1B 和 2B,那这个就大了一整圈。

我不太清楚微软对“小型”的定义是什么,不过反正这是个可以自由使用、很好上手的模型。

在这个逐步引导里,我们会讲清楚 Phi-4 是什么、Phi-4 有什么特别之处、怎么在本地用 Phi-4、以及为什么它比 LLM 表现更好。

Phi-4 是什么?

Phi-4 是一种尝试,展示一种与传统 AI 模型不同的路径。它的特点是参数量相对较小,仅有 140 亿,但相比 Google 的 Gemini Pro 1.5 等大规模 AI 模型却表现出更强的性能。

尤其值得注意的是它在数学推理方面的高能力。在由美国数学协会(MAA)主办的美国数学竞赛(AMC)上,Phi-4 在需要逻辑思维的问题(比如复杂方程求解和数学证明)中超过了大模型。

这个结果说明它在那些对数字计算精度要求极高的领域(比如科研、工程、金融建模)中有使用的可能性。

Phi-4 有什么特别之处?

Phi-4 的基准测试结果如下:

另外,Phi-4 的架构和训练过程非常强调精度与效率。它是一个 140 亿参数的纯解码器 Transformer 模型,在一个共计 9.8 万亿 token 的混合真实与合成数据集上训练而成。

在训练方法上,微软团队使用了监督微调和 DPO(直接偏好优化)方法,以确保性能可靠,同时解决公平性和稳定性等问题。

微软强调 Phi-4 的训练过程遵循负责任的 AI 开发原则,并进行了大量的安全评估,包括攻击测试,以降低偏见、有害内容和错误信息的风险。

本地安装

💡 像这种本地部署场景,其实正是我这类AI开发从业者最常解决的问题之一。项目落地中遇到性能瓶颈、模型运行不了、参数不清楚……我们都踩过坑,也做过商业化落地。想搞AI项目但不知道怎么配套软硬件?可以看完这部分内容后私信我聊聊。

在 Hugging Face 上的模型体积相当大,大概有 30GB,我的电脑跑起来有点吃力。

我当时在想是不是可以通过 4-bit 量化之类的方法让它能跑起来,结果发现 Ollama 已经提供了一个量化成 Q4_K_M 的模型,真是谢天谢地,于是我借用了它,并想分享一下我记的操作笔记。

下载

ollama run phi4

注意:模型加载到内存后会超过 10GB,所以如果你在 CPU 上跑,需要有至少那么多的内存空闲;如果你在 GPU 上跑,那 GPU 显存也必须至少这么多,否则会报错。

如果模型下载成功,你会看到这个提示:

Send a message (/? for help)

看到这个提示之后,就可以试着发送消息啦。如果你是在 GPU 加速环境下跑,响应速度会非常快。

Hello!

Hello! Please let me know how I can assist you. If you have any questions, feel free to ask.

Send a message (/? for help)

就这么简单,我现在就能在本地用 Phi-4 啦!这真的太方便了!

现在,你可以用 Ollama 在自己电脑上轻松跑起 Phi-4!甚至还有 REST API!

这就意味着,一个接近 70B 参数性能的模型可以在一台笔记本上跑起来,借助 Ollama 的 REST API 功能,可以很快设置好一个环境,让别的应用和库本地调用。未来怎么用它,还有很多玩法值得期待。

Phi-4 vs GPT-4o vs o1

我们来对比一下 o1 和 GPT-4o,并验证一下 Phi-4 的表现到底如何。

接下来我想给出一些应用问题,看看和 GPT-4o 以及 o1 的结果是不是不一样,以及是否更简洁。

我们要测试的问题如下:

    求以下数列的第 20 项:1, 1, 2, 3, 5, 8, …某银行提供年利率 5%,半年复利一次。如果存入 100 万美元 10 年后是多少?一个长方体体积是 120 立方厘米,高为 6cm,宽为 4cm,求它的长?

问题 1:测试准确率、简洁性、响应时间
问题 2:测试利率计算准确度
问题 3:测试几何推理能力

接下来我们来解题,输入以下内容:

The sequence given is the Fibonacci sequence, where each term is the sum of the two preceding ones, starting with 1 and 1. The first few terms are:

( F_1 = 1 )
( F_2 = 1 )
( F_3 = F_1 + F_2 = 1 + 1 = 2 )
( F_4 = F_2 + F_3 = 1 + 2 = 3 )
( F_5 = F_3 + F_4 = 2 + 3 = 5 )
( F_6 = F_4 + F_5 = 3 + 5 = 8 )
We need to find the 20th term, ( F_{20} ).

Continuing the sequence:

( F_7 = F_5 + F_6 = 5 + 8 = 13 )
( F_8 = F_6 + F_7 = 8 + 13 = 21 )
( F_9 = F_7 + F_8 = 13 + 21 = 34 )
( F_{10} = F_8 + F_9 = 21 + 34 = 55 )
( F_{11} = F_9 + F_{10} = 34 + 55 = 89 )
( F_{12} = F_{10} + F_{11} = 55 + 89 = 144 )
( F_{13} = F_{11} + F_{12} = 89 + 144 = 233 )
( F_{14} = F_{12} + F_{13} = 144 + 233 = 377 )
( F_{15} = F_{13} + F_{14} = 233 + 377 = 610 )
( F_{16} = F_{14} + F_{15} = 377 + 610 = 987 )
( F_{17} = F_{15} + F_{16} = 610 + 987 = 1597 )
( F_{18} = F_{16} + F_{17} = 987 + 1597 = 2584 )
( F_{19} = F_{17} + F_{18} = 1597 + 2584 = 4181 )
( F_{20} = F_{18} + F_{19} = 2584 + 4181 = 6765 )
Thus, the 20th value of the sequence is ( \boxed{6765} ).

Phi-4

The sequence given is the Fibonacci sequence, where each term is the sum of the two preceding ones, starting with 1 and 1. The first few terms are:

( F_1 = 1 )
( F_2 = 1 )
( F_3 = F_1 + F_2 = 1 + 1 = 2 )
( F_4 = F_2 + F_3 = 1 + 2 = 3 )
( F_5 = F_3 + F_4 = 2 + 3 = 5 )
( F_6 = F_4 + F_5 = 3 + 5 = 8 )
We need to find the 20th term, ( F_{20} ).

Continuing the sequence:

( F_7 = F_5 + F_6 = 5 + 8 = 13 )
( F_8 = F_6 + F_7 = 8 + 13 = 21 )
( F_9 = F_7 + F_8 = 13 + 21 = 34 )
( F_{10} = F_8 + F_9 = 21 + 34 = 55 )
( F_{11} = F_9 + F_{10} = 34 + 55 = 89 )
( F_{12} = F_{10} + F_{11} = 55 + 89 = 144 )
( F_{13} = F_{11} + F_{12} = 89 + 144 = 233 )
( F_{14} = F_{12} + F_{13} = 144 + 233 = 377 )
( F_{15} = F_{13} + F_{14} = 233 + 377 = 610 )
( F_{16} = F_{14} + F_{15} = 377 + 610 = 987 )
( F_{17} = F_{15} + F_{16} = 610 + 987 = 1597 )
( F_{18} = F_{16} + F_{17} = 987 + 1597 = 2584 )
( F_{19} = F_{17} + F_{18} = 1597 + 2584 = 4181 )
( F_{20} = F_{18} + F_{19} = 2584 + 4181 = 6765 )
Thus, the 20th value of the sequence is ( \boxed{6765} ).

GPT-4o

The sequence given is the Fibonacci sequence, where each term is the sum of the two preceding terms:

[F(n) = F(n-1) + F(n-2) \quad \text{for } n \geq 3]
with ( F(1) = 1 ) and ( F(2) = 1 ).

We need to compute the 20th value of this sequence.

The first few terms are:

    ( F(1) = 1 )

    ( F(2) = 1 )

    ( F(3) = 2 )

    ( F(4) = 3 )

    ( F(5) = 5 )

    ( F(6) = 8 )

    ( F(7) = 13 )

    ( F(8) = 21 )

    ( F(9) = 34 )

    ( F(10) = 55 )

    ( F(11) = 89 )

    ( F(12) = 144 )

    ( F(13) = 233 )

    ( F(14) = 377 )

    ( F(15) = 610 )

    ( F(16) = 987 )

    ( F(17) = 1597 )

    ( F(18) = 2584 )

    ( F(19) = 4181 )

    ( F(20) = 6765 )

Thus, the 20th value in the sequence is: 6765

o1

The given sequence is the Fibonacci sequence, defined by:

[F 1 = 1, \quad F 2 = 1, \quad \text{and} \quad F {n} = F {n-1} + F *_{n-2} \text{ for } n > 2.]

To find the 20th term, let's list them out (or recall the known values):

    ( F_* 1 = 1 )( F *_2 = 1 )( F_* 3 = 2 )( F *_4 = 3 )( F_* 5 = 5 )( F *_6 = 8 )( F_* 7 = 13 )( F *_8 = 21 )( F_* 9 = 34 )( F *_{10} = 55 )( F_* {11} = 89 )( F *_{12} = 144 )( F_* {13} = 233 )( F *_{14} = 377 )( F_* {15} = 610 )( F *_{16} = 987 )( F_* {17} = 1597 )( F *_{18} = 2584 )( F_* {19} = 4181 )( F *_{20} = 6765 )

Therefore, the 20th value in the sequence is: 6765*

所有模型都能给出正确答案,但 Phi-4 的解题方式对数学基础不太好的人比较友好。响应时间上也没啥大差别。

我们继续试第二题,使用这个提示词:

A bank offers an annual interest rate of 5%, compounded semi-annually.
What is the final amount if 1 million dollar is deposited for 10 years?

Phi-4

To calculate the final amount when interest is compounded semi-annually, we can use the compound interest formula:

[ A = P \left(1 + \frac{r}{n}\right)^{nt} ]

where:

( A ) is the final amount
( P ) is the principal amount (initial deposit)
( r ) is the annual interest rate (in decimal form)
( n ) is the number of times the interest is compounded per year
( t ) is the number of years
For this problem:

( P = 1,000,000 ) dollars
( r = 0.05 ) (5% as a decimal)
( n = 2 ) (compounded semi-annually)
( t = 10 ) years
Plugging the values into the formula:

[ A = 1,000,000 \left(1 + \frac{0.05}{2}\right)^{2 \times 10} ]

[ A = 1,000,000 \left(1 + 0.025\right)^{20} ]

[ A = 1,000,000 \times (1.025)^{20} ]

Now, calculate ( (1.025)^{20} ):

[ (1.025)^{20} \approx 1.638616 ]

Therefore:

[ A \approx 1,000,000 \times 1.638616 ]

[ A \approx 1,638,616 ]

The final amount after 10 years is approximately $1,638,616.

GPT-4o

To calculate the final amount with compound interest, you can use the formula:

[ A = P \left(1 + \frac{r}{n}\right)^{nt} ]

where:

Given:

Plug these values into the formula:

[ A = 1,000,000 \left(1 + \frac{0.05}{2}\right)^{2 \times 10} ]
[ A = 1,000,000 \left(1 + 0.025\right)^{20} ]
[ A = 1,000,000 \times (1.025)^{20} ]

Now calculate ( (1.025)^{20} ):

[ (1.025)^{20} \approx 1.6386164403 ]

Now find the final amount ( A ):

[ A \approx 1,000,000 \times 1.6386164403 ]
[ A \approx 1,638,616.44 ]

The final amount after 10 years will be approximately $1,638,616.44.

o1

To calculate the final amount for a $1,000,000 deposit with an annual interest rate of 5% compounded semi-annually over 10 years, you can use the compound interest formula:

[
A = P \left(1 + \frac{r}{n}\right)^{nt}
]

Where:

Plugging in the values:

[
A = 1,000,000 \times \left(1 + \frac{0.05}{2}\right)^{2 \times 10} = 1,000,000 \times (1.025)^{20}
]

Calculating ( (1.025)^{20} ):

[
(1.025)^{20} \approx 1.63862
]

Now, multiply this by the principal:

[
A \approx 1,000,000 \times 1.63862 = 1,638,620
]

Final Amount: After 10 years, the investment will grow to approximately $1,638,620.

三个模型都能算对。o1 明显更快,只用了 11.38 毫秒。GPT-4o 和 Phi-4 慢一些,Phi-4 比 GPT-4o 还稍慢。

最后一个测试:几何推理能力

使用这个提示词:

If the volume of a rectangular prism is 120 cubic centimetres,
its height is 6 cm, and its width is 4 cm, what is its length?

Phi-4

To find the length of the rectangular prism, we can use the formula for the volume of a rectangular prism:

[ \text{Volume} = \text{Length} \times \text{Width} \times \text{Height} ]

Given:

Volume = 120 cubic centimeters
Height = 6 cm
Width = 4 cm
Substitute the known values into the formula:

[ 120 = \text{Length} \times 4 \times 6 ]

Simplify the equation:

[ 120 = \text{Length} \times 24 ]

To solve for the Length, divide both sides by 24:

[ \text{Length} = \frac{120}{24} ]

Calculate the division:

[ \text{Length} = 5 ]

Thus, the length of the rectangular prism is 5 cm.

GPT-4o

To find the length of the rectangular prism, you can use the formula for the volume of a rectangular prism:

[ \text{Volume} = \text{length} \times \text{width} \times \text{height} ]

You are given the volume (120 cubic centimeters), the height (6 cm), and the width (4 cm). Plug these values into the formula:

[ 120 = \text{length} \times 4 \times 6 ]

First, calculate (4 \times 6):

[ 120 = \text{length} \times 24 ]

Next, solve for the length by dividing both sides of the equation by 24:

[ \text{length} = \frac{120}{24} ]

[ \text{length} = 5 ]

Thus, the length of the rectangular prism is 5 centimeters.

o1

To find the length of the rectangular prism, you can use the formula for the volume of a rectangular prism:

[
\text{Volume} = \text{Length} \times \text{Width} \times \text{Height}
]

Given:

Step 1: Write down the formula with the known values.

[
120 = \text{Length} \times 4 \times 6
]

Step 2: Simplify the equation to solve for Length.

[
\text{Length} = \frac{120}{4 \times 6}
]

[
\text{Length} = \frac{120}{24}
]

[
\text{Length} = 5 \text{ cm}
]

Answer: The length of the rectangular prism is 5 centimetres .

三个模型都答对了,但 o1 和 Phi-4 的推理方式更像人类那种一步步思考的方式。让我惊讶的是 GPT-4o 在推理能力上没 o1 和 Phi-4 强。

总结:

我对比了 Phi-4、GPT-4o 和 o1,速度基本差不多。Phi-4 是开源的,我本身更喜欢开源。虽然 o1 更快更强,我还是更愿意用 Phi-4。我觉得 Phi-4 和其他模型的差距不大,而且它小到可以在我的笔记本上运行,这点对我很重要,因为我没有性能强大的电脑。

📌 总结完这篇之后,如果你也想把类似 Phi-4 这种模型跑在自己的机器上,甚至部署为产品、连上数据库、配接口……那恭喜你,已经走在大多数人前面了。

我也整理了不少相关图文方案与完整开发流程,适合AI从业者和初学者,欢迎来百度搜索关键词「AI软硬件一体解决者」,或者直接私信我。

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

Phi-4 小型语言模型 数学推理 模型部署 Ollama
相关文章