Keras - 文本分类 - LSTM - 如何输入文本?

Keras - Text Classification - LSTM - How to input text?

我正在尝试了解如何使用 LSTM 对我拥有的某个数据集进行分类。

我研究并找到了 keras 和 imdb 的这个例子: https://github.com/fchollet/keras/blob/master/examples/imdb_lstm.py

但是,我对必须如何处理数据集才能输入感到困惑。

我知道keras有预处理文本的方法,但我不确定该用哪个。

x 包含 n 行文本,y 按 happiness/sadness 对文本进行分类。基本上,1.0 表示 100% 快乐,0.0 表示完全悲伤。数字可能会有所不同,例如0.25~~等等。

所以我的问题是,我如何正确输入 x 和 y?我必须使用词袋吗? 任何提示表示赞赏!

我在下面对此进行了编码,但我不断收到相同的错误:

#('Bad input argument to theano function with name ... at index 1(0-based)', 
'could not convert string to float: negative')
import keras.preprocessing.text
import numpy as np

np.random.seed(1337)  # for reproducibility

from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.layers.embeddings import Embedding
from keras.layers.recurrent import LSTM

print('Loading data...')
import pandas

thedata = pandas.read_csv("dataset/text.csv", sep=', ', delimiter=',', header='infer', names=None)

x = thedata['text']
y = thedata['sentiment']

x = x.iloc[:].values
y = y.iloc[:].values

###################################
tk = keras.preprocessing.text.Tokenizer(nb_words=2000, filters=keras.preprocessing.text.base_filter(), lower=True, split=" ")
tk.fit_on_texts(x)

x = tk.texts_to_sequences(x)


###################################
max_len = 80
print "max_len ", max_len
print('Pad sequences (samples x time)')

x = sequence.pad_sequences(x, maxlen=max_len)

#########################
max_features = 20000
model = Sequential()
print('Build model...')

model = Sequential()
model.add(Embedding(max_features, 128, input_length=max_len, dropout=0.2))
model.add(LSTM(128, dropout_W=0.2, dropout_U=0.2))
model.add(Dense(1))
model.add(Activation('sigmoid'))

model.compile(loss='binary_crossentropy', optimizer='rmsprop')

model.fit(x, y=y, batch_size=200, nb_epoch=1, verbose=1, validation_split=0.2, show_accuracy=True, shuffle=True)

# at index 1(0-based)', 'could not convert string to float: negative')

查看您如何使用 CSV 解析器读取文本。确保字段的格式为“文本”、“情绪”(如果您想使用您在代码中编写的解析器)。