【问题标题】:Text classification with LSTM Network and Keras 0.0% accuracy使用 LSTM 网络和 Keras 进行文本分类,准确率为 0.0%
【发布时间】:2019-05-26 22:53:54
【问题描述】:

我有两列的 csv 文件:

category, description

文件中有 1030 个类别,只有大约 12,600 行

我需要一个文本分类模型,并根据这些数据进行训练。我将 keras 与 LSTM 模型一起使用。

我找到了一篇描述如何进行二元分类的文章,并对其稍作修改以使用多个类别。

我的代码:

import pandas as pd
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Embedding, LSTM
from numpy import array
from keras.preprocessing.text import one_hot
from sklearn.preprocessing import LabelEncoder
from keras.preprocessing import sequence 
import keras

df = pd.read_csv('/tmp/input_data.csv')

#one hot encode your documents

# integer encode the documents
vocab_size = 2000
encoded_docs = [one_hot(d, vocab_size) for d in df['description']]

def load_data_from_arrays(strings, labels, train_test_split=0.9):
    data_size = len(strings)
    test_size = int(data_size - round(data_size * train_test_split))
    print("Test size: {}".format(test_size))

    print("\nTraining set:")
    x_train = strings[test_size:]
    print("\t - x_train: {}".format(len(x_train)))
    y_train = labels[test_size:]
    print("\t - y_train: {}".format(len(y_train)))

    print("\nTesting set:")
    x_test = strings[:test_size]
    print("\t - x_test: {}".format(len(x_test)))
    y_test = labels[:test_size]
    print("\t - y_test: {}".format(len(y_test)))

    return x_train, y_train, x_test, y_test


encoder = LabelEncoder()
categories = encoder.fit_transform(df['category'])
num_classes = np.max(categories) + 1
print('Categories count: {}'.format(num_classes))
#Categories count: 1030

X_train, y_train, x_test, y_test = load_data_from_arrays(encoded_docs, categories, train_test_split=0.8)

# Truncate and pad the review sequences 

max_review_length = 500 
X_train = sequence.pad_sequences(X_train, maxlen=max_review_length) 
x_test = sequence.pad_sequences(x_test, maxlen=max_review_length) 

y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
print('y_train shape:', y_train.shape)
print('y_test shape:', y_test.shape)

# Build the model 
embedding_vector_length = 32 
top_words = 10000

model = Sequential() 
model.add(Embedding(top_words, embedding_vector_length, input_length=max_review_length)) 
model.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2)) 
model.add(Dense(num_classes, activation='softmax')) 
model.compile(loss='categorical_crossentropy',optimizer='adam', metrics=['accuracy']) 
print(model.summary())

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
embedding_8 (Embedding)      (None, 500, 32)           320000    
_________________________________________________________________
lstm_8 (LSTM)                (None, 100)               53200     
_________________________________________________________________
dense_8 (Dense)              (None, 1030)              104030    
=================================================================
Total params: 477,230
Trainable params: 477,230
Non-trainable params: 0
_________________________________________________________________
None

#Train the model
model.fit(X_train, y_train, validation_data=(x_test, y_test), epochs=5, batch_size=64) 

Train on 10118 samples, validate on 2530 samples
Epoch 1/5
10118/10118 [==============================] - 60s 6ms/step - loss: 6.5086 - acc: 0.0019 - val_loss: 10.0911 - val_acc: 0.0000e+00
Epoch 2/5
10118/10118 [==============================] - 63s 6ms/step - loss: 6.3281 - acc: 0.0028 - val_loss: 10.8270 - val_acc: 0.0000e+00
Epoch 3/5
10118/10118 [==============================] - 63s 6ms/step - loss: 6.3120 - acc: 0.0024 - val_loss: 11.0078 - val_acc: 0.0000e+00
Epoch 4/5
10118/10118 [==============================] - 64s 6ms/step - loss: 6.2891 - acc: 0.0030 - val_loss: 11.8264 - val_acc: 0.0000e+00
Epoch 5/5
10118/10118 [==============================] - 69s 7ms/step - loss: 6.2559 - acc: 0.0032 - val_loss: 12.1625 - val_acc: 0.0000e+00

#Evaluate the model
scores = model.evaluate(x_test, y_test, verbose=0) 
print("Accuracy: %.2f%%" % (scores[1]*100))

Accuracy: 0.00%

我在准备数据时犯了什么错误? 为什么准确率总是0?

【问题讨论】:

    标签: python tensorflow keras deep-learning


    【解决方案1】:

    我猜你的 vocab_size 太低了。如果您正在处理通常的文本,请尝试以 10.000 - 100.000 作为起点。

    one_hot 所做的是使用hashing trick。这意味着您的所有单词都经过哈希处理并投影到 2000 个向量空间中。这不仅意味着您的 dict 有 2000 个单词长,还意味着每个单词都会被投影到这个空间中,这实际上会导致很多冲突,其中单词具有相同的索引并且在 LSTM 中被认为是相等的。

    此外,您还应该查看转换后的文本,以了解此处发生的情况。为此,请构建反向查找并将所有索引转换回来。

    作为进一步的改进,可以使用词干提取、规范化等常用技术对文本进行预处理,并使用词汇表或丢弃词袋并使用词嵌入。

    【讨论】:

    • 我试过vocab_size = 100000,但结果没有改变。 “要做到这一点,建立一个反向查找并将所有索引转换回来” - 你能给我一个提示如何做到这一点吗?谢谢
    • 我猜你的代码有问题。尝试在将数据传递给 LSTM 之前对其进行检查。您可以尝试使用预训练嵌入
    猜你喜欢
    • 2017-12-11
    • 2021-05-08
    • 2019-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    • 2018-05-31
    • 2016-12-03
    相关资源
    最近更新 更多