Icon

Building Sentiment Predictor - Deep Learning

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.

Building a Sentiment Analysis Predictive Model - Deep Learning using an Recurrent Neural Network (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.
2. Read annotated twitter dataset.
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.
3. Manipulate and Encode DataThe 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-reviewIn 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.
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
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.
Save model
PMML Writer
Save network
Keras Network Writer
Save dictionary
Table Writer
Kaggle Dataset N=14640 Tweets from consumers to airlines
CSV Reader
Set up a conda environment: dl_sentiment_keras
Conda Environment Propagation
Input: # word in dictionary + 1 128 units
Keras Embedding Layer
Scorer
Units for cellstate: 256
Keras LSTM Layer
Create Collection Column
Softmax with 3 unitsNote: An appropriate output layer for amulticlass classification task is a softmax layer with as many unitsas classes.
Keras Dense Layer
Encode each class with an index
Category to Number
80% training 20% testing
Partitioning
Shape: ?Note: Using ? as inputshape allows to handle different sequence lengths
Keras Input Layer
Class with highest probability
Extract Prediction
Output: Softmax layer=> Probability for the different classes
Keras Network Executor
Index encoding and zero padding
Loss function:Categorical CrossEntropyEpochs: 50
Keras Network Learner

Nodes

Extensions

Links