Building a Model with Neural Networks Tutorial

In this tutorial, we will be designing a machine learning classification model with the help of Artificial Neural Networks.

The tutorial consists of the following sections:

  • Setup - A quick guide on how to setup the System
  • Overview - A quick guide on how to use the Neural Networks Library
  • Methods - A brief description of the methods within the Library
  • Assets - A list of all the pre-packaged assets for the IPM
  • Usage - A deeper understanding of how to build a classification model

Setup

  • Create an account here if you have not done it already.
  • Click on Add System -> Advanced and copy the following link in the box
    https://github.com/ClearBlade/Neural-Networks-Library
  • Click Create to create a new system with the Neural Networks Library.

Overview

The overview provides a basic understanding of the architecture and functionality of the library :

Architecture

  1. The Neural Networks Library is designed to provide a three-layered Neural Network Architecture.
  2. Different hyperparameters can be provided to train the model which include:
    • Number of neurons in the Input layer
    • Number of neurons in the Hidden layer
    • Validation Split value
    • Number of epochs for which the model has to be trained
    • Batch size
  3. Tuning these hyper-parameters will change the accuracy accordingly and you can choose the model with the best accuracy for making predictions.

Functionality

  1. The Neural Networks library essentially allows you to:
    • Select a Training Dataset
    • Design your own classification model
    • Train the model
    • Save the model
    • Load the model
    • Test the model
  2. It provides various mathematical functions to convert the data into matrix representation so that further training computations can be simplified.

Methods

This section provides detailed information about the different functions provided by the Library for building the model.

Methods Use Required Parameters Optional Parameters
getData(req,dataName,inputs) Takes a collection name and number of input neurons and returns a 2-D array req : Request, dataName : Name of Collection in which training data is placed, inputs : Number of neurons in the first input layer. (Must be equal to the number of features)
validationSplit(data,labels,validationPercent) Divides your dataset into train dataset and validation dataset with the specified validation percentage data : Training Data, labels : Training Labels validationPercent : Percentage by which the data should be split for training and validation. (Default = 5%)
NeuralNetwork(input,hidden,outputs) Initializes a New Neural Network object with the specified number of neurons in the input, hidden and output layer respectively outputs : Number of neurons in the output layer, input : Number of neurons in the input layer hidden : Number of neurons in the hidden layer. (Default = 16)
fit(trainX,trainY,testX,testY,epochs) Trains your model with training data, training labels, validation data, validation labels and epochs respectively trainX : Training Data, trainY : Training Labels, testX : Validation Data, testY : Validation Labels epochs : Number of epochs (Default = 4)
predict(testData) Takes test data and outputs a prediction which is essentially the probability values of the data belonging to the particular class testData : Data to be tested
saveModel(resp,colName,modelName) Saves the model in a collection resp : Response, colName : Collection name where the model is going to be saved, modelName : Model Name
loadModel(resp,colName,modelName) Loads the model from a collection resp : Response,colName : Collection name from where the model is going to be loaded, modelName : Model Name

Assets

You should have all the following pre-packaged assets for the IPM available in your new system:

Assets Name Description
Code Service nn_test An example that shows how to use the Neural Networks Library
Library nn Neural Networks Library
Collections Machine1 Collection which stores the Sensor Data
nn_models Collection which stores the model Architecture

Usage

The data stored in the Machine1 Collection is a list of 100 readings taken from 3 different sensors viz. Power Sensor, Temperature Sensor and a Vibration Sensor. The Collection has a maintenance_required column which provides information about whether a maintainance was scheduled for a particular reading. We train this model with maintenance_required as the training label and test the model by giving new power, temperature and vibration values to predict whether a maintenance is required or not.

  • Initialize all the hyper-parameters. For example:
    var inputs = 3;  <- This value should be equal to the Features
    var batch = 100;
  • Load the training data from a collection using the getData method.
    var data = getData(req, collectionName, inputs)
    var data_x = data[0]        Here, data[0] : Features
    var data_y = data[1]              data[1] : One Hot encodings
    var outputs = data[2]             data[2] : Label Count
  • Slice the data based on the batch size so that the training data can be loaded in batches during each epoch. For example:
    data_x = data_x.slice(data_x.length - batch)
    data_y = data_y.slice(data_x.length - batch)
  • Run the validationSplit function to split the training and validation datasets:
    var datasets = validationSplit(data_x, data_y, validation_split)
    var train_x = datasets[0]           Here datasets[0] : Train Features
    var train_y = datasets[1]                datasets[1] : Train Labels
    var test_x = datasets[2]                 datasets[2] : Validation Features
    var test_y = datasets[3]                 datasets[3] : Validation Labels
  • Initialize a Neural Network model:
    var nn = new NeuralNetwork(inputs, hidden, outputs);
  • Start the Training Process:
    var trained_model = nn.fit(train_x, train_y, test_x, test_y, epochs)
  • Save the Model:
   saveModel(resp, saveCollectionName, nn)
  • Load the Model:
    loadModel(resp, saveCollectionName, nn)
  • Predict using the Model:
    var prediction = nn.predict([test_data])
    OUTPUT :
    [
        0.9610983899129381,
        0.03584318389661973
    ]

This can be interpreted as : From the test data provided, the machine is not scheduled for maintenance since the probability of not scheduling a maintenance is 96%.