天天看点

RNN分类IMDB电影评分

本文从头开始实践如何利用RNN构建一个电影评价的正负面分类器。

1.  IMDB数据集的下载

2.  数据预处理

# 数据准备
from keras.datasets import imdb
# 可以直接使用 imdb.load_data() 下载数据
from keras.preprocessing import sequence
from keras.preprocessing.text import Tokenizer
import re
import os

# 用来去掉文本中的标签,例如<p>,</p>
re_tag = re.compile(r'<[^>]+>')
def rm_tags(text):
    return re_tag.sub('', text)

# 读取文件
def read_files(filetype):
    path = "data/aclImdb/"
    file_list = []
    positive_path = path + filetype + "/pos/"
    for f in os.listdir(positive_path):
        file_list += [positive_path + f]
    negative_path = path + filetype + "/neg/"
    for f in os.listdir(negative_path):
        file_list += [negative_path + f]
    print('read', filetype, 'files:', len(fi