{"id":13535139,"url":"https://github.com/liyibo/text-classification-demos","last_synced_at":"2025-04-02T00:32:32.917Z","repository":{"id":44424643,"uuid":"163942168","full_name":"liyibo/text-classification-demos","owner":"liyibo","description":"Neural models for Text Classification in Tensorflow, such as cnn, dpcnn, fasttext, bert ...","archived":false,"fork":false,"pushed_at":"2019-03-25T05:15:47.000Z","size":950,"stargazers_count":191,"open_issues_count":5,"forks_count":44,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-11-02T23:32:20.299Z","etag":null,"topics":["bert","cnn","fasttext","tensorflow","text-classification"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/liyibo.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-01-03T08:07:43.000Z","updated_at":"2024-06-06T11:48:54.000Z","dependencies_parsed_at":"2022-09-21T09:10:38.316Z","dependency_job_id":null,"html_url":"https://github.com/liyibo/text-classification-demos","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liyibo%2Ftext-classification-demos","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liyibo%2Ftext-classification-demos/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liyibo%2Ftext-classification-demos/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liyibo%2Ftext-classification-demos/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/liyibo","download_url":"https://codeload.github.com/liyibo/text-classification-demos/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246735353,"owners_count":20825221,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bert","cnn","fasttext","tensorflow","text-classification"],"created_at":"2024-08-01T08:00:50.200Z","updated_at":"2025-04-02T00:32:32.193Z","avatar_url":"https://github.com/liyibo.png","language":"Python","funding_links":[],"categories":["BERT classification task:"],"sub_categories":[],"readme":"\n# Text classification demos\n\nTensorflow 环境下,不同的神经网络模型对中文文本进行分类，本文中的 demo 都是字符级别的文本分类(增加了word-based 的统计结果)，简化了文本分类的流程，字符级别的分类在有些任务上的效果可能不好，需要结合实际情况添加自定义的分词模块。  \n\n## 数据集  \n\n下载地址: https://pan.baidu.com/s/1hugrfRu 密码: qfud\n\n使用 THUCNews 的一个子集进行训练与测试，使用了其中的 10 个分类，每个分类 6500 条数据。\n\n类别如下：\n\n体育, 财经, 房产, 家居, 教育, 科技, 时尚, 时政, 游戏, 娱乐\n\n数据集划分如下：\n\n训练集: 5000 \\* 10  \n验证集: 500 \\* 10  \n测试集: 1000 \\* 10  \n\n具体介绍请参考：[text-classification-cnn-rnn](https://github.com/gaussic/text-classification-cnn-rnn)  \n\n## 分类效果  \n\n- char-based\n\n| model      |fasttext |   cnn   |   rnn   |  rcnn   |   han   |  dpcnn  |  bert   |\n|:-----      | :-----: | :-----: | :-----: | :-----: | :-----  | :-----: | :-----: |\n| val_acc    |  92.92  |  93.56  |  93.56  |  94.36  |  93.94  |  93.70  |  97.84  |\n| test_acc   |  93.15  |  94.57  |  94.37  |  95.53  |  93.65  |  94.87  |  96.93  |\n\n- word-based\n\n| model      |fasttext |   cnn   |   rnn   |  rcnn   |   han   |  dpcnn  |  bert   |\n|:-----      | :-----: | :-----: | :-----: | :-----: | :-----  | :-----: | :-----: |\n| val_acc    |  95.52  |  95.28  |  93.10  |  95.60  |  95.10  |  95.68  |    -    |\n| test_acc   |  95.34  |  95.77  |  94.05  |  96.36  |  95.66  |  95.97  |    -    |\n\n\n## 模型介绍  \n\n### 1、FastText  \n\nfasttext_model.py 文件为训练和测试 fasttext 模型的代码\n\n![图1 FastText 模型结构图](images/fasttext.jpg?raw=true)\n\n本代码简化了 fasttext 模型的结构，模型结构非常简单，运行速度简直飞快，模型准确率也不错，可根据实际需要优化模型结构\n\n### 2、TextCNN  \n\ncnn_model.py 文件为训练和测试 TextCNN 模型的代码\n\n![图2 TextCNN 模型结构图](images/textcnn.jpg?raw=true)\n\n本代码实现了 TextCNN 模型的结构，通过 3 个不同大小的卷积核，对输入文本进一维卷积，分别 pooling 三个卷积之后的 feature， 拼接到一起，然后进行 dense 操作，最终输出模型结果。可实现速度和精度之间较好的折中。\n\n### 3、RNN\n\nrnn_model.py 文件为训练和测试 TextCNN 模型的代码\n\n![图8 TextRNN 模型结构图](images/textrnn.jpg?raw=true)\n\n本代码实现了 TextRNN 模型的结构，对输入序列进行embedding，然后输入两层的 rnn_cell中学习序列特征，取最后一个 word 的 state 作为进行后续的 fc 操作，最终输出模型结果。\n\n### 4、RCNN  \n\nrcnn_model.py 文件为训练和测试 RCNN 模型的代码\n\n![图3 RCNN 模型结构图](images/rcnn.jpg?raw=true)\n\n[Recurrent Convolutional Neural Network for Text Classification](https://scholar.google.com.hk/scholar?q=Recurrent+Convolutional+Neural+Networks+for+Text+Classification\u0026hl=zhCN\u0026as_sdt=0\u0026as_vis=1\u0026oi=scholart\u0026sa=X\u0026ved=0ahUKEwjpx82cvqTUAhWHspQKHUbDBDYQgQMIITAA), 在学习 word representations 时候，同时采用了 rnn 结构来学习 word 的上下文，虽然模型名称为 RCNN，但并没有显式的存在卷积操作。\n\n\n1、采用双向lstm学习 word 的上下文\n\n```\n   c_left = tf.concat([tf.zeros(shape), output_fw[:, :-1]], axis=1, name=\"context_left\")\n   c_right = tf.concat([output_bw[:, 1:], tf.zeros(shape)], axis=1, name=\"context_right\")\n   word_representation = tf.concat([c_left, embedding_inputs, c_right], axis=2, name=\"last\")\n```\n2、pooling + softmax\n\n  word_representation  的维度是 batch_size \\* seq_length \\* 2 \\* context_dim + embedding_dim\n\n  在 seq_length 维度进行 max pooling，然后进行 fc 操作就可以进行分类了，可以将该网络看成是 fasttext 的改进版本\n\n\n### 5、HAN  \n\nhan_model.py 文件为训练和测试 HAN 模型的代码\n\n![图4 HAN 模型结构图](images/han.jpg?raw=true)\n\nHAN 为 Hierarchical Attention Networks，将待分类文本，分为一定数量的句子，分别在 word level 和 sentence level 进行 encoder 和 attention 操作，从而实现对较长文本的分类。  \n\n本文是按照句子长度将文本分句的，实际操作中可按照标点符号等进行分句，理论上效果能好一点。\n\n- 1、对文本进行分句\n\n\n  对每个句子进行双向lstm编码\n\n  batch_size = 64, seq_length = 600,\n  sent_num = 10, emb_size = 128,\n  lstm_hid_dim = 256\n\n  数据维度变化：64 \\* 600 \\* 128 --- (64\\*10） \\* 60 \\* 128 --- (64\\*10） \\* 60 \\* 512\n\n\n- 2、word level attention\n\n![图4 attention](images/han_2.jpg?raw=true)\n\n(1) 将输入的lstm编码结果做一次非线性变换，可以看做是输入编码的hidden representation， shape = (64\\*10） \\* 60 \\* 256\n\n(2) 将 hidden representation 与一个学习得到的 word level context vector 的相似性进行 softmax，得到每个单词在句子中的权重\n\n(3) 对输入的lstm 编码进行加权求和，得到句子的向量表示\n\n数据维度变化：(64\\*10） \\* 60 \\* 512 --- (64\\*10） \\* 512\n\n- 3、得到每个句子的向量表示\n\n- 4、sentence level attention\n\n  与 word level attention 过程一样，只是该层是句子级别的attention\n\n  数据维度变化：64 \\* 10 \\* 512 --- 64 \\* 512\n\n- 5、得到 document 的向量表示\n\n- 6、dence + softmax\n\n\n### 6、DPCNN  \n\ndpcnn_model.py 文件为训练和测试 DPCNN 模型的代码  \n\n![图5 DPCNN 模型结构图](images/dpcnn.jpg?raw=true)\n\nDPCNN 通过卷积和残差连接增加了以往用于文本分类 CNN 网络的深度，可以有效提取文本中的远程关系特征，并且复杂度不高，实验表名，效果比以往的 CNN 结构要好一点。\n\n- region_embedding: word_embedding 之后进行的 ngram 卷积结果\n\n### 7、BERT  \n\nbert_model.py 文件为训练和测试 BERT 模型的代码  \n\ngoogle官方提供用于文本分类的demo写的比较抽象，所以本文基于 google 提供的代码和初始化模型，重写了文本分类模型的训练和测试代码，bert 分类模型在小数据集下效果很好，通过较少的迭代次数就能得到很好的效果，但是训练和测试速度较慢，这点不如基于 CNN 的网络结构。  \n\nbert_model.py 将训练数据和验证数据存储为 tfrecord 文件，然后进行训练  \n\n由于 bert 提供的预训练模型较大，需要自己去 [google-research/bert](https://github.com/google-research/bert) 中下载预训练好的模型，本实验采用的是 \"BERT-Base, Chinese\" 模型。\n\n![图6 BERT 输入数据格式](images/bert_1.jpeg?raw=true)\n\n![图7 BERT 下游任务介绍](images/bert_2.jpeg?raw=true)\n\n## 参考  \n\n- 1 [text-classification-cnn-rnn](https://github.com/gaussic/text-classification-cnn-rnn)  \n- 2 [text_classification](https://github.com/brightmart/text_classification)  \n- 3 [bert](https://github.com/google-research/bert)  \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliyibo%2Ftext-classification-demos","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fliyibo%2Ftext-classification-demos","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliyibo%2Ftext-classification-demos/lists"}