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

相关推荐
叠层归一研究院2 分钟前
基于极限自指的叠层归一宇宙结构理论——无元外部封闭系统的内生区分模型
人工智能·经验分享·算法·agi
IT_陈寒4 分钟前
Java线程池用错参数,我的服务居然悄悄崩溃了
前端·人工智能·后端
why-geo12 分钟前
Hermes Agent 与 Python 的会产生什么样的碰撞
人工智能·python·ai编程
正经教主20 分钟前
【FDE系列】阶段2:Day 29:FastAPI 进阶 — Pydantic 模型与完整 CRUD 实战
人工智能·python·fde
时速GEO系统36 分钟前
初元 AI V1.1 版本升级,首个正式版发布一周・实现生成、部署、上线、优化全流程自动化闭环
人工智能·数据挖掘·node.js
咕泡科技39 分钟前
咕泡科技FDE系列最新产品重磅发布!
人工智能·大模型·ai落地·fde·前沿部署工程师
犀利豆1 小时前
为什么全世界的 AI 都画不好一只骑自行车的鹈鹕
人工智能·llm·aigc
金色印象1 小时前
【论文解读】DRSN:把软阈值去噪“内化”进残差网络的强噪声故障诊断方法
深度学习·残差网络·故障诊断·软阈值·收缩函数·强噪声·drsn
天天被压力1 小时前
【跨市场数据实战 #08】可转债折价机会怎么筛:3个接口抓比价、列表和实时盘口
java·人工智能·python
baopixiaoz1 小时前
BeeQuant × BeeAgent:用AI加速策略验证
大数据·人工智能·python·区块链