 In the recent times, Deep Learning which is a subset of Artificial Intelligence is the cutting edge technology. When it comes to the ecosystem undoubtedly TensorFlow is leading with 100K starts in Github.

I have started exploring Machine Learning with Text as well as Image-based recommendation Engines and understood how the Tech giants like Amazon, Flipkart will be doing it to show us the recommendations when we search for a product.

Recently I have started exploring TensorFlow.js which does not require any kind of complex environment setup, installing the third party dependencies and so on. It directly runs in the browser which makes it portable. This is the one which has attracted me to start digging into TensorFlow.js since I can run my applications in Mobile Device as well.

In this article, I am going to explain how can we calculate the coefficients of a polynomial function.

• Generating the data to plot the points in the coordinate system.
• Building the model.
• Training the model to calculate the coefficients.

A Tensor is a central unit of data in tensorflow.js which can be used to create data from one to n-dimension.

suppose assume that our polynomial function is :

we need to calculate the coefficients a,b,c & d which will match our data.

Generating data:

TensorFlow.js provides API to create random data which can be used to train our model. using the below API you can create 100 x-coordinates which can be used to calculate actual y-coordinates by substituting in f(x).

we have generated our data. Now Let us build the model which has to be trained using our data.

Building the model:

The Advantage of using tidy API is that once the function that is passed as an argument gets executed it will dispose of all the intermediate tensors that were created. General convention that people say to write all the operations inside tidy is because of this reason.

Loss Function:

Before training our model, we need to define the loss function. Since it is a cubic polynomial which is non-linear the suitable loss function should be calculating the Euclidean distance between the network output & expected output. So the most suitable loss function which can perform this operation is undoubtedly Mean Square error.