Icon

Example 3 - Building Sentiment Predictor - LSTM

Building a Sentiment Analysis Predictive Model - Deep Learning using an RNN

This workflow uses a Kaggle Dataset including 14K customer tweets towards six US airlines (https://www.kaggle.com/crowdflower/twitter-airline-sentiment). Contributors annotated the valence of the tweets as positive, negative and neutral. Once users are satisfied with the model evaluation, they should export (1) the Dictionary, (2) the Category to Number Model, and (3) the Trained Network for deployment in non-annotated data.

This workflow is tailored for Windows. If you run it on another system, you may have to (1) adapt the environment of the Conda Environment Propagation node and (2) make sure that the Keras Embedding Layer node has the right number of units, which depends on the native encoding of the system and is indicated in the CSV Reader node.

If you use this workflow, please cite:
F. Villaroel Ordenes & R. Silipo, “Machine learning for marketing on the KNIME Hub: The development of a live repository for marketing applications”, Journal of Business Research 137(1):393-410, DOI: 10.1016/j.jbusres.2021.08.036.

Annotations and comments
  • Building a Sentiment Analysis Predictive Model - LSTM model This workflow uses a Kaggle Dataset including 14K customer tweets towards six US airlines (https://www.kaggle.com/crowdflower/twitter-airline-sentiment). Contributors annotated the valence of the tweets as positive, negative and neutral. Once users are satisfied with the model evaluation, they should export (1) the Dictionary, (2) the Category to Number Model, and (3) the Trained Network for deployment in non-annotated data. This workflow is tailored for Windows. If you run it on another system, you may have to (1) adapt the environment of the Conda Environment Propagation node and (2) make sure that the Keras Embedding Layer node has the right number of units, which depends on the native encoding of the system and is indicated in the CSV Reader node.
  • 1. Define the Network Architecture The Keras Layer nodes define an LSTM based recurrent neural network. The network structure can be extended by adding more Keras Layer nodes.
  • 5. Evaluate Trained Network The metanode Extract Predictions uses the probabilities produced by the Keras Network Executor node and extracts the class with the highest probability.
  • 4. Train and Apply Network The Keras Network Learner node trains the defined network. In the configuration window you can define the input column(s), target column(s), the loss function, and the training parameters, e.g. number of epochs, batch size, and optimizer. The Keras Network Executor node applies the trained network to the input data. In the configuration window you can select the input column(s) and define the output by clicking on the "add output" button. In this worklflow the softmax output layer is defined as output. This means that the output are the probabilities for the three classes. The Conda Environment Propagation node ensures the existence of a Conda environment with all packages. Another option is to setup your Python integration to use a Conda environment with all packages as described here: https://docs.knime.com/2019-06/deep_learning_installation_guide/index.html#dl_python_setup
  • 2. Read annotated twitter dataset.
  • 3. Manipulate and Encode Data The metanode performs an index encoding to encode each word with an index. This blog post describes different encoding options https://www.knime.com/blog/text-encoding-a-review In general recurrent neural networks can handle sequences with different lengths. During training though, all sequences must have the same length. Therefore, the metanode adds zeros to the end of the sequences, so that all sequences have the same length. This approach is known as zero padding.
  • Workflow source: https://hub.knime.com/knime/spaces/Machine%20Learning%20and%20Marketing/latest/Sentiment%20Analysis~-z5F8gFJ9Fm1756a/ F. Villaroel Ordenes & R. Silipo, “Machine learning for marketing on the KNIME Hub: The development of a live repository for marketing applications”, Journal of Business Research 137(1):393-410, DOI: 10.1016/j.jbusres.2021.08.036.
  • Kaggle Dataset N=14640 Tweets from consumers to airlines
  • Encode each class with an index
  • Set up a conda environment: dl_sentiment_keras
  • Extract class index from column header
  • Extract column header for the class with the highest probility
  • Apply dictionary, keeping ids and counts alone
  • Split on space
  • Create an id for each token
  • Get the count for each token in the dataset
  • Create one input sequence per review, with the ids that are present per tweet
  • Softmax with 3 units Note: An appropriate output layer for a multiclass classification task is a softmax layer with as many units as classes.
  • Input: # word in dictionary + 1 128 units
  • Shape: ? Note: Using ? as input shape allows to handle different sequence lengths
  • Units for cell state: 256
  • Output: Softmax layer => Probability for the different classes
  • Loss function: Categorical Cross Entropy Epochs: 50
  • 80% training 20% testing

Nodes

Extensions

Links