A blog for computer science passionates.

Wednesday, 22 August 2018

Hello Friends!!!

Now a day one of the most popular thing in the field of IT is Machine Learning and deep learning, specially for research  areas and for researcher in this field. As per the usage of internet groves by leaps and bounds, all the researchers, developers find a way how to manage machine learning and deep learning online.
So, here we have a tensorflow.js
It is an open source WebGL accelerated JavaScript library file for Machine Learning and deep learning. To  build neural network online to run on your browser tensorflow.js is used. It provides low - level building blocks as well as high level API for Machine Learning. It is also inspired by Keras library to construct neural network or deep learning model.

 Tensorflow.js works with tensor. now we have that question what is tensor?

In mathematical terms, tensor means a generalized matrix, it can be 1 - D or 3 - D or higher dimensional matrix.
Here, the central unit of data in tensorflow.js is called a tensor, set of numerical elements shaped in an array one or more dimensional. in tensorflow.js we have shape attribute to define array shape or dimension.

Now Lets' start tensorflow.js with simple example with brief summery,
To start with tensorflow.js create your webpage. i.e. create a simple HTML file.

<html>
<head>
<!-- first load tensorflow.js from CDN-->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.12.0"> </script>

    <script>
      // Define a model and add layers in it. I have only one input and one output so I add a layer with single input and output to a model.
      const model = tf.sequential();      model.add(tf.layers.dense({units: 1, inputShape: [1]}));

      // Prepare the model for training: Specify the loss and the optimizer.
      model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});

      // Generate some synthetic data for training. My Input as well as both are of same shape
      const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);     //For input Here I define a tensor with [4,1] for input
      const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);    //For output I will get the same shape of output.


      // Train the model using the data. here I give epochs i.e. loop through data 10 times.

      model.fit(xs, ys, {epochs: 10}).then(() => {
       
        // Open the browser devtools to see the output
        model.predict(tf.tensor2d([5], [1, 1])).print();          //Here I specify [1,1] tensor with value [5] i.e. containing value is [5] it will print in web console.
      });
    </script>
</head>
<body></body>
</html>

So, This is a very simple model we create with brief summery.  Hope you enjoy....

Happy Coding and have fun with tensorflow.js!!!!