Text Classification with CNN and RNN
使用卷积神经网络以及循环神经网络进行中文文本分类
1、 环境配置
-
Python 2/3
-
TensorFlow 1.3以上
-
numpy
-
scikit-learn
-
scipy
2、 数据集
使用THUCNews的一个子集进行训练与测试,
本次训练使用了其中的10个分类,每个分类6500条数据。
类别如下:
```
体育, 财经, 房产, 家居, 教育, 科技, 时尚, 时政, 游戏, 娱乐
```
这个子集可以在此下载:链接: https://pan.baidu.com/s/1hugrfRu 密码: qfud
数据集划分如下:
训练集: 5000*10
验证集: 500*10
测试集: 1000*10
原数据集生成子集:
helper下的两个脚本。
copy_data.sh用于从每个分类拷贝6500个文件,
#!/bin/bash
# copy MAXCOUNT files from each directory
MAXCOUNT=6500
# shellcheck disable=SC2045
for category in $( ls D:/data_training_sample/THUCNews); do
echo item: $category
dir=D:/data_training_sample/THUCNews/$category
newdir=D:/data_training_sample/helper/txt
if [ -d $newdir ]; then
rm -rf $newdir
mkdir $newdir
fi
COUNTER=1
for i in $(ls $dir); do
cp $dir/$i $newdir
if [ $COUNTER -ge $MAXCOUNT ]
then
echo finished
break
fi
let COUNTER=COUNTER+1
done
done
cnews_group.py用于将多个文件整合到一个文件中。执行该文件后,得到三个数据文件:
- cnews.train.txt: 训练集(50000条)
- cnews.val.txt: 验证集(5000条)
- cnews.test.txt: 测试集(10000条)
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
将文本整合到 train、test、val 三个文件中
"""
import os
def _read_file(filename):
"""读取一个文件并转换为一行