Top AI Repos — open-source AI, indexed and scored
Top AI Repos tracks AI repositories on GitHub and answers two different questions about each one: is it moving right now, and would you bet a product on it.
Top AI Repos tracks AI repositories on GitHub and answers two different questions about each one: is it moving right now, and would you bet a product on it.
CNN-RNN中文文本分类,基于TensorFlow
| Date | Stars |
|---|---|
| 2026-07-30 | 4304 |
| 2026-08-07 | 4301 |
| 2026-08-15 | 4300 |
| 2026-08-18 | 4301 |
| 2026-09-03 | 4300 |
| 2026-09-07 | 4300 |
| 2026-09-08 | 4299 |
| 2026-09-12 | 4299 |
| 2026-09-14 | 4298 |
| 2026-09-17 | 4298 |
| 2026-09-18 | 4299 |
| 2026-09-20 | 4299 |
| 2026-09-24 | 4298 |
| 2026-09-27 | 4298 |
| 2026-09-28 | 4298 |
| 2026-09-30 | 4297 |
| 2026-10-02 | 4298 |
| 2026-10-03 | 4297 |
| 2026-10-06 | 4296 |
| 2026-10-07 | 4296 |
| 2026-10-11 | 4296 |
| 2026-10-13 | 4297 |
| 2026-10-14 | 4296 |
| 2026-10-18 | 4297 |
| 2026-10-19 | 4298 |
| 2026-10-23 | 4299 |
| 2026-10-24 | 4298 |
| 2026-10-30 | 4299 |
| 2026-11-03 | 4300 |
| 2026-11-04 | 4300 |
Today
— stars today
This week
+2 stars this week
This month
+3 stars this month
Momentum
0.0
growth rate 0.05%/day
# Text Classification with CNN and RNN
使用卷积神经网络以及循环神经网络进行中文文本分类
CNN做句子分类的论文可以参看: [Convolutional Neural Networks for Sentence Classification](https://arxiv.org/abs/1408.5882)
还可以去读dennybritz大牛的博客:[Implementing a CNN for Text Classification in TensorFlow](http://www.wildml.com/2015/12/implementing-a-cnn-for-text-classification-in-tensorflow/)
以及字符级CNN的论文:[Character-level Convolutional Networks for Text Classification](https://arxiv.org/abs/1509.01626)
本文是基于TensorFlow在中文数据集上的简化实现,使用了字符级CNN和RNN对中文文本进行分类,达到了较好的效果。
文中所使用的Conv1D与论文中有些不同,详细参考官方文档:[tf.nn.conv1d](https://www.tensorflow.org/api_docs/python/tf/nn/conv1d)
## 环境
- Python 2/3 (感谢[howie.hu](https://github.com/howie6879)调试Python2环境)
- TensorFlow 1.3以上
- numpy
- scikit-learn
- scipy
## 数据集
使用THUCNews的一个子集进行训练与测试,数据集请自行到[THUCTC:一个高效的中文文本分类工具包](http://thuctc.thunlp.org/)下载,请遵循数据提供方的开源协议。
本次训练使用了其中的10个分类,每个分类6500条数据。
类别如下:
```
体育, 财经, 房产, 家居, 教育, 科技, 时尚, 时政, 游戏, 娱乐
```
这个子集可以在此下载:链接: https://pan.baidu.com/s/1hugrfRu 密码: qfud
数据集划分如下:
- 训练集: 5000*10
- 验证集: 500*10
- 测试集: 1000*10
从原数据集生成子集的过程请参看`helper`下的两个脚本。其中,`copy_data.sh`用于从每个分类拷贝6500个文件,`cnews_group.py`用于将多个文件整合到一个文件中。执行该文件后,得到三个数据文件:
- cnews.train.txt: 训练集(50000条)
- cnews.val.txt: 验证集(5000条)
- cnews.test.txt: 测试集(10000条)
## 预处理
`data/cnews_loader.py`为数据的预处理文件。
- `read_file()`: 读取文件数据;
- `build_vocab()`: 构建词汇表,使用字符级的表示,这一函数会将词汇表存储下来,避免每一次重复处理;
- `read_vocab()`: 读取上一步存储的词汇表,转换为`{词:id}`表示;
- `read_category()`: 将分类目录固定,转换为`{类别: id}`表示;
- `to_words()`: 将一条由id表示的数据重新转换为文字;
- `process_file()`: 将数据集从文字转换为固定长度的id序列表示;
- `batch_iter()`: 为神经网络的训练准备经过shuffle的批次的数据。
经过数据预处理,数据的格式如下:
| Data | Shape | Data | Shape |
| :---------- | :---------- | :---------- | :---------- |
| x_train | [50000, 600] | y_train | [50000, 10] |
| x_val | [5000, 600] | y_val | [5000, 10] |
| x_test | [10000, 600] | y_test | [10000, 10] |
## CNN卷积神经网络
### 配置项
CNN可配置的参数如下所示,在`cnn_model.py`中。
```python
class TCNNConfig(object):
"""CNN配置参数"""
embedding_dim = 64 # 词向量维度
seq_length = 600 # 序列长度
num_classes = 10 # 类别数
num_filters = 128 # 卷积核数目
kernel_size = 5 # 卷积核尺寸
vocab_size = 5000 # 词汇表达小
hidden_dim = 128 # 全连接层神经元
dropout_keep_prob = 0.5 # dropout保留比例
learning_rate = 1e-3 # 学习率
batch_size = 64 # 每批训练大小
num_epochs = 10 # 总迭代轮次
print_per_batch = 100 # 每多少轮输出一次结果
save_per_batch = 10 # 每多少轮存入tensorboard
```
### CNN模型
具体参看`cnn_model.py`的实现。
大致结构如下:

### 训练与验证
运行 `python run_cnn.py train`,可以开始训练。
> 若之前进行过训练,请把tensorboard/textcnn删除,避免TensorBoard多次训练结果重叠。
```
Configuring CNN model...
Configuring TensorBoard and Saver...
Loading training and validation data...
Time usage: 0:00:14
Training and evaluating...
Epoch: 1
Iter: 0, Train Loss: 2.3, Train Acc: 10.94%, Val Loss: 2.3, Val Acc: 8.92%, Time: 0:00:01 *
Iter: 100, Train Loss: 0.88, Train Acc: 73.44%, Val Loss: 1.2, Val Acc: 68.46%, Time: 0:00:04 *
Iter: 200, Train Loss: 0.38, Train Acc: 92.19%, Val Loss: 0.75, Val Acc: 77.32%, Time: 0:00:07 *
Iter: 300, Train Loss: 0.22, Train Acc: 92.19%, Val Loss: 0.46, Val Acc: 87.08%, Time: 0:00:09 *
Iter: 400, Train Loss: 0.24, Train Acc: 90.62%, Val Loss: 0.4, Val Acc: 88.62%, Time: 0:00:12 *
Iter: 500, Train Loss: 0.16, Train Acc: 96.88%, Val Loss: 0.36, Val Acc: 90.38%, Time: 0:00:15 *
Iter: 600, Train Loss: 0.084, Train Acc: 96.88%, Val Loss: 0.35, Val Acc: 91.36%, Time: 0:00:17 *
Iter: 700, Train Loss: 0.21, Train Acc: 93.75%, Val Loss: 0.26, Val Acc: 92.58%, Time: 0:00:20 *
Epoch: 2
Iter: 800, Train Loss: 0.07, Train Acc: 98.44%, Val Loss: 0.24, Val Acc: 94.12%, Time: 0:00:23 *
Iter: 900, Train Loss: 0.092, Train Acc: 96.88%, Val Loss: 0.27, Val Acc: 92.86%, Time: 0:00:25
Iter: 1000, Train Loss: 0.17, Train Acc: 95.31%, Val Loss: 0.28, Val Acc: 92.82%, TimeExcerpt of 12,320 characters
Read on GitHub55
howie.hu
3
1
Would you bet a product on this? Bounded 0–100 and slow moving.
matched fp:7e68b8028e03b1d2, topic:text-classification, name:text classification, readme:text classification
matched fp:7e68b8028e03b1d2, topic:tensorflow