news 2026/6/9 19:54:02

python bert_score使用本地模型的方法

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
python bert_score使用本地模型的方法

1. 背景

bert_score是一个用于评估文本生成质量的Python库,基于预训练的BERT模型计算参考文本与生成文本之间的语义相似度。它通过比较两个文本的BERT嵌入向量来评估它们的匹配程度,比传统的n-gram匹配方法(如BLEU、ROUGE)更能捕捉语义层面的相似性。一般bert_score 是仅仅支持指定模型名称然后再代码实现里面从Hugging Face上面拉取模型。但是如果我们在有限制的网络环境中在线拉取模型就会失败,比较合适的解决办法就是先将模型下载到本地,然后给bert_score指定模型路径。

2. 支持的模型和语言

bert_score中通过model_type和lang两个参数来执行模型和对应的文本语言。

具体文档可以参考:https://github.com/Tiiiger/bert_score

from bert_score import score candidates = ["The cat sits on the mat."] # 生成文本 references = ["A cat is sitting on the rug."] # 参考文本 # 计算BERTScore P, R, F1 = score(candidates, references, model_type='roberta-large',lang="en") print(f"Precision: {P.mean():.3f}, Recall: {R.mean():.3f}, F1: {F1.mean():.3f}")

3.支持本地模型的方法

3.1 修改源码

1. 在score方法里面可以看到get_model和get_tokenizer两个方法

2.点进去可以看到对应函数的源码

def get_model(model_type, num_layers, all_layers=None): if model_type.startswith("scibert"): model = AutoModel.from_pretrained(cache_scibert(model_type)) elif "t5" in model_type: from transformers import T5EncoderModel model = T5EncoderModel.from_pretrained(model_type) else: model = AutoModel.from_pretrained(model_type) model.eval() if hasattr(model, "decoder") and hasattr(model, "encoder"): model = model.encoder # drop unused layers # ...... return model def get_tokenizer(model_type, use_fast=False): if model_type.startswith("scibert"): model_type = cache_scibert(model_type) if version.parse(trans_version) >= version.parse("4.0.0"): tokenizer = AutoTokenizer.from_pretrained(model_type, use_fast=use_fast) else: assert not use_fast, "Fast tokenizer is not available for version < 4.0.0" tokenizer = AutoTokenizer.from_pretrained(model_type) return tokenizer

3. 修改源码直接将我们的model_path硬编码进去,当然为了更好的适配性,可以将model_path做成一个可选参数,然后传递出去给score函数

def get_model(model_type, num_layers, all_layers=None): model_path = 'xxx' if model_type.startswith("scibert"): model = AutoModel.from_pretrained(cache_scibert(model_type)) elif "t5" in model_type: from transformers import T5EncoderModel model = T5EncoderModel.from_pretrained(model_path) else: model = AutoModel.from_pretrained(model_path) model.eval() def get_tokenizer(model_type, use_fast=False): if model_type.startswith("scibert"): model_type = cache_scibert(model_type) model_path = 'xxx' if version.parse(trans_version) >= version.parse("4.0.0"): tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=use_fast) else: assert not use_fast, "Fast tokenizer is not available for version < 4.0.0" tokenizer = AutoTokenizer.from_pretrained(model_path) return tokenizer

3.2 添加对应的num_layers参数

这里还有一种方法就是通过给model_type传递给本地模型所在的路径。同时传递num_layers参数来指定。

原因是因为其实Transformers里面的xxx.from_pretrained方法其实也是可以支持你配置本地路径来加载的,这里加载模型也不会有问题。但是在bert_score中他是需要提前知道层数来优化计算过程的。这里才是报错的原因。

这一部分代码是在utils文件中。

def calculate_bert_score(original_text: str, polished_text: str, lang: str = "zh",model_type:str=None) -> float: try: # 使用bert_score计算F1分数 score_params = { "verbose": False, "device": "cuda" if torch.cuda.is_available() else "cpu" } if model_type is None: score_params["model_type"] = BERT_MODEL_PATH #指定模型路径 score_params["num_layers"] = BERT_BASE_CHINESE_NUM_LAYERS#执行模型层数(12) else: score_params["model_type"] = model_type P, R, F1 = score([polished_text], [original_text], **score_params) # 返回F1分数(转换为Python float) f1_score = F1.item() if hasattr(F1, 'item') else float(F1) return f1_score except Exception as e: print(f"计算bert_score时出错: {str(e)}") # 如果计算失败,返回一个默认值或者抛出异常 raise
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 19:13:52

Git Push大文件错误终极解决指南-解决 git push 8192 MiB 错误的方法

解决 git push 8192 MiB 错误的方法 错误通常是由于 Git 默认限制推送文件大小导致的&#xff0c;可以通过以下方法解决&#xff1a; 调整 Git 的 postBuffer 大小 运行以下命令将 postBuffer 设置为更大的值&#xff0c;例如 2GB&#xff1a; git config --global http.pos…

作者头像 李华
网站建设 2026/6/10 17:18:53

提升交互体验:在LobeChat中集成自定义角色和提示词模板

提升交互体验&#xff1a;在LobeChat中集成自定义角色和提示词模板架构演进中的对话设计挑战 当大语言模型的能力已经不再是瓶颈&#xff0c;我们真正该思考的问题是&#xff1a;如何让强大的AI真正服务于具体的人、具体的场景&#xff1f; 今天&#xff0c;调用一次OpenAI或通…

作者头像 李华
网站建设 2026/6/10 13:20:46

LobeChat支持Markdown渲染吗?AI回复排版效果测试

LobeChat 支持 Markdown 渲染吗&#xff1f;AI 回复排版效果实测 在今天&#xff0c;一个 AI 聊天工具是否“好用”&#xff0c;早已不只看它能不能回答问题&#xff0c;更要看它怎么回答。 想象一下&#xff1a;你让 AI 帮你写一段 Python 代码、列一个项目计划表&#xff0…

作者头像 李华
网站建设 2026/6/10 16:22:36

【官方方法】Hugging Face Hub下载单个文件

在机器学习和自然语言处理领域,Hugging Face Hub 已经成为模型、数据集和空间的事实标准存储库。无论是研究人员还是开发者,都需要经常从 Hub 下载文件。本文将详细介绍使用 Hugging Face 官方命令行工具 hf 下载单个文件的多种方法。 一、安装与配置 1.1 安装 huggingface…

作者头像 李华
网站建设 2026/6/10 2:51:40

说真的,你可能误会Pandas了

有人担心pandas处理数据的效率是不是不咋地。pandas是基于numpy数组来计算的&#xff0c;其实本身有优势&#xff0c;处理小批量数据集&#xff08;百万行以下&#xff0c;1GB以内&#xff09;效率是完全可以接受的&#xff0c;相比其他的数据处理库其实差异不大&#xff0c;因…

作者头像 李华
网站建设 2026/6/10 7:08:26

第 1 讲:什么是 Vibe Coding?

在开始之前&#xff0c;先纠正 3 个关键认知误区在过去一年里&#xff0c;“AI 编码”、“AI 生成应用”、“Vibe Coding”逐渐从演示视频走向真实使用场景&#xff0c;但在实践中&#xff0c;我发现一个普遍问题&#xff1a;很多人并不是“用不好 AI Coding”&#xff0c;而是…

作者头像 李华