Bert-Chinese-Text-Classification-Pytorch icon indicating copy to clipboard operation
Bert-Chinese-Text-Classification-Pytorch copied to clipboard

单条文本数据的预测代码

Open shehuan opened this issue 4 years ago • 15 comments

import torch
from importlib import import_module

key = {
    0: 'finance',
    1: 'realty',
    2: 'stocks',
    3: 'education',
    4: 'science',
    5: 'society',
    6: 'politics',
    7: 'sports',
    8: 'game',
    9: 'entertainment'
}

model_name = 'bert'
x = import_module('models.' + model_name)
config = x.Config('THUCNews')
model = x.Model(config).to(config.device)
model.load_state_dict(torch.load(config.save_path, map_location='cpu'))


def build_predict_text(text):
    token = config.tokenizer.tokenize(text)
    token = ['[CLS]'] + token
    seq_len = len(token)
    mask = []
    token_ids = config.tokenizer.convert_tokens_to_ids(token)
    pad_size = config.pad_size
    if pad_size:
        if len(token) < pad_size:
            mask = [1] * len(token_ids) + ([0] * (pad_size - len(token)))
            token_ids += ([0] * (pad_size - len(token)))
        else:
            mask = [1] * pad_size
            token_ids = token_ids[:pad_size]
            seq_len = pad_size
    ids = torch.LongTensor([token_ids])
    seq_len = torch.LongTensor([seq_len])
    mask = torch.LongTensor([mask])
    return ids, seq_len, mask


def predict(text):
    """
    单个文本预测
    :param text:
    :return:
    """
    data = build_predict_text(text)
    with torch.no_grad():
        outputs = model(data)
        num = torch.argmax(outputs)
    return key[int(num)]


if __name__ == '__main__':
    print(predict("备考2012高考作文必读美文50篇(一)"))

shehuan avatar Jan 29 '21 08:01 shehuan

您好,这个代码运行有问题啊,报这个错。 RuntimeError: Expected object of backend CUDA but got backend CPU for argument #3 'index'

bboyxu5928 avatar Feb 03 '21 07:02 bboyxu5928

请问一下,如果我想连续加载两个模型,加载完第一个之后应该怎么重置torch,才能让第二个模型加载的时候不受第一个模型的影响

shengtaovvv avatar Mar 19 '21 07:03 shengtaovvv

您好,这个代码运行有问题啊,报这个错。 RuntimeError: Expected object of backend CUDA but got backend CPU for argument #3 'index'

你好,我也遇到了,你解决了没?可以分享下吗? 谢谢

ZhangChaoZhong avatar May 03 '21 05:05 ZhangChaoZhong

您好,这个代码运行有问题啊,报这个错。 RuntimeError: Expected object of backend CUDA but got backend CPU for argument #3 'index'

你好,我也遇到了,你解决了没?可以分享下吗? 谢谢

你这个问题是pytorch类型不匹配,把build_predict_text() 方法中 ids = torch.LongTensor([token_ids]) 改成 ids = torch.LongTensor([token_ids]).cuda()就行了,下面seq_len和mask同样。

Rvlis avatar May 10 '21 08:05 Rvlis

两种修改方式。分别对应使用cpu,gpu预测。 1.image 改成这个就是用cpu进行预测。 2. image 改成这个就是用gpu进行预测

linnnff avatar May 24 '21 09:05 linnnff

请问我用这个代码进行预测,为什么和训练的时候的测试准确率相差很大

Howard0x3f avatar Jun 30 '21 01:06 Howard0x3f

RuntimeError: Error(s) in loading state_dict for Model: Unexpected key(s) in state_dict: "conv_region.weight", "conv_region.bias", "conv.weight", "conv.bias". size mismatch for fc.weight: copying a param with shape torch.Size([10, 250]) from checkpoint, the shape in current model is torch.Size([10, 768]).

你好报这个错是什么原因呢?

Nju-Ben avatar Dec 14 '21 06:12 Nju-Ben

为什么每次预测的结果都不一样?并且预测的准确率和训练得到的准确率相差太大。

zhanghaoxuan1999 avatar Apr 02 '22 08:04 zhanghaoxuan1999

请问你这个问题解决了吗 我每次预测的结果也是无法保持一致

youngwensi avatar May 18 '22 14:05 youngwensi

RuntimeError: Error(s) in loading state_dict for Model: Unexpected key(s) in state_dict: "conv_region.weight", "conv_region.bias", "conv.weight", "conv.bias". size mismatch for fc.weight: copying a param with shape torch.Size([10, 250]) from checkpoint, the shape in current model is torch.Size([10, 768]).

你好报这个错是什么原因呢?

我的也是?解决了吗?

liuxiaobei6667 avatar May 19 '22 13:05 liuxiaobei6667

import torch
from importlib import import_module

key = {
    0: 'finance',
    1: 'realty',
    2: 'stocks',
    3: 'education',
    4: 'science',
    5: 'society',
    6: 'politics',
    7: 'sports',
    8: 'game',
    9: 'entertainment'
}

model_name = 'bert'
x = import_module('models.' + model_name)
config = x.Config('THUCNews')
model = x.Model(config).to(config.device)
model.load_state_dict(torch.load(config.save_path, map_location='cpu'))


def build_predict_text(text):
    token = config.tokenizer.tokenize(text)
    token = ['[CLS]'] + token
    seq_len = len(token)
    mask = []
    token_ids = config.tokenizer.convert_tokens_to_ids(token)
    pad_size = config.pad_size
    if pad_size:
        if len(token) < pad_size:
            mask = [1] * len(token_ids) + ([0] * (pad_size - len(token)))
            token_ids += ([0] * (pad_size - len(token)))
        else:
            mask = [1] * pad_size
            token_ids = token_ids[:pad_size]
            seq_len = pad_size
    ids = torch.LongTensor([token_ids])
    seq_len = torch.LongTensor([seq_len])
    mask = torch.LongTensor([mask])
    return ids, seq_len, mask


def predict(text):
    """
    单个文本预测
    :param text:
    :return:
    """
    data = build_predict_text(text)
    with torch.no_grad():
        outputs = model(data)
        num = torch.argmax(outputs)
    return key[int(num)]


if __name__ == '__main__':
    print(predict("备考2012高考作文必读美文50篇(一)"))

这预测结果相差太大了吧

liuxiaobei6667 avatar May 19 '22 14:05 liuxiaobei6667

预测结果太离谱

liuxiaobei6667 avatar May 19 '22 14:05 liuxiaobei6667

同一条文本信息,为什么每次结果不一样呢

we0091234 avatar Jun 14 '22 07:06 we0091234