JS doing deep learning, accidental discovery and introduction

Source: Internet
Author: User

JS doing deep learning, accidental discovery and introduction

Recently I first dabbled with node. js, and used it to develop a graduation design Web module, and then through the call System command in node execution Python file way to achieve deep learning function module docking, Python code intervention, make JS code seems very cumbersome, I said I love ES6 after JS and very annoying PYT Hon Code style, helpless, I wrote the completion of the Google has not officially released a deep learning framework based on JS, well, I have complained about this for a long time, but my "voice" seems to be Google "agreed" (funny), in my reply shortly after the end, April 2018 The official Tensorflow.js launched, referred to as TFJS.

Project Introduction

When I discovered this project this morning, the number of star on GitHub was up to 7k+, and it was only 3 months from the project launch! Can't help but sigh, this day finally still arrived, JS finally did not let deep learning ha haha.

The following is the details of the project, the address is: HTTPS://GITHUB.COM/TENSORFLOW/TFJS
Google TensorFlow Official Guide address is: https://js.tensorflow.org/tutorials/


Notice that the actual author of this project is called "CAISQ", and someone will ask who he is. Obviously a man of some kind. I found his real name on GitHub-"Cai Shanqing", looks like a Chinese, hurriedly on Baidu found his personal blog, do not see, a look at the urine, really drops is a big guy level of the figure, probably meaning is the current Google brain engineers, graduated from Tsinghua University, Bachelor of Engineering, Also have what Harvard-mit XXX Institute of Ph.D degree, I also can not understand, know is what Harvard-Massachusetts (fit? Dr., a terrible mess of the look, I admire the pleasantly surprised, for our Chinese people proud!

About his address: http://scai.io/

Installation and Getting Started

On GitHub's Readme page and on the TensorFlow website, there are "helloworld" cases to tell about the core ideas and the introduction of programming, Of course there is the way to install Tensorflow.js, let's take a look at the introductory case, I will directly copy the translation of the official tutorial, while making some easy to understand improvements.

First Step Installation

Since it is JavaScript, it can run both in the browser and in node, and the official given is the two operating environments. Interestingly, the official emphasis on WebGL interface and browser, which can be seen in the introduction of the official website, after I experienced the WebGL in the introduction.

Browser JS Installation:

Use the CDN installation to introduce the online JS address directly under the Script tab:

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"></script>

NodeJS NPM Installation:

Personal suggestion project Development directly new Package.json, then NPM install:

{    "name":"",    "description":"",    "version":"0.0.1",    "dependencies":{        "@tensorflow/tfjs":"latest"    }}

Note that this must be "@tensorflow/tfjs", which must be complete when introduced.

The official given is the global installation:

yarn add @tensorflow/tfjs
npm install @tensorflow/tfjs
Step two: "helloworld!"

We start from the basic example, the official home page gives the example is a linear regression case, which is also TensorFlow's classic introductory case, what is linear regression, in fact, the previous blog also mentioned, is a similar "undetermined factor" for the y=kx+b in the K and B process, This is also the core idea of deep learning. Note that the following cases are all run based on node. JS encoding.

Here are the official examples:

Import *  asTf from ' @tensorflow/tfjs ';//Define a model for linear regressionConstModel= TF.Sequential();Model.Add(TF.Layers.Dense({units: 1, Inputshape:[1]}));//Prepare the model for training:specify the loss and the optimizerModel.Compile({Loss: ' Meansquarederror ', Optimizer: ' SGD '});//Generate Some synthetic data for trainingConstXs= TF.tensor2d([1, 2, 3, 4],[4, 1]);ConstYs= TF.tensor2d([1, 3, 5, 7],[4, 1]);//Train the model using the dataModel.Fit(XS,YS). Then(()= {    //Use the model to does inference on a data point the model hasn ' t seen before    Model.predict(TF.tensor2d([5],[1, 1])).Print();});

This case developed a model in which there is a neural network, a neuron, each time a data is entered, a target data is obtained, and 4 training data and 4 training reference target data are given, respectively, 1,2,3,4 (x) and 1,3,5,7 (y), for y=kx+b. The last four lines of code enter a number x that the machine has never seen in the model, so that he can draw a target value y according to the law.

Here's a little something wrong, let's say:
First of all, node. JS has not officially supported Import,export keywords (I am also drunk, I downloaded the 10.X version of all), step back said that the case code import must be changed to require to introduce TFJS.
Then the case is unclear, not suitable for tensorflow beginners to understand, because the case does not list the complete training process, resulting in different results after the run. That is, the model inputs an unknown number x, and ultimately cannot give a reasonable result of Y.

I have improved the code, added the training process, and made the code simulate the linear function of y=2x+1. As follows:

ConstTf= require("@tensorflow/tfjs");ConstModel= TF.Sequential();//define network structure, number of layers, number of units, inputModel.Add(TF.Layers.Dense({units: 1, Inputshape:[1]}));//define OptimizerModel.Compile({Loss: ' Meansquarederror ', Optimizer: ' SGD '});//target: y=2x+1;ConstXs= TF.tensor2d([1,2,3,5],[4,1]);ConstYs= TF.tensor2d([3,5,7, One],[4,1]);//Use async because there is an asynchronous operation in the training, need to use await(Async()=>{    //Training 1000 times     for( LetI=0;I< +;I++){AwaitModel.Fit(XS,Ys;//Asynchronous operation waiting to be trained        Console.Log(' first${I}Times ');    }    Model.predict(TF.tensor2d([ -],[1, 1])).Print();})();

This makes it very clear that the code is running, and the result is:

I input to the model is four sets of training data, training 1000 times, after the end of the training, like model input 100, get 201, basically close to the target, success! (Remember, deep learning is a process that tends to be precise, and does not get standard results as humans do).

Next, I will continue to learn more about node. JS Technology and focus on its use in deep learning, and share my results through blog posts! (After all, I've been looking forward to tensorflow.js for a long time! )

JS doing deep learning, accidental discovery and introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.