1 Star 0 Fork 0

QFork/Bert-Chinese-Text-Classification

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
bert_tuili.py 1.09 KB
一键复制 编辑 原始数据 按行查看 历史
QijingGJ 提交于 2024-02-08 12:21 +08:00 . first commit
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 2 15:05:26 2024
@author: QiJing
"""
import os
from transformers import BertTokenizer
import torch
from bert_get_data import BertClassifier
bert_name = './bert-base-chinese'
tokenizer = BertTokenizer.from_pretrained(bert_name)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
save_path = './bert_checkpoint'
model = BertClassifier()
model.load_state_dict(torch.load(os.path.join(save_path, 'best.pt')))
model = model.to(device)
model.eval()
real_labels = []
with open('./THUCNews/data/class.txt', 'r') as f:
for row in f.readlines():
real_labels.append(row.strip())
while True:
text = input('请输入新闻标题:')
bert_input = tokenizer(text, padding='max_length',
max_length = 35,
truncation=True,
return_tensors="pt")
input_ids = bert_input['input_ids'].to(device)
masks = bert_input['attention_mask'].unsqueeze(1).to(device)
output = model(input_ids, masks)
pred = output.argmax(dim=1)
print(real_labels[pred])
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/qfork/Bert-Chinese-Text-Classification.git
git@gitee.com:qfork/Bert-Chinese-Text-Classification.git
qfork
Bert-Chinese-Text-Classification
Bert-Chinese-Text-Classification
main

搜索帮助