1. 概述
在情感分析的應用領域,例如判斷某一句話是positive或者是negative的案例中,咱們可以通過傳統的standard neuro network來作為解決方案,但是傳統的神經網絡在應用的時候是不能獲取前后文字之間的關系的,不能獲取到整個句子的一個整體的意思,只能通過每一個詞的意思來最終決定一句話的情感,這顯然是不合理的,導致的結果就是訓練出來的模型質量可能不是很高。那么這里就需要用到LSTM來解決這個問題了,LSTM能夠很好的表達出句子中詞的關系,能將句子當做一個整體來看待,而不是一個個單獨的詞。所以這一節主要講一下如何應用LSTM來做句子的情感分析的應用,這里主要包括2部分內容,第一部分是設計整個LSTM的網絡結構,第二步是用代碼是實現這個網絡。
2. 結構設計
首先咱們知道應用的目的是根據句子的內容來判斷情感的,那么咱們就可以很明確的知道這是一個Sequence model, 其次這個Sequence Model還是一個Many-To-One的結構。那么這里我為了提高咱們應用的復雜度,我選用了一個2層的LSTM layer, 就是將第一層的每一個LSTM cell 的輸出作為第二層的每一個相應的LSTM cell的輸入。聽起來挺抽象的,實際咱們通過下面的圖可以很清楚的看出它的整體結構(注意:下面的圖,我省略了LSTM的memory cell,沒有畫出來)。實際的應用中,咱們很少會有大於3層的LSTM layer,因為它計算所需要的資源是非常大的。其實在這個例子中,一層的LSTM layer其實是足夠的,我這里只是為了演示2層layer,所以故意這樣處理的,下面咱們看一下結構圖吧
首先第一步還是要將所有的Word經過embedding layer, 然后再將這些features帶入到LSTM layer中。第一層的LSTM layer每一個time step的輸出都需要作為第二步的輸入,所以在下面的代碼中,第一層的LSTM的return_sequences需要設置成true;而第二層中只需要最后一個time step的輸出,所以return_sequences設置成false。因為咱們這是的a, c設置的dimension都比較大,所以每一步都經過一個dropout layer讓它按照一定的比例丟棄一部分信息,否則咱們的計算成本太高了。既然它的結構咱們已經設計出來了,那接下來咱們看看它的代碼實現過程吧。
3.神經網絡結構的代碼實現
上面的代碼咱們已經設計出來咱們對於這個應用的網絡結構,接下來就是代碼實現的過程,這里咱們應用了Keras的Functional API來搭建網絡結構,咱們直接看一下代碼吧,這里我只展示這個神經網絡的構建代碼:
def Sentiment_Model(input_shape): """ Function creating the Emojify-v2 model's graph. Arguments: input_shape -- shape of the input, usually (max_len,) Returns: model -- a model instance in Keras """ # Define sentence_indices as the input of the graph. # It should be of shape input_shape and dtype 'int32' (as it contains indices, which are integers). sentence_sequences = Input(input_shape, dtype='int32') # Propagate sentence_indices through your embedding layer # (See additional hints in the instructions). embeddings = Keras.Embedding(sentence_sequences) # Propagate the embeddings through an LSTM layer with 128-dimensional hidden state # The returned output should be a batch of sequences. X = LSTM(128, return_sequences = True)(embeddings) # Add dropout with a probability of 0.5 X = Dropout(0.5)(X) # Propagate X trough another LSTM layer with 128-dimensional hidden state # The returned output should be a single hidden state, not a batch of sequences. X = LSTM(128, return_sequences=False)(X) # Add dropout with a probability of 0.5 X = Dropout(0.5)(X) # Propagate X through a Dense layer with 5 units X = Dense(5)(X) # Add a softmax activation X = Activation('softmax')(X) # Create Model instance which converts sentence_indices into X. model = Model(inputs=sentence_sequences, outputs=X) return model
首先第一步上面的model要定義一個Input, 它的shape就是一句話的長度;后面的embedding layer, Dropout layer和LSTM layer都是按照上面的結構來實現的。具體里面的細節,我在代碼中的注釋已經寫得非常清楚了。主要有幾個細節部分需要注意一下,第一個是hidden state的dimension;第二個是return_sequence的參數,如果大家對return_sequence和return_state的參數不理解,有疑問,我推薦大家看一下這個博客,解釋的非常到位, https://machinelearningmastery.com/return-sequences-and-return-states-for-lstms-in-keras/;第三個就是上面的Input_shape是省略batch_size參數的;至於這個model的fitting, evaluation等其他過程和其他的model都是一樣的,這里我就不展開來說了,最核心的就是上面的這個結構的構建。
4. 總結
這里主要講述了一個Many-to-One結構的LSTM的一般應用,這里主要是用一個情感分析的例子來演示,但是它的應用非常廣泛的。這里的重點是對LSTM 的Many-to-One結構的理解,其次是對於多層LSTM layer的應用和理解。至於用TensorFlow來實現這個神經網絡的部分,主要是對LSTM 中的一些參數的設置要理解就行了,其他的都很簡單。