Upoznajte TensorFlow.js za 7 minuta

I naučite kako možete pokretati ML / DL modele izravno u pregledniku

Sve veći broj programera koristi TensorFlow u svojim projektima strojnog učenja. U ožujku ove godine Googleov tim TensorFlow najavio je dolazak toliko očekivanog JavaScript okvira, TensorFlow.js (koji se prethodno zvao DeepLearn.js).

Sada programeri mogu graditi lagane modele i pokretati ih u pregledniku pomoću JavaScript-a. Razumijemo koja je bila potreba za razvojem ovog okvira.

Povijest

Prije odlaska na TensorFlow.js, želio bih započeti s TensorFlowom.

TensorFlow je razvijen 2011. na Googleu kao njihova prikladna biblioteka za strojno učenje / aplikacije za duboko učenje na Googleu. Ova je knjižnica otvorena 2015. godine pod licencom Apache.

TensorFlow je ugrađen u C ++, što omogućuje izvršavanje koda na vrlo niskoj razini. TensorFlow ima vezove za različite jezike kao što su Python, R i Java. To omogućuje upotrebu TensorFlow-a na tim jezicima.

Dakle, očito je pitanje: što je s JavaScriptom?

Uobičajeno se u JavaScript-u ML / DL provodio pomoću API-ja. API je napravljen pomoću nekog okvira, a model je postavljen na poslužitelju. Klijent je poslao zahtjev koristeći JavaScript za dobivanje rezultata s poslužitelja.

2017. godine pojavio se projekt pod nazivom Deeplearn.js, čiji je cilj bio omogućiti ML / DL u JavaScript-u, bez API gnjavaže.

Ali bilo je pitanja o brzini. Bilo je vrlo dobro poznato da JavaScript kod ne može raditi na GPU-u. Da bi se riješio ovaj problem, uveden je WebGL. Ovo je sučelje preglednika za OpenGL. WebGL je omogućio izvršavanje JavaScript koda na GPU-u.

U ožujku 2018. tim DeepLearn.js spojen je u TensorFlow tim na Googleu i preimenovan je u TensorFlow.js.

Za detalje pogledajte video u nastavku:

TensorFlow.js

Tensorflow.js nudi dvije stvari:

  • CoreAPI, koji se bavi kodom niske razine
  • LayerAPI izgrađen je preko CoreAPI-a i olakšava nam život povećavajući razinu apstrakcije.

Početak rada

Postoje dva glavna načina za dobivanje TensorFlow.js u vašem projektu:

1. putem Taga

Dodajte sljedeći kod u HTML datoteku:

 Hello 

2. putem NPM-a

Dodajte TensorFlow.js u svoj projekt pomoću pređe ili npm.

yarn add @tensorflow/tfjs
npm install @tensorflow/tfjs

U vašoj glavnoj js datoteci:

import * as tf from '@tensorflow/tfjs';

CoreAPI

1. Tenzori

Pa, što je tenzor?

  • Skalar je jedan broj. Na primjer, x = 1
  • Vektor je niz brojeva. Na primjer, x = [1,2]
  • Matrica je 2-D niz

    ([[1, 2],

    [3, 4],

    [5, 6]])

  • Tenzor je n- dimenzionalni niz s n > 2

TensorFlow.js ima korisne funkcije za uobičajene slučajeve poput skalarnih, 1D, 2D, 3D i 4D tenzora, kao i brojne funkcije za inicijalizaciju tenzora na načine korisne za strojno učenje.

Primjeri koda

tf.tensor ():

// Pass an array of values to create a vector.tf.tensor([1, 2, 3, 4]).print();

tf.scalar ():

tf.scalar(3.14).print();

I tako dalje…

Pogledajte donji videozapis da biste dobili dubok uvid u tenzore na TensorFlow.js:

2. Varijable i operacije

Tenzori su nepromjenjive strukture podataka. To znači da se njihove vrijednosti ne mogu mijenjati nakon što se postave.

Međutim,tf.variable()je uveden u TensorFlow.js. Stvarni slučaj upotrebe tf.variable()jest kada trebamo često mijenjati podatke, primjerice prilikom podešavanja težina modela u Strojnom učenju.

Uzorak koda:

const x = tf.variable(tf.tensor([1, 2, 3]));x.assign(tf.tensor([4, 5, 6]));x.print();

Operacije

There are various operations in TensorFlow.js. In order to perform mathematical computation on Tensors, we use operations. Tensors are immutable, so all operations always return new Tensors and never modify input Tensors. So tf.variable() can be used in order to save memory.

Let’s look into some operations:

tf.add() — Adds two tf.Tensors element-wise

const a = tf.tensor1d([1, 2, 3, 4]);const b = tf.tensor1d([10, 20, 30, 40]);a.add(b).print(); // or tf.add(a, b)

There are many operations in TensorFlow.js. You can check the documentation for other operations. I will demonstrate one more operation here: tf.matmul()

tf.matmul() — Computes the dot product of two matrices, A * B.

const a = tf.tensor2d([1, 2], [1, 2]);const b = tf.tensor2d([1, 2, 3, 4], [2, 2]);
a.matMul(b).print(); // or tf.matMul(a, b)

Watch the below video for deep insight into Variable and Operations:

3. Memory Management

Memory management is the key in Machine Learning/Deep Learning tasks, because they are generally computationally expensive.

TensorFlow.js provides two major ways to manage memory:

  1. tf.dispose()
  2. tf.tidy()

They both typically do the same thing, but they do it in different ways.

tf.tidy()

This executes the provided function fn and after it is executed, cleans up all intermediate tensors allocated by fn except those returned by fn.

tf.tidy() helps avoid memory leaks. In general, it wraps calls to operations in tf.tidy() for automatic memory cleanup.

Code example:

const y = tf.tidy(() => { // aa, b, and two will be cleaned up when the tidy ends. const two= tf.scalar(2); const aa = tf.scalar(2); const b = aa.square(); console.log('numTensors (in tidy): ' + tf.memory().numTensors); // The value returned inside the tidy function will return // through the tidy, in this case to the variable y. return b.add(two);});console.log('numTensors (outside tidy): ' + tf.memory().numTensors);y.print();

tf.dispose()

Disposes any tf.Tensors found within the mentioned object.

Code example:

const two= tf.scalar(2);
two.dispose()

LayersAPI

Layers are the primary building block for constructing a ML/DL Model. Each layer will typically perform some computation to transform its input to its output. Under the hood, every layer uses the CoreAPI of Tensorflow.js.

Layers will automatically take care of creating and initializing the various internal variables/weights they need to function. So, basically it makes life easier by increasing the level of abstraction.

We will make a simple example feed forward network using the LayerAPI. The Feed Forward network we will build is as below:

Code:

Index.html

Tensorflow JS Demo

main.js

const model = tf.sequential();
//config for layerconst config_hidden = { inputShape:[3], activation:'sigmoid', units:4}const config_output={ units:2, activation:'sigmoid'}
//defining the hidden and output layerconst hidden = tf.layers.dense(config_hidden);const output = tf.layers.dense(config_output);
//adding layers to modelmodel.add(hidden);model.add(output);
//define an optimizerconst optimize=tf.train.sgd(0.1);
//config for modelconst config={optimizer:optimize,loss:'meanSquaredError'}
//compiling the modelmodel.compile(config);
console.log('Model Successfully Compiled');
//Dummy training dataconst x_train = tf.tensor([ [0.1,0.5,0.1], [0.9,0.3,0.4], [0.4,0.5,0.5], [0.7,0.1,0.9]])
//Dummy training labelsconst y_train = tf.tensor([ [0.2,0.8], [0.9,0.10], [0.4,0.6], [0.5,0.5]])
//Dummy testing dataconst x_test = tf.tensor([ [0.9,0.1,0.5]])
train_data().then(function(){ console.log('Training is Complete'); console.log('Predictions :'); model.predict(x_test).print();})
async function train_data(){ for(let i=0;i<10;i++){ const res = await model.fit(x_train,y_train,epoch=1000,batch_size=10); console.log(res.history.loss[0]); }}

Output:

Watch the below videos for deep insight and code explanation:

I understand that this is a small overview on the Tensorflow.js Library. I feel this can be a starting point before you read the documentation and go through some real world applications.

I will be posting real world examples using TensorFlow.js as below:

More Real world examples coming soon…Stay Tuned…

My take on this

This is excellent for coders who are familiar with JavaScript and are trying to find their way in the ML/DL world!

It makes things a lot simpler for people coming from a non-ML/DL background, but who are looking to understand this field. The use cases for this are many, and I personally think it’s something we need at the moment.

In my next article and video, I will talk about ML5 which is built over TensorFlow.js. ML5 is built at New York University and is under active development.

What do you think about TensorFlow.js? Let me know in the comments section below. If you like this article, you would also like my Videos on Youtube.

If you liked my article, please click the ? below And follow me on Medium & :

If you have any questions, please let me know in a comment below or Twitter.