Multi-label Text classification with Keras/Theano and LSTM

2.4k views Asked by At

Trying to get runing LSTM multi-label text classification with Keras/Theano.

I have a text/label csv. Text is pure text, labels are numeric, nine in total, from 1 to 9.

I think I am not configuring the model properly for this problem. My code so far:

import keras.preprocessing.text
import numpy as np
     Using Theano backend.

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

import pandas
data = pandas.read_csv("for_keras_text_label.csv", sep = ',', quotechar = '"', header = 0)

x = data['text']
y = data['label']

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)

# the model
max_features = 20000
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(9))
model.add(Activation('softmax'))
model.compile(loss='sparse_categorical_crossentropy', optimizer='rmsprop',    metrics=["accuracy"])

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

I am getting this error:

 IndexError: index 9 is out of bounds for axis 1 with size 9 Apply node that caused the error: 
AdvancedIncSubtensor{inplace=False,  set_instead_of_inc=True}(Alloc.0, TensorConstant{1}, ARange{dtype='int64'}.0, Elemwise{Cast{int32}}.0)
Toposort index: 213
Inputs types: [TensorType(float32, matrix), TensorType(int8, scalar), TensorType(int64, vector), TensorType(int32, vector)]
Inputs shapes: [(200, 9), (), (200,), (200,)]
Inputs strides: [(36, 4), (), (8,), (4,)]
Inputs values: ['not shown', array(1, dtype=int8), 'not shown', 'not shown']
Outputs clients: [[Reshape{2}(AdvancedIncSubtensor{inplace=False,  set_instead_of_inc=True}.0, MakeVector{dtype='int64'}.0)]]

Backtrace when the node is created(use Theano flag traceback.limit=N to make it longer):
  File "/home/ubuntu/anaconda3/envs/theano/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2827, in run_ast_nodes
    if self.run_code(code, result):
  File "/home/ubuntu/anaconda3/envs/theano/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2881, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-14-5264b8e23f0a>", line 7, in <module>
    model.compile(loss='sparse_categorical_crossentropy', optimizer='rmsprop', metrics=["accuracy"])
  File "/home/ubuntu/anaconda3/envs/theano/lib/python2.7/site-packages/keras/models.py", line 578, in compile
    **kwargs)
  File "/home/ubuntu/anaconda3/envs/theano/lib/python2.7/site-packages/keras/engine/training.py", line 604, in compile
    sample_weight, mask)
  File "/home/ubuntu/anaconda3/envs/theano/lib/python2.7/site-packages/keras/engine/training.py", line 303, in weighted
    score_array = fn(y_true, y_pred)
  File "/home/ubuntu/anaconda3/envs/theano/lib/python2.7/site-packages/keras/objectives.py", line 45, in sparse_categorical_crossentropy
    return K.sparse_categorical_crossentropy(y_pred, y_true)
  File "/home/ubuntu/anaconda3/envs/theano/lib/python2.7/site-packages/keras/backend/theano_backend.py", line 1079, in sparse_categorical_crossentropy
    target = T.extra_ops.to_one_hot(target, nb_class=output.shape[-1])
0

There are 0 answers