【发布时间】:2018-08-20 00:01:57
【问题描述】:
我正在尝试使用 Keras 训练一个基本的文本分类 NN。我从一个网站下载了 12,500 个 pos 和 12,500 个负面电影评论。但是,我无法将数据处理成 Keras 可以使用的东西。
首先,我打开 25000 个文本文件并将每个文件存储到一个数组中。然后我通过这个函数运行每个数组(一个正一个负):
def process_for_model(textArray):
'''
Given a 2D array of the form:
[[fileLines1],[fileLines2]...[fileLinesN]]
converts the text into integers
'''
result = []
for file_ in textArray:
inner = []
for line in file_:
length = len(set(text_to_word_sequence(line)))
inner.append(hashing_trick(line,round(length*1.3),hash_function='md5'))
result.append(inner)
return result
目的是将单词转换为数字,使它们接近 Keras 模型可以使用的东西。
然后我将转换后的数字附加到一个数组中,同时将 0 或 1 作为标签附加到另一个数组中:
training_labels = []
train_batches = []
for i in range(len(positive_encoded)):
train_batches.append(positive_encoded[i])
training_labels.append([0])
for i in range(len(negative_encoded)):
train_batches.append(negative_encoded[i])
training_labels.append([1])
最后我将每个数组转换为一个 np 数组:
train_batches = array(train_batches)
training_labels = array(training_labels)
但是,我不确定从这里去哪里。我相信每条评论都是 168 字。我不知道如何为这些数据创建合适的模型,也不知道如何使用 sklearn 将所有数字正确缩放到 0 和 1 之间。
我最困惑的事情是:我应该有多少层,每层应该有多少神经元,以及第一层我应该有多少输入维度。
我应该完全采用另一种方法吗?
【问题讨论】:
标签: python machine-learning keras text-classification