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

相关推荐
这张生成的图像能检测吗1 分钟前
(论文速读)DI-CDM:微调条件扩散模型在结构健康监测中的损伤成像
人工智能·lora·扩散模型·高分辨率成像·结构健康监测
阿伟玩不懂3 分钟前
接入第一个 MCP Server:让 AI 自己看你的数据库
ide·人工智能
中电金信4 分钟前
中电金信参编的团体标准《商业银行应用程序接口治理能力要求》正式发布
大数据·运维·人工智能
Data-Miner9 分钟前
怎么用AI给Excel去重?先核对这7条再下手,别让好数据被删错:数以轻舟方案解析
人工智能·excel
小宋102110 分钟前
MCP 工具也有供应链风险:注册中心、签名校验、版本锁定与白名单
开发语言·网络·人工智能·php
悟天特斯11 分钟前
AI驱动的楼宇节能:从经验粗放到数据精准的转型路径
人工智能·物联网
IT古董17 分钟前
AI 资讯日报|2026年9月19日:模型迭代进入高频节奏,国产全模态与算力持续突破,AI应用与具身智能加速落地
人工智能
2601_9623807623 分钟前
节气民俗科普视频的 AI 动效分镜工作流:从文案到成片怎么做
人工智能·音视频
启雀AI30 分钟前
培训管理系统的 AI 智能陪练完整功能逻辑,以家电门店销售为例的剧本框架
人工智能·ai·软件需求·培训系统·培训平台
hunteritself34 分钟前
夯爆了!Qoder 狂肝 2 小时,293 个测试全绿,Credits 一分没扣
前端·人工智能·chrome·深度学习·机器学习