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

相关推荐
鲲穹AI种草1 分钟前
文本内容规范化整理,多款 AI 格式化工具能力客观记录
人工智能·文本内容
冬奇Lab6 分钟前
一天一个开源项目(第211篇):prime-agent —— 会自我改进的 RLM 编码智能体
人工智能·开源·资讯
大模型丫丫13 分钟前
用 TypeScript、NestJS 和 LangGraph 搭建一个可控的 AI Agent
javascript·人工智能·typescript
冬奇Lab35 分钟前
Code Agent 解剖(23):从零扩展——接入 MCP 外部工具生态
人工智能·agent
xian_wwq37 分钟前
【学习笔记】深度认知系列-第16讲-提示词工程
人工智能·笔记·学习
乃嘿仔42 分钟前
AI 热点日报 · 2026-09-04
人工智能·chatgpt
落羽的落羽1 小时前
【AI】快速理解AI应用的相关名词概念
linux·c++·人工智能·python·计算机网络·算法
“AI国潮设计-小江”1 小时前
《Python实战 | SDXL大模型批量生成“英歌舞海浪”蛋糕IP,附核心Prompt控制代码与IP授权变现思路》
人工智能·python·prompt·aigc
虹科网络安全1 小时前
使用艾体宝 IOTA 10 CORE+ 监控企业网络中的 AI 流量
网络·人工智能
朝阳资本论1 小时前
群核科技:从空间设计龙头到物理AI“卖水人”的升维之战
人工智能