Search code examples
pythonneural-networknlplstmsentiment-analysis

LSTM for sentiment analysis


I saw this tensorflow model which is used for telling if text is positive or negative, and I don't fully understand it. I know that LSTM saves the words and predict the next words based on the previous words, but how does this help network to distinguish emotions of the text?

def tensorflow_based_model(): 
    inputs = Input(name='inputs',shape=[max_len])
    layer = Embedding(2000,50,input_length=max_len)(inputs) 
    layer = LSTM(64)(layer)
    layer = Dense(256,name='FC1')(layer) 
    layer = Activation('relu')(layer) 
    layer = Dropout(0.5)(layer)
    layer = Dense(1,name='out_layer')(layer) 
    layer = Activation('sigmoid')(layer) 
    model = Model(inputs=inputs,outputs=layer) 
    return model 

Solution

  • Altough LSTM can be used in the text generation, the main use of LSTM (or any recurrent neural network layer) is to understand sequences. You can find more information in this blog posts:

    The Unreasonable Effectiveness of Recurrent Neural Networks

    Understanding LSTM Networks

    In the case of sentiment analysis, Instead of helping to generate new word, LSTM helps us understand what was said. It basicaly reads over the string and keeps some important information about the previously said words.