Sentiment Analysis using ANNdotNET


The October 2018 issue of MSDN magazine brings the article “Sentiment Analysis Using CNTK” written by James McCaffrey. I was wondering if I can implement this solution in ANNdotNET as Dr. McCaffrey written in the magazine. Indeed I have implemented complete solution in less than 5 minutes.

In this blog post I am going to walk you through this very good and well written MSDN article example. I am not going to repeat the text written in the MSDN article, so it is recommendation to read the article first, and back here and implement the example in ANNdotNET. Since the ANNdotNET is GUI tool, it is interesting to see all great visualizations during the model training and evaluation. Also the ANNdotNET provides complete binary model evaluation by providing the confusion matrix, ROC Curve, and other binary performance parameters, this example makes more interesting and valuable to read.

Whole example is implemented in five steps.

Step 1: Prepare files and folder structure

First we need to create several folders and files in order to create empty annproject. This manual creation of folders are necessary because ANNdotNET v1.0 has not option to create Empty project. This will be added in the next version.

So first, create the following set of hierarchically ordered folders:

  • SentimentAnalysis
    • MoveReview
      • data

The following figure shows this set of folder.

2018-10-15_21-08-04

Step 2: Download data sets used in the example.

Only thing we need from the MSDN article is train and test data sets. The data can be downloaded from the MSDN sample: Code_McCaffreyTestRun1018.zip. Once the zip file is downloaded unzip the sample, and copy files: imdb_sparse_train_50w.txt and indb_sparse_test_50w.txt to data folder as image above shows.

Step 3: Create MoviewReview.ann and LSTM-Net.mlconfig files

  • Open Notepad and create file with the following content:
project:|Name:MovieReview |Type:NoRawData |MLConfigs:LSTM-Net
data:|RawData:MovieReview_rawdata.txt
parser:|RowSeparator:rn |ColumnSeparator: ; |Header:0 |SkipLines:0

Save file in SentimenAnalysis folder as MovieReview.ann. The following picture shows saved annproject file on disk.

2018-10-15_21-29-24

Now open Notepad again, create a new empty file. The empty file is supposed to be  mlconfig file with the content shown below. Don’t worry about the content of the file, since all those details will be visible once we open it with ANNdotNET. If you want to know more about structure of the mlconfig file, please refer to this wiki page of the ANNdotNET project.

configid:msdn-oct-2018-issue-sentiment-analysis-article
metadata:|Column02:y;Category;Label;Random;0;1
features:|x 129892 1
labels:|y 2 0
network:|Layer:Embedding 50 0 0 None 0 0 |Layer:LSTM 25 25 0 TanH 1 1 |Layer:Dense 2 0 0 Softmax 0 0
learning:|Type:AdamLearner |LRate:0.01 |Momentum:0.85 |Loss:CrossEntropyWithSoftmax |Eval:ClassificationAccuracy |L1:0 |L2:0
training:|Type:Default |BatchSize:250 |Epochs:400 |Normalization:0 |RandomizeBatch:0 |SaveWhileTraining:0 |FullTrainingSetEval:1 |ProgressFrequency:1 |ContinueTraining:0 |TrainedModel:
paths:|Training:data\imdb_sparse_train_50w.txt |Validation:data\imdb_sparse_test_50w.txt |Test:data\imdb_sparse_test_50w.txt |TempModels:temp_models |Models:models |Result:LSTM-Net_result.csv |Logs:log<span id="mce_SELREST_end" style="overflow:hidden;line-height:0;"></span>

The file should be saved in the MovieReview folder with LSTM-Net.mlconfig file name. The next image shows where mlconfig file is stored.

2018-10-15_21-41-16

Step 4. Open annproject file with ANNdotNET GUI tool

Now we have setup everything in order to open and train sentiment analysis example with ANNdotNET. Since ANNdotNET implements MLEngine which is based on CNTK, data sets are compatible and can be read by the trainer. In order to get better result we have changed learning parameter a little bit. Instead of SGD we used AdamLearner.

In case you don’t have ANNdotNET tool installed on your machine, just go to release section and download the latest version. Or clone the GitHub repository and run it within the Visual Studio. All information about how to run ANNdotNET as standalone application or as the Visual Studio solution can be found at GitHub page https://github.com/bhrnjica/anndotnet.

After simple unzipping binaries of the ANNdotNET on your machine, run it by simply selecting anndotnet.wnd.exe file. Once the ANNdotNET is running, click the Open application command and select the MoveReview.ann file. In a second the application loads the project with corresponded mlconfig file. From the project explorer, click on LSTM-NET three item, and similar content as image below should be appeared.

2018-10-15_21-54-34

Everything we have written into mlconfig file are now shown in the Network settings tab page.

  1. Input layer with 129892 dimensions
  2. Output layer with 2 dimension (binary problem)
  3. Learning parameters:
    1. AdamLearner, with 0.01 lr and 0.85 momentum,
    2. Loss Function is CrossEntropywithSoftmax
    3. Evaluation function is ClassificationAccuracy
  4. NNetwork Designer shows typical LSTM recurrent network

Step 5. Training and Evaluation of the Example

Now that we reviewed the network settings, we can switch to the train tab page, and review the training parameters. Since we already setup training parameters in the mlconfig file, we don’t need to change anything.

Start training process by click on the Run application command. After some time we should see the following result:

2018-10-16_16-44-28

If we switch to Evaluation page we can perform some statistics analysis in order to evaluate if the model is good or not. Once the evaluation tab page is shown, click on Refresh button to evaluate the model against training and validation data stets.

2018-10-16_16-44-39

The left statistics are for the training dataset, and the left side is for the validation data set. As can be seen, the model perfectly predicted all data from the training data set, and about 70% of accuracy described the validation data set. Off cource, the model is not good as we expected for the production, but for this demonstration is good enough. There are also two buttons to show ROC curve, and other binary performance parameters, for both data sets, which the reader my taste.

That’s all needed in order to have complete Sentiment Analysis exemple setup and running. In case you want complete ANNdotNET project, it can be downloaded from here.

Advertisement

ANNdotNET v1.0 has been released


Half year ago, in this post announced the new open source project ANNdotNET, which was ANN part of the GPdotNET v4 – artificial intelligence tool. On that day I finished my new book about machine learning and genetic programming when also released the new version of GPdotNET V5.0 genetic programming tool, without ANN and other non GP related modules. Now my second big open source project achieved the first stable released.

ANNdotNET (http://github.com/bhrnjica/anndotnet) is deep learning tool on .NET platform, which has similar workflow as GPdotNET. Both projects share several modules, mostly for data preparation, and model evaluation since all that stuff are same.

anndotnet_gpdotnet.png

ANNdotNET is project which is more than GUI tool, since it contains CMD tool, which can be part of bigger cloud solution. There are several key concepts of the project which is worth to mention here:

1. Machine Learning Configuration mlconfig file

The ANNdotNET is based on so called machine learning configuration file, where everything about data, training and learning parameters, as well as neural network layers are store in the file so called mlconfig file. Along mlconfig file, there are several other file types generated during development of the ml solution. The mlconfig file can be shared between cloud services in order to prepare and transform data, train, evaluate or export ml models. If you want to see more information about files in ANNdotNET you can look at the wiki page of the project. Since the mlconfig file is independent of the tool, it can be executed with GUI or CMD tool, or any other custom tool, implemented on anndotnet API.

2. Machine Learning Project Explorer

In order to start developing ml solution with ANNdotNET, the first thing you do is create annproject file, by selecting New option from the Application command. Under annproject the user can create as many mlconfig files as he/she want. The annproject and related mlconfig files are presented in the ML Project Explorer, where the user can manage them as ordinary list items.

mlproject_explorer.png

3. ANNdotNET MLEngine – Machine Learning Engine

ANNdotNET introduces the ANNdotNET Machine Learning Engine (MLEngine) which is responsible for training and evaluation models defined in the mlconfig files.The ML Engine relies on Microsoft Cognitive Toolkit, CNTK open source library which is proved to be one of the best open source library for deep learning. Through all application’s components ML Engine exposed all great features of the CNTK e.g. GPU support for training and evaluation, different kind of learners, but also extends CNTK features with more Evaluation functions (RMSE, MSE, Classification Accuracy, Coefficient of Determination, etc.), Extended Mini-batch Sources, Trainer and Evaluation models.

anndotnet_mlengine

MLEngine is build on top of CNTK and .NET, with ability to provide backed component for any cloud/on-premise  ML solution.

4. Visual Neural Network Designer

ML Engine also contains the implementation of neural network layers which supposed to be high level CNTK API very similar as layer implementation in Keras and other python based deep learning APIs. With this implementation the ANNdotNET implements the Visual Neural Network Designer called ANNdotNET NNDesigner which allows the user to design neural network configuration of any size with any type of the layers. In the first release the following layers are implemented:

  • Normalization Layer – takes the numerical features and normalizes its values before getting to the network. More information can be found here.
  • Dense – classic neural network layer with activation function,
  • LSTM – LSTM layer with option for peephole and self-stabilization.
  • Embedding – Embedding layer.
  • Drop – drop layer.

More layer types will be added in the future release. More information about Visual Network Designer can be found on previous blog post.

5. Data Transformation

Along the ml related stuff, ANNdotNET implement set of components for data transformation from raw dataset into mlready datasets. The user doesn’t worry about complex CNTK file format, one-hot encoding, and other data and variable transformation e.g handling missing values, data normalization etc. Data transformation starts loading raw data file into ANNdotNET, then with set of GUI related options the data can be completely prepared to mlready dataset. There are set of short videos about how to quickly transform raw dataset into mlready dataset.

5. Model Evaluation, Saving Good Models & Retraining Trained Models

Once the model is trained, ANNdotNET provides basic evaluation tool for evaluating trained models. The MLEvaluator contains set of basic options in order to evaluate regression, binary or multi-class classification models. Without leaving ANNdotNET the user has ability to decide if the model is good or not by performing set of statistics measures agains model and related datasets (training, validation and test). Beside evaluation, ANNdotNET offers instantly evaluation during training phase, by providing an option for saving good models during training phase. On this way ANNdotNET has ability to select best trained model regardless of the number of iterations. Different strategy for selecting the best model among set of saved models will be implemented in the future. Also any previous trained models can be trained again from the last check point. This is important option in various scenario. For example to change some parameters and continue training. Also this option has ability to start training model on one machine or environment, and then continue with training on different machine or environment.

Summary

ANNdotNET – is an open source project for deep learning on .NET Platform. This is complete GUI solution for data preparation, training, evaluation and deployment ml models. ANNdotNET introduces the ANNdotNET Machine Learning Engine ( MLEngine) which is responsible for training and evaluation models defined in the mlconfig files. The MLEngine relies on Microsoft Cognitive Toolkit, CNTK open source library which is proved to be one of the best open source library for deep learning. Through all application’s components MLEngine exposed all great features of the CNTK e.g. GPU support for training and evaluation, different kind of learners. MLEngine also extends CNTK features with more evaluation functions (RMSE, MSE, Classification Accuracy, Coefficient of Determination, etc.), Extended Mini-batch Sources, Trainer and Evaluation models.
The process of creating, training, evaluating and exporting models is provided from the GUI Application and does not require knowledge for supported programming languages.

The ANNdotNET is ideal in several scenarios:

  • more focus on network development and training process using classic desktop approach, instead of focusing on coding,
  • less time spending on debugging source code, more focusing on different configuration and parameter variants,
  • ideal for engineers/users who are not familiar with programming languages,
  • in case the problem requires coding custom models, or training process, ANNdotNET CMD provides high level of API for such implementation,
  • all ml configurations developed with GUI tool,can be handled with CMD tool and vice versa.


In case you like this project star it on GitHub at http://github.com/bhrnjica/anndotnet. In case you want to use it in you academic paper, please cite it appropriate as specified at this link: https://doi.org/10.5281/zenodo.1461722