Wednesday, December 30, 2020

Building a k-nearest neighbors tensorflow.js application:

 Introduction: TensorFlow is a machine learning framework for JavaScript applications. 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 find similar requests so that they might be used for prediction. 

Description: The JavaScript application uses data from a CSV that has categorizations of requests and the resolution time. The attributes of each request include a category_id, a pseudo parameter attribute, and the execution time. The data used in this sample has 1200 records but the attributes are minimum to keep the application 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 KNN model. This model is appropriate for finding the k nearest neighbors to those it was previously shown. The default number of neighbors is 3. This model is suitable for one input and one output and where the tensors are distinct and not affecting each other. The output consists of a label with the most confidence which is a statistical parameter based on the support for the label, a class index, and a score set for the confidence associated with each label. 

TensorFlow makes it easy to construct this model using an API. It can only present the output 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 is executed faster when there is GPU added to the computing. 

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 upfront. 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 

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 takes significantly more time than setting it up, evaluating, and using it. 

 

No comments:

Post a Comment