寫這篇文章的初衷:
最近帶一個本科生做畢設(shè)夏醉,畢設(shè)內(nèi)容是用lstm做情感分析琼懊。文章思路其實就是一個文本三分類的問題(正、中肮帐、負)咖驮。
首先:
該文章用到了word embedding,可以使用gensim里面的word2vec工具訓(xùn)練word embedding训枢。訓(xùn)練出來的詞向量是一個固定維度的向量托修。而訓(xùn)練的過程是使用word2vec里面的兩個模型(CBOW或skip-gram)進行訓(xùn)練的。我們可以將這模型的原理是Huffman樹恒界。但是睦刃,今天我找到一個更加通俗、更加易于理解的解釋:
訓(xùn)練好的詞向量模型被保存下來,該模型的本質(zhì)就是一個m*n的矩陣,m代表訓(xùn)練語料中詞的個數(shù)缀去,n代表訓(xùn)練時我們設(shè)定的詞向量維度旦袋。當(dāng)我們訓(xùn)練好模型后再次調(diào)用時锣杂,就可以從該模型中直接獲取到對應(yīng)詞的詞向量。這種表示方法相比one-hot編碼不知要好了多少倍,原因是one-hot編碼是一個基于統(tǒng)計的編碼方式,不觸及到文本的語義層面搓彻。
其次:
通過上面我們可以拿到每個詞的詞向量,但是我們?nèi)蝿?wù)處理時一般是對句子或文本進行操作(如文本分類嘱朽、情感分析等等)旭贬,那下一步該怎么辦呢?好搪泳,別著急稀轨,看下圖:https://github.com/Babyzpj/NLP/tree/master/SentimentAnalysis-master
通過該圖我們知道,當(dāng)我們拿到一個詞向量后岸军,那么一個句子或一個文本就可以用詞表示成矩陣(假設(shè)一個句子有5個詞奋刽,詞向量維度是64,那么該矩陣就是5*64),然后可以用CNN或RNN(LSTM)模型將該矩陣編碼成一個一維向量凛膏,并保留大多數(shù)文本信息杨名。然后將該向量作為深度神經(jīng)網(wǎng)絡(luò)分類器的輸入脏榆,即可得到最終的結(jié)果:
https://yq.aliyun.com/articles/221681
http://blog.sina.com.cn/s/blog_1450ac3c60102x79x.html
最后:
下面給出使用keras將文本向量矩陣進行一維化的例子:
from keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers.embeddings import Embedding
# define documents
docs = ['Well done!',
'Good work',
'Great effort',
'nice work',
'Excellent!',
'Weak',
'Poor effort!',
'not good',
'poor work',
'Could have done better.']
# define class labels
labels = [1,1,1,1,1,0,0,0,0,0]
# integer encode the documents
vocab_size = 50
encoded_docs = [one_hot(d, vocab_size) for d in docs]
print(encoded_docs)
# pad documents to a max length of 4 words
max_length = 4
padded_docs = pad_sequences(encoded_docs, maxlen=max_length, padding='post')
print(padded_docs)
# define the model
model = Sequential()
model.add(Embedding(vocab_size, 8, input_length=max_length))
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))
# compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc'])
# summarize the model
print(model.summary())
# fit the model
model.fit(padded_docs, labels, epochs=50, verbose=0)
# evaluate the model
loss, accuracy = model.evaluate(padded_docs, labels, verbose=0)
print('Accuracy: %f' % (accuracy*100))
以下為實驗結(jié)果:
[[33, 37], [18, 16], [31, 24], [33, 16], [5], [11], [34, 24], [11, 18], [34, 16], [48, 38, 37, 7]]
[[33 37 0 0]
[18 16 0 0]
[31 24 0 0]
[33 16 0 0]
[ 5 0 0 0]
[11 0 0 0]
[34 24 0 0]
[11 18 0 0]
[34 16 0 0]
[48 38 37 7]]
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding_1 (Embedding) (None, 4, 8) 400
_________________________________________________________________
flatten_1 (Flatten) (None, 32) 0
_________________________________________________________________
dense_1 (Dense) (None, 1) 33
=================================================================
Total params: 433
Trainable params: 433
Non-trainable params: 0
_________________________________________________________________
None
Accuracy: 89.999998
Process finished with exit code 0
參考:
1须喂、http://blog.sina.com.cn/s/blog_1450ac3c60102x79x.html
2吁断、https://machinelearningmastery.com/use-word-embedding-layers-deep-learning-keras/
3趁蕊、http://keras-cn.readthedocs.io/en/latest/layers/embedding_layer/