Tuesday, December 29, 2020

Building a tensorflow.js application with high chart visualization for service request analysis:


Introduction: TensorFlow is a machine learning framework for JavaScript applications that can use both tensors/vectors and scalars. It helps us build models that can be directly used in the browser or in the node.js server. We use this framework for building an application that can predict request resolution time. 

Description: This JavaScript application uses data from a csv that has categorizations of service requests, with their attributes and the resolution time.  The attributes of each request include a category_id, a pseudo parameter attribute and the execution time and these helps define the tensor. This data is sampled from an inventory and it is also simplified to keep this analysis simple. 

As with any ML learning example, the data is split into 70% training set and 30% test set. There is no order to the data and the split is taken over a random set.  

The model chosen is a Sequential model. This model is appropriate for a plain stack of layers where each layer has exactly one input tensor and one output tensor.   Layers act in succession taking output of one as the input of another. Thus, this model is suitable for one input and one output and where the layers are distinct and not sharing any input.  

Another model that can be used is one that is more generic and loads an acyclic graph.  A sequential model only uses a linear stack of layers. The input or output of the layers must be specified. A convolutional layer creates convolution kernel which is a small matrix of weights. The kernel slides over the input layer and performs an element wise multiplication with the part of the input the kernel is on. The resulting scalar forms the element of a new kernel. This resulting kernel is called the convolution kernel. The graph model and sequential model can both be used with a tensor or a scalar. 

TensorFlow makes it easy to construct this model using an API keras.Sequential. It can only present the hidden weight matrix after the model is executed. In this case, the model must be run before the weights are available.  The output of each layer can be printed using the summary() method.  

With the model and training/test sets defined, it is now as easy to evaluate the model and run the inference.  The model can also be saved and restored. It executed faster when there is GPU added to the compute. 

The features are available with the feature_extractor. It is evaluated on the training set using model.compile() and model.fit(). The model can then be called on a test input. Additionally, if a specific layer was to be evaluated, we can call just that layer on the test input. 

When the model is trained, it can be done in batches of predefined size. The number of passes of the entire training dataset called epochs can also be set up front. It is helpful to visualize the training with the help of a high chart that updates the chart with the loss after each epoch 

When the model is tested, it predicts the resolution time for the given attributes of category_id and parameter attribute 

Creating the highchart is a simple javascript call to their api: 

Highcharts.chart('container', { 

    chart: { 

        type: 'bar’ 

    }, 

    title: { 

        text: 'relief time' 

    }, 

    subtitle: { 

        text: 'Source: <a href="">Response time</a>' 

    }, 

    xAxis: { 

        title: { 

            text: "Time (seconds)" 

        } 

    }, 

    yAxis: { 

        min: 0, 

        title: { 

            text: 'Service requests', 

            align: 'high' 

        }, 

        labels: { 

            overflow: 'justify' 

        } 

    }, 

    tooltip: { 

        valueSuffix: ' time’ 

    }, 

    plotOptions: { 

        bar: { 

            dataLabels: { 

                enabled: true 

            } 

        } 

    }, 

    legend: { 

        layout: 'horizontal', 

        align: 'top', 

        verticalAlign: 'right', 

        x: -40, 

        y: 80, 

        floating: true, 

        borderWidth: 1, 

        backgroundColor: 

            Highcharts.defaultOptions.legend.backgroundColor || '#FFFFFF', 

        shadow: true 

    }, 

    credits: { 

        enabled: false 

    }, 

    series: [{ 

        name: 'Service requests', 

        data: [13, 19, 17, 123, 116, 14, 13, 18, 12, 17, 19, 110, 112, 16, 15, 11, 17] 

    }] 

});  

Conclusion: Tensorflow.js is becoming a standard for implementing machine learning models. Its usage is fairly simple but the choice of model and the preparation of data take significant more time than setting it up, evaluating and using it. 

 

No comments:

Post a Comment