NeuralNLP-NeuralClassifier的使用记录(二),训练预测自己的【中文文本多分类】

NeuralNLP-NeuralClassifier的使用记录,训练预测自己的【中文文本多分类】

数据准备:

​ 与英文的训练预测一致,都使用相同的数据格式,将数据通过代码处理为JSON格式,以下是我使用的一种,不同的原数据情况会有所改动:

复制代码
import jieba.analyse as ana
import re
import jieba

def make_data_json(df,outpath):
    def stop_words(path):
        txt = open(outpath,"r",encoding='utf-8') 
        lines = txt.readlines()
        txt.close()
        stop_txt = []
        for line in lines:
            stop_txt.append(line.strip('\n'))
        return stop_txt
    
    
    with open(outpath, "w+", encoding='utf-8') as f:
        
        # with open(output_path, "w") as fw:
        for indexs in df.index:
            dict1 = {}
            dict1['doc_label'] = [str(df.loc[indexs].values[0])]
            doc_token = df.loc[indexs].values[1]
            # 只保留中文、大小写字母和阿拉伯数字
            reg = "[^0-9A-Za-z\u4e00-\u9fa5]"
            doc_token = re.sub(reg, '', doc_token)
            print(doc_token)
            # 中文分词
            seg_list = jieba.cut(doc_token, cut_all=False)
            #$提取关键词,20个:
            ana.set_stop_words('./人工智能挑战赛-文本分类/停用词列表.txt')
            keyword = ana.extract_tags(doc_token, topK=20,withWeight=False,)   #True表示显示权重
            # 去除停用词
            content = [x for x in seg_list if x not in stop_words('../data/stop_words.txt')]
            dict1['doc_token'] = content
            dict1['doc_keyword'] = keyword
            dict1['doc_topic'] = []
            # 组合成字典
            print(dict1)
            # 将字典转化成字符串
            json_str = json.dumps(dict1, ensure_ascii=False)
            f.write('%s\n' % json_str)

使用构造JSON数据方法:

训练前期准备:

1、创建中文数据文件夹,Chinese_datas,

2、创建该数据的文本数据对应的标签集Chinese_label.taxonomy

3、创建该数据的训练配置文件Chinese_train_conf.json,

继续目录如下:

配置文件的注意点:

其中需要额外修改的地方:

work_nums=0

以及涉及代码中,有读取文件的部分都需要给编码中文编码:

with open(encoding='utf-8')

训练:

训练代码:

复制代码
python train.py conf/Chinese_train_conf.json

训练后生成的权重文件,在配置文件中就写出了:

预测:

复制代码
python predict.py conf/Chinese_train_conf.json Chinese_datas/predict_data.json

预测结果:

可以看出预测效果仅一个错误,该模型方便NLP的比赛分类等,准确率也很高。

代码获取:

下载就是中文分类版,在命令界面进行命令行输入,训练和预测,:

链接:https://pan.baidu.com/s/1fw_ipmOFWMiTLAFrs9i5ig

提取码:2023

相关推荐
齐翊35 分钟前
怎么确认 AI 看懂了你的提示词?
人工智能·github·ai编程
饼干哥哥1 小时前
Reddit VOC调研太慢?搭一个AI专家团队半小时洞察任何品类|以猫用饮水机为例
人工智能·算法·ai编程
以和为贵1 小时前
前端也能搞懂 RAG:用 JS 手写一条最小检索增强链路
前端·人工智能·面试
武子康2 小时前
调查研究-192 AI Agent 之间也需要“信任“:把多 Agent 信任变成可测指标
人工智能·openai·agent
Smoothcloud_润云2 小时前
Hermes Agent 的上下文记忆机制:一个开源 Agent 是怎么"记住"你的
人工智能·agent·gpu
早点睡啊2 小时前
精读 LangChain 官方文档(一)总览、安装与快速开始:从 create_agent 跑通第一个智能体
人工智能
牛奶3 小时前
AI时代裁员后:清零是君子豹变
人工智能·程序员
武子康3 小时前
调查研究-191 SenseVoice 不只是 ASR:把语音从“转文字“升级成“理解状态“
人工智能·深度学习·openai
程序员cxuan4 小时前
Codex 会把磁盘给烧了?完整复盘来了!
人工智能·后端·程序员