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

相关推荐
7177773 小时前
国内 AI 研发效能工具怎么选:2026 年基于公开信息的选型框架与 Gitee 实践
人工智能·gitee
Csvn4 小时前
换供应商要改一遍代码?把模型接入收进一层网关(C04)
人工智能·aigc·agent
孟健4 小时前
开源AutoClip切直播:1小时48分出19段,整月API只花0.08元
人工智能·开源·aigc
翰墨之道4 小时前
AI日报:豆包工作ARR超20亿,GPT-6.2跑分与实测割裂
人工智能·gpt·数据挖掘
Maynor9965 小时前
Trymodel 使用介绍:一个平台同时用 Claude、GPT、Gemini 和 Grok
人工智能·gpt·ai·程序员·教程·claude·语音
字节跳动视频云技术团队6 小时前
接大模型别再自己配 Key、搭代理了,火山引擎 IGA Pages 一步搞定
人工智能
阿里云大数据AI技术6 小时前
EMR Serverless StarRocks 产品介绍:从统一 OLAP 走向多模态分析与检索
人工智能
Forget the Dream7 小时前
Spring AI 2.0 系列(二):第一个 ChatClient——从注入到一次完整调用
java·人工智能·spring boot·ai·springai
郝学胜-神的一滴7 小时前
Effective Python 条款35:用类封装替代生成器异常注入
开发语言·人工智能·python·金融·pandas·scikit-learn·matplotlib
火山引擎开发者社区8 小时前
豆包工作,3 个月,800 万,20 亿
人工智能