多标签文本分类

目录

pytorch框架 

pytorch-nlu 网络比较全

支持bert albert,

运行报错:No module named 'transformers.modeling_bert'

albertv2 预训练模型和数据集

albertv2推理

libMultiLabel

liblinear不能安装,安装会报错库找不到

安装torchtext 会把torch重新安装一遍

tf的

clue


pytorch框架 

pytorch-nlu 网络比较全

GitHub - yongzhuo/Pytorch-NLU: Pytorch-NLU,一个中文文本分类、序列标注工具包,支持中文长文本、短文本的多类、多标签分类任务,支持中文命名实体识别、词性标注、分词、抽取式文本摘要等序列标注任务。 Ptorch NLU, a Chinese text classification and sequence annotation toolkit, supports multi class and multi label classification tasks of Chinese long text and short text, and supports sequence annotation tasks such as Chinese named entity recognition, part of spee

支持bert albert,

GitHub - lonePatient/Bert-Multi-Label-Text-Classification: This repo contains a PyTorch implementation of a pretrained BERT model for multi-label text classification.

运行报错:No module named 'transformers.modeling_bert'

报错代码:

from transformers.modeling_bert import BertPreTrainedModel, BertModel

改为:

from pybert.model.albert.modeling_bert import BertPreTrainedModel, BertModel

作者说的transformers安装地址:

https://github.com/huggingface/transformers

albertv2 预训练模型和数据集

预训练模型下载地址

https://github.com/lonePatient/albert_pytorch

切换模型:需要把目录pybert/dataset中的cached_train_examples_albert 和相似文件删除

albertv2推理
import torch
from pybert.configs.basic_config import config
from pybert.io.albert_processor import AlbertProcessor
from pybert.model.albert_for_multi_label import AlbertForMultiLable

def main(text,arch,max_seq_length,do_lower_case):
    processor = AlbertProcessor(spm_model_file=config['albert_vocab_path'], do_lower_case=do_lower_case, vocab_file=None)
    label_list = processor.get_labels()
    id2label = {i: label for i, label in enumerate(label_list)}
    model = AlbertForMultiLable.from_pretrained(config['checkpoint_dir'] /f'{arch}', num_labels=len(label_list))
    tokens = processor.tokenizer.tokenize(text)
    if len(tokens) > max_seq_length - 2:
        tokens = tokens[:max_seq_length - 2]
    tokens = ['[CLS]'] + tokens + ['[SEP]']
    input_ids = processor.tokenizer.convert_tokens_to_ids(tokens)
    input_ids = torch.tensor(input_ids).unsqueeze(0)  # Batch size 1, 2 choices
    logits = model(input_ids)
    probs = logits.sigmoid()
    return probs.cpu().detach().numpy()[0]

if __name__ == "__main__":
    text = ''''"FUCK YOUR FILTHY MOTHER IN THE ASS, DRY!"'''
    max_seq_length = 256
    do_loer_case = False
    arch = 'albert'
    probs = main(text,arch,max_seq_length,do_loer_case)
    print(probs)
    
    '''
    #output
    [0.98304486 0.40958735 0.9851305  0.04566246 0.8630512  0.07316463]
    '''

数据集下载,用谷歌账号登录,然后同意协议,可以下载。

Toxic Comment Classification Challenge | Kaggle

libMultiLabel

https://github.com/ASUS-AICS/LibMultiLabel

依赖项

nltk

# wait for https://github.com/Lightning-AI/pytorch-lightning/pull/19191

lightning==2.0.9 torch torchmetrics==0.10.3

torchtext

transformers

liblinear-multicore

liblinear不能安装,安装会报错库找不到
安装torchtext 会把torch重新安装一遍

GitHub - FBI1314/textClassification: 短文本分类、多标签分类方法封装

'max_input_length':20,    #训练文本最大长度

"src_vocab_size": 20000,  # 输入词袋最多值
"label_vocab_size": 20,   #label最大词袋长度

tf的

clue

GitHub - CLUEbenchmark/CLUE: 中文语言理解测评基准 Chinese Language Understanding Evaluation Benchmark: datasets, baselines, pre-trained models, corpus and leaderboard

https://github.com/yuanxiaosc/BERT-for-Sequence-Labeling-and-Text-Classification/tree/master

https://github.com/RandolphVI/Hierarchical-Multi-Label-Text-Classification/tree/master

GitHub - brightmart/albert_zh: A LITE BERT FOR SELF-SUPERVISED LEARNING OF LANGUAGE REPRESENTATIONS, 海量中文预训练ALBERT模型

数据;

文本分类语料库(复旦)测试语料 – 自然语言处理与信息检索共享平台

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值