Coggle数据科学 | 小白学大模型:大模型输出概率 logprobs

本文来源公众号“Coggle数据科学”,仅用于学术分享,侵权删,干货满满。

原文链接:小白学大模型:大模型输出概率 logprobs

在ChatGPT API中启用 logprobs 参数时,API 会返回每个输出标记的对数概率,以及每个标记位置上最可能的几个标记及其对数概率的有限数量。

https://cookbook.openai.com/examples/using_logprobs

相关的请求参数包括:

  • logprobs:是否返回输出标记的对数概率。如果设置为 true,则返回消息内容中每个输出标记的对数概率。目前在 gpt-4-vision-preview 模型中不可用。

  • top_logprobs:一个介于 0 和 5 之间的整数,指定要在每个标记位置返回的最可能标记的数量,每个标记都有一个关联的对数概率。如果使用了此参数,logprobs 必须设置为 true。

输出标记的对数概率表示在给定上下文的情况下,每个标记出现在序列中的可能性。简单来说,对数概率是 log(p),其中 p 是基于上下文中先前标记的概率。关于 logprobs 的一些关键点:

  • 较高的对数概率表明在该上下文中标记出现的可能性较高。这使用户能够评估模型对其输出的信心程度,或者探索模型考虑的替代响应。

  • 对数概率可以是任何负数或 0.00.0 对应于 100% 的概率。

  • 对数概率允许我们计算序列的联合概率,即个别标记的对数概率之和。这对于评分和排名模型输出很有用。另一种常见的方法是取一个句子的每个标记的平均对数概率来选择最佳生成结果。

  • 我们可以检查分配给不同候选标记的 logprobs,以了解模型考虑的哪些选项是合理的或不合理的。

logprobs使用案例

分类任务

大型语言模型在许多分类任务上表现出色,但准确地衡量模型对其输出的信心可能具有挑战性。logprobs 提供了与每个类别预测相关联的概率,使用户能够设置自己的分类或置信度阈值。

CLASSIFICATION_PROMPT = """You will be given a headline of a news article.
Classify the article into one of the following categories: Technology, Politics, Sports, and Art.
Return only the name of the category, and nothing else.
MAKE SURE your output is one of the four categories stated.
Article headline: {headline}"""

headlines = [
    "Tech Giant Unveils Latest Smartphone Model with Advanced Photo-Editing Features.",
    "Local Mayor Launches Initiative to Enhance Urban Public Transport.",
    "Tennis Champion Showcases Hidden Talents in Symphony Orchestra Debut",
]

for headline in headlines:
    print(f"\nHeadline: {headline}")
    API_RESPONSE = get_completion(
        [{"role": "user", "content": CLASSIFICATION_PROMPT.format(headline=headline)}],
        model="gpt-4",
        logprobs=True,
        top_logprobs=2,
    )
    top_two_logprobs = API_RESPONSE.choices[0].logprobs.content[0].top_logprobs
    html_content = ""
    for i, logprob in enumerate(top_two_logprobs, start=1):
        html_content += (
            f"<span style='color: cyan'>Output token {i}:</span> {logprob.token}, "
            f"<span style='color: darkorange'>logprobs:</span> {logprob.logprob}, "
            f"<span style='color: magenta'>linear probability:</span> {np.round(np.exp(logprob.logprob)*100,2)}%<br>"
        )
    display(HTML(html_content))
    print("\n")

检索(问答)评估

logprobs 可以帮助在检索应用中进行自我评估。在问答示例中,模型输出一个虚构的 has_sufficient_context_for_answer 布尔值,它可以作为答案是否包含在检索内容中的置信度分数。这种类型的评估可以减少基于检索的幻觉,并增强准确性。

# Article retrieved
ada_lovelace_article = """Augusta Ada King, Countess of Lovelace (née Byron; 10 December 1815 – 27 November 1852) was an English mathematician and writer, chiefly known for her work on Charles Babbage's proposed mechanical general-purpose computer, the Analytical Engine. She was the first to recognise that the machine had applications beyond pure calculation.
Ada Byron was the only legitimate child of poet Lord Byron and reformer Lady Byron. All Lovelace's half-siblings, Lord Byron's other children, were born out of wedlock to other women. Byron separated from his wife a month after Ada was born and left England forever. He died in Greece when Ada was eight. Her mother was anxious about her upbringing and promoted Ada's interest in mathematics and logic in an effort to prevent her from developing her father's perceived insanity. Despite this, Ada remained interested in him, naming her two sons Byron and Gordon. Upon her death, she was buried next to him at her request. Although often ill in her childhood, Ada pursued her studies assiduously. She married William King in 1835. King was made Earl of Lovelace in 1838, Ada thereby becoming Countess of Lovelace.
Her educational and social exploits brought her into contact with scientists such as Andrew Crosse, Charles Babbage, Sir David Brewster, Charles Wheatstone, Michael Faraday, and the author Charles Dickens, contacts which she used to further her education. Ada described her approach as "poetical science" and herself as an "Analyst (& Metaphysician)".
When she was eighteen, her mathematical talents led her to a long working relationship and friendship with fellow British mathematician Charles Babbage, who is known as "the father of computers". She was in particular interested in Babbage's work on the Analytical Engine. Lovelace first met him in June 1833, through their mutual friend, and her private tutor, Mary Somerville.
Between 1842 and 1843, Ada translated an article by the military engineer Luigi Menabrea (later Prime Minister of Italy) about the Analytical Engine, supplementing it with an elaborate set of seven notes, simply called "Notes".
Lovelace's notes are important in the early history of computers, especially since the seventh one contained what many consider to be the first computer program—that is, an algorithm designed to be carried out by a machine. Other historians reject this perspective and point out that Babbage's personal notes from the years 1836/1837 contain the first programs for the engine. She also developed a vision of the capability of computers to go beyond mere calculating or number-crunching, while many others, including Babbage himself, focused only on those capabilities. Her mindset of "poetical science" led her to ask questions about the Analytical Engine (as shown in her notes) examining how individuals and society relate to technology as a collaborative tool.
"""

# Questions that can be easily answered given the article
easy_questions = [
    "What nationality was Ada Lovelace?",
    "What was an important finding from Lovelace's seventh note?",
]

# Questions that are not fully covered in the article
medium_questions = [
    "Did Lovelace collaborate with Charles Dickens",
    "What concepts did Lovelace build with Charles Babbage",
]

PROMPT = """You retrieved this article: {article}. The question is: {question}.
Before even answering the question, consider whether you have sufficient information in the article to answer the question fully.
Your output should JUST be the boolean true or false, of if you have sufficient information in the article to answer the question.
Respond with just one word, the boolean true or false. You must output the word 'True', or the word 'False', nothing else.
"""

自动补全

logprobs 可以帮助我们决定在用户输入时如何建议单词。

sentence_list = [
    "My",
    "My least",
    "My least favorite",
    "My least favorite TV",
    "My least favorite TV show",
    "My least favorite TV show is",
    "My least favorite TV show is Breaking Bad",
]

计算困惑度

logprobs 可以用于帮助我们评估模型对结果的整体信心,并帮助我们比较来自不同提示的结果的置信度。

prompts = [
    "In a short sentence, has artifical intelligence grown in the last decade?",
    "In a short sentence, what are your thoughts on the future of artificial intelligence?",
]

for prompt in prompts:
    API_RESPONSE = get_completion(
        [{"role": "user", "content": prompt}],
        model="gpt-3.5-turbo",
        logprobs=True,
    )

    logprobs = [token.logprob for token in API_RESPONSE.choices[0].logprobs.content]
    response_text = API_RESPONSE.choices[0].message.content
    response_text_tokens = [token.token for token in API_RESPONSE.choices[0].logprobs.content]
    max_starter_length = max(len(s) for s in ["Prompt:", "Response:", "Tokens:", "Logprobs:", "Perplexity:"])
    max_token_length = max(len(s) for s in response_text_tokens)
    

    formatted_response_tokens = [s.rjust(max_token_length) for s in response_text_tokens]
    formatted_lps = [f"{lp:.2f}".rjust(max_token_length) for lp in logprobs]

    perplexity_score = np.exp(-np.mean(logprobs))
    print("Prompt:".ljust(max_starter_length), prompt)
    print("Response:".ljust(max_starter_length), response_text, "\n")
    print("Tokens:".ljust(max_starter_length), " ".join(formatted_response_tokens))
    print("Logprobs:".ljust(max_starter_length), " ".join(formatted_lps))
    print("Perplexity:".ljust(max_starter_length), perplexity_score, "\n")

THE END!

文章结束,感谢阅读。您的点赞,收藏,评论是我继续更新的动力。大家有推荐的公众号可以评论区留言,共同学习,一起进步。

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Stm32CubeMX 为 stm32mp135d 添加 spi

Stm32CubeMX 为 stm32mp135d 添加 spi 一、启用设备1. spi 设备添加2. spi 引脚配置2. spi 时钟配置 二、 生成代码1. optee 配置 spi 时钟和安全验证2. linux spi 设备 dts 配置 bringup 可参考&#xff1a;Stm32CubeMX 生成设备树 一、启用设备 1. spi 设备添加 选中spi设…

Django后台项目开发实战四

用户可以浏览工作列表以及工作详情 第四阶段 在 jobs 文件夹下创建 templates 文件夹&#xff0c;在里面创建 base.html 网页&#xff0c;内容如下 <!-- base.html --> <div style"text-align:center;"><h1 style "margin:auto; width:50%;&…

【Canvas与艺术】绘制地平线

【关键点】 灭点在透视中的作用。 【成图】 【代码】 <!DOCTYPE html> <html lang"utf-8"> <meta http-equiv"Content-Type" content"text/html; charsetutf-8"/> <head><title>使用HTML5/Canvas绘制地平线<…

神经网络的优化器

神经网络的优化器是用于训练神经网络的一类算法&#xff0c;它们的核心目的是通过改变神经网络的权值参数来最小化或最大化一个损失函数。优化器对损失函数的搜索过程对于神经网络性能至关重要。 作用&#xff1a; 参数更新&#xff1a;优化器通过计算损失函数相对于权重参数的…

洞察Agent AI智能体的未来:机遇与挑战并存

&#x1f512;文章目录&#xff1a; &#x1f6f4;什么是Agent AI智能体 &#x1f4a5;Agent AI智能体的技术组成 ☂️Agent AI智能体的应用场景 &#x1f4a3;Agent AI智能体的挑战与问题 &#x1f6b2; Agent AI智能体在未来社会中的角色和影响 ❤️对Agent AI智能体未来的期…

【人工智能AI书籍】TensorFlow机器学习实战指南(推荐)

今天又来给大家推荐一本人工智能方面的书籍<TensorFlow机器学习实战指南>。TensorFlow是一个开源机器学习库。本书从TensorFlow的基础开始介绍&#xff0c;涉及变量、矩阵和各种数据源。之后&#xff0c;针对使用TensorFlow线性回归技术的实践经验进行详细讲解。后续章节…

[1688]jsp工资投放管理系统Myeclipse开发mysql数据库web结构java编程计算机网页项目

一、源码特点 JSP 工资投放管理系统是一套完善的java web信息管理系统&#xff0c;对理解JSP java编程开发语言有帮助&#xff0c;系统具有完整的源代码和数据库&#xff0c;系统主要采用B/S模式开发。开发环境为 TOMCAT7.0,Myeclipse8.5开发&#xff0c;数据库为Mysql5.0…

[1671]jsp教材管理系统Myeclipse开发mysql数据库web结构java编程计算机网页项目

一、源码特点 JSP 教材管理系统是一套完善的java web信息管理系统&#xff0c;对理解JSP java编程开发语言有帮助&#xff0c;系统具有完整的源代码和数据库&#xff0c;系统主要采用B/S模式开发。开发环境为TOMCAT7.0,Myeclipse8.5开发&#xff0c;数据库为Mysql5.0&…

k8s 资源组版本支持列表

1 kubernetes的资源注册表 kube-apiserver组件启动后的第一件事情是将Kubernetes所支持的资源注册到Scheme资源注册表中,这样后面启动的逻辑才能够从Scheme资源注册表中拿到资源信息并启动和运行API服务。 kube-apiserver资源注册分为两步:第1步,初始化Scheme资源注册表;…

社交媒体数据恢复:推特、Twitter

推特&#xff08;Twitter&#xff09;数据恢复&#xff1a;如何找回丢失的内容 随着社交媒体的普及&#xff0c;越来越多的人开始使用推特&#xff08;Twitter&#xff09;来分享生活点滴、发表观点和获取信息。然而&#xff0c;有时候我们会不小心删除了重要的推文&#xff0…

【Jenkins】持续集成与交付 (四):修改Jenkins插件下载地址、汉化

🟣【Jenkins】持续集成与交付 (四):修改Jenkins插件下载地址、汉化 一、修改Jenkins插件下载地址二、汉化Jenkins三、关于Jenkins💖The Begin💖点点关注,收藏不迷路💖 一、修改Jenkins插件下载地址 由于Jenkins官方插件地址下载速度较慢,我们可以通过修改下载地址…

上位机图像处理和嵌入式模块部署(树莓派4b利用驱动实现进程数据共享)

【 声明&#xff1a;版权所有&#xff0c;欢迎转载&#xff0c;请勿用于商业用途。 联系信箱&#xff1a;feixiaoxing 163.com】 前面我们讨论过&#xff0c;目前在linux系统上面有很多办法可以实现多进程数据共享。这里面比如说管道&#xff0c;比如说共享内存&#xff0c;比如…

uniapp 自定义 App启动图

由于uniapp默认的启动界面太过普通 所以需要自定义个启动图 普通的图片不可以过不了苹果的审核 所以使用storyboard启动图 生成 storyboard 的网站&#xff1a;初雪云-提供一站式App上传发布解决方案

Docker-compose部署LTC同步节点

1、下载ltc程序包&#xff0c;litecoin下载地址 下载页 mkdir /data/docker-compose/ltc cd /data/docker-compose/ltc https://github.com/litecoin-project/litecoin/releases/download/v0.21.3/litecoin-0.21.3-x86_64-linux-gnu.tar.gz2、编写dockerfile和bitcoin.conf b…

笔记-word导出PDF老是更新域导致图片和表格题注发生变化

问题描述&#xff1a;微软word 导出PDF时&#xff0c;老是更新域&#xff0c;导致图片和表格题注否跟着变化 以下是解决方法的具体描述。 目录 一、准备工作二、操作步骤 一、准备工作 1、工具版本&#xff1a;微软 word 2016&#xff08;其他微软word版本也OK&#xff09; …

第二证券投资参考:汽车以旧换新细则发布 云厂商AI投资持续加码

上星期五&#xff0c;A股放量大涨。两市股指盘中单边上行&#xff0c;午后再度攀升&#xff0c;沪指涨超1%&#xff0c;创业板指大涨超3%&#xff1b;到收盘&#xff0c;沪指涨1.17%报3088.64点&#xff0c;深证成指涨2.15%报9463.91点&#xff1b;创业板指涨3.34%报1823.74点&…

2024五一数学建模B题思路代码与论文分析

2024五一数学建模B题完整代码和成品论文获取↓↓↓↓↓ https://www.yuque.com/u42168770/qv6z0d/gyoz9ou5upvkv6nx?singleDoc# B题 未来新城交通需求规划与可达率问题需要建立的模型和算法: 1. 图论 2. 网络流模型 3. 线性规划/整数规划 4. 组合优化 5. 随机过程 6. …

es环境安装及php对接使用

Elasticsearch Elasticsearch 是一个分布式、高扩展、高实时的搜索与数据分析引擎。它提供了一个分布式多用户能力的全文搜索引擎&#xff0c;基于RESTful web接口。Elasticsearch是用Java语言开发的&#xff0c;并作为Apache许可条款下的开放源码发布&#xff0c;是一种流行的…

76、堆-数据流的中位数

思路&#xff1a; 这个问题是动态数据流中位数查找问题。在数据流中&#xff0c;数据是逐个到来的&#xff0c;而我们需要在任何时候快速返回已有数据的中位数。中位数是将数据集分成两个等长的子集&#xff0c;一个包含所有较小的元素而另一个包含所有较大的元素。 为了高效解…

利用word2vec包将中文转变为词向量

代码展示&#xff1a; import jieba import re import json import logging import sys import gensim.models as word2vec from gensim.models.word2vec import LineSentence, loggerpattern u[\\s\\d,.<>/?:;\\"[\\]{}()\\|~!\t"#$%^&*\\-_a-zA-Z&…
最新文章