bert-base-chinese另外的加载方法.txt

import os

os.environ'HF_ENDPOINT' = 'https://hf-mirror.com'

import torch

from transformers import pipeline

pipe = pipeline("fill-mask", model="google-bert/bert-base-chinese")

from transformers import AutoTokenizer, AutoModelForMaskedLM

tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-chinese")

model = AutoModelForMaskedLM.from_pretrained("google-bert/bert-base-chinese")

input_text='bert-base-chinese该怎么用?'

inx_text=tokenizer.encode(input_text)

inv_text=tokenizer.convert_ids_to_tokens(inx_text)

with torch.no_grad():

output=model(torch.tensor(inx_text))

print(type(output))

last_hidden=output'logits'

print(last_hidden.shape,last_hidden0)# (1,9,21128)(batch_size,seq_len,d_model)

from transformers import AutoModelForQuestionAnswering

aq=AutoModelForQuestionAnswering.from_pretrained("google-bert/bert-base-chinese")

定义问题和上下文

question = "你好,请问今天天气怎么样?"

context = "今天是晴天,气温适中,非常适合户外活动。"

使用分词器对问题和上下文进行编码

inputs = tokenizer(question, context, return_tensors='pt', \

padding=True, truncation=True)

input_ids = inputs'input_ids'

attention_mask = inputs'attention_mask'

在Transformers库中,模型并不是通过数字本身来识别分割符的,

而是通过分词器(Tokenizer)对输入文本的处理来识别这些特殊标记。

在不计算梯度的情况下进行推理

with torch.no_grad():

aq_outputs =aq(input_ids, attention_mask=attention_mask)

start_inxes=aq_outputs'start_logits'0

end_inxes=aq_outputs'end_logits'0

print(len(start_inxes),len(end_inxes))

start=torch.argmax(start_inxes)

end=torch.argmax(end_inxes)

(question+context)start:end

相关推荐
雪隐10 小时前
个人电脑玩AI-15让5060 Ti给你打工——MiniMax H3 本地部署实录:一个自带录音棚的视频模型,和它的 NVFP4 瘦身奇遇
前端·人工智能·后端
数字供应链安全产品选型10 小时前
中国版 Anthropic Mythos,为何是悬镜安全?
人工智能·安全
甲维斯10 小时前
Qoder+Qwen3.8Max白嫖测试!这次真牛逼了?和K3比如何?
人工智能
一个数据大开发10 小时前
Skill、Tool、SOP、MCP 文章合集
大数据·人工智能·知识图谱
紫禁玄科10 小时前
公共WiFi流量安全全解析
网络·人工智能·web安全·网络安全·系统安全
梦想三三10 小时前
LangChain RAG PDF 智能问答实战:用 Streamlit 构建本地知识库(完整代码)
人工智能·python·langchain·大模型·rag
不加辣椒10 小时前
第11章:性能优化与成本控制
人工智能
xcLeigh10 小时前
KingbaseES 的卢智能运维体架构深度拆解
运维·数据库·人工智能·ai·架构·ffmpeg·智能体
GuWenyue10 小时前
不用第三方SDK!Vue3原生Fetch实现DeepSeek流式输出,90%前端都会踩的分片解析坑一次性解决
前端·人工智能·llm