<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: ddif06</title>
    <description>The latest articles on DEV Community by ddif06 (@ddif06).</description>
    <link>https://dev.to/ddif06</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F342235%2F97871879-3a28-46e6-81fe-acf9f71ac337.jpeg</url>
      <title>DEV Community: ddif06</title>
      <link>https://dev.to/ddif06</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ddif06"/>
    <language>en</language>
    <item>
      <title>How to run Tensorflow.js on a serverless platform : deploying models</title>
      <dc:creator>ddif06</dc:creator>
      <pubDate>Wed, 25 Mar 2020 12:10:13 +0000</pubDate>
      <link>https://dev.to/oxygenit/how-to-run-tensorflow-js-on-a-serverless-platform-deploying-models-3b1i</link>
      <guid>https://dev.to/oxygenit/how-to-run-tensorflow-js-on-a-serverless-platform-deploying-models-3b1i</guid>
      <description>&lt;p&gt;This is the last part of a 3 articles serie.&lt;br&gt;
In the &lt;a href="https://dev.to/warpjs/introduction-to-tensorflow-js-the-ai-stack-for-javascript-2fb0"&gt;first part&lt;/a&gt;, we introduced neural networks and TensorFlow framework basics.&lt;br&gt;
In the &lt;a href=""&gt;second part&lt;/a&gt;, we explained how to convert existing models from Python to &lt;a href="https://www.tensorflow.org/js" rel="noopener noreferrer"&gt;TensorFlow.js&lt;/a&gt;&lt;br&gt;
Finally, we present today, through an example, how to use an online TensorFlow.js model and deploy it rapidly using our &lt;a href="https://scaledynamics.io/warpjs" rel="noopener noreferrer"&gt;WarpJS&lt;/a&gt; JavaScript Serverless Function-as-a-Service (FaaS).&lt;/p&gt;
&lt;h1&gt;
  
  
  Using an online model with TensorFlow.js
&lt;/h1&gt;

&lt;p&gt;Many public models can be retrieved from web databases. TensorFlow Hub, in particular, hosts models usable with TensorFlow.js, which are generally also available as npm packages.&lt;/p&gt;

&lt;p&gt;We'll use the "toxicity" pre-trained model in the next sections as an example.&lt;/p&gt;

&lt;p&gt;The toxicity model detects whether text contains toxic content such as threatening language, insults, obscenities, identity-based hate, or sexually explicit language. The model was trained on the civil comments' dataset: &lt;a href="https://figshare.com/articles/data_json/7376747" rel="noopener noreferrer"&gt;https://figshare.com/articles/data_json/7376747&lt;/a&gt; which contains ~2 million comments labeled for toxicity. The model is built on top of the Universal Sentence Encoder (&lt;a href="https://arxiv.org/pdf/1803.11175.pdf" rel="noopener noreferrer"&gt;Cer et al., 2018&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Browser-based usage:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The model can be directly loaded for use in JavaScript at:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://cdn.jsdelivr.net/npm/@tensorflow-models/toxicity" rel="noopener noreferrer"&gt;https://cdn.jsdelivr.net/npm/@tensorflow-models/toxicity&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the html, add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://cdn.jsdelivr.net/npm/@tensorflow-models/toxicity"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, in the JS code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// sets the minimum prediction confidence&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;threshold&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.9&lt;/span&gt;
&lt;span class="c1"&gt;// load and init the model&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;toxicity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="c1"&gt;// apply an inference&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;predictions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;classify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inputText&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Node-based usage:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Toxicity is also available as a NPM module for Node.js (package that actually loads the model from the storage link above):&lt;br&gt;
&lt;code&gt;$ npm install @tensorflow-models/toxicity&lt;/code&gt;&lt;br&gt;
Then, in the JS code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;toxicity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@tensorflow-models/toxicity&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// sets the minimum prediction confidence&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;threshold&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.9&lt;/span&gt;         &lt;span class="c1"&gt;// sets the minimum prediction confidence&lt;/span&gt;
&lt;span class="c1"&gt;// load and init the model&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;toxicity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="c1"&gt;// apply an inference&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;predictions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;classify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inputText&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Deploying a model with WarpJS
&lt;/h1&gt;

&lt;p&gt;As discussed before, inference on big data sets in the browser comes rapidly short in terms of performance due to model and data loading time &lt;br&gt;
as well as computing capabilities (even with accelerated backends).&lt;/p&gt;

&lt;p&gt;Node.js allows to push further the performance limit by deploying on a high performance GPU engine and in the network neighborhood of the dataset, but the user will face complexity when trying to address distributed processing for the next performance step.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://scaledynamics.io/warpjs/" rel="noopener noreferrer"&gt;WarpJS&lt;/a&gt; JavaScript FaaS enables easy serverless process distribution with very little development effort.&lt;/p&gt;

&lt;p&gt;Example: toxicity model serverless deployment&lt;/p&gt;

&lt;p&gt;WarpJS installation guidelines can be found here: &lt;a href="https://warpjs.dev/docs/getting-started" rel="noopener noreferrer"&gt;Getting started with WarpJS&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can request an account on WarpJS &lt;a href="https://scaledynamics.io/warpjs" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This &lt;a href="https://medium.com/warpjs/implementing-a-serverless-api-proxy-in-10-minutes-5730f039a12c" rel="noopener noreferrer"&gt;article&lt;/a&gt; also provides a good tutorial on all steps to operate WarpJS.&lt;/p&gt;

&lt;p&gt;In our WarpJS serverless operation, the browser acts as the primary &lt;br&gt;
input/output interface, through an index.html file.&lt;/p&gt;

&lt;p&gt;It contains a text box to submit the input text to be analyzed and a "classify" button triggering the inference process.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
...
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
   ...
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;TensorFlow.js toxicity demo with WarpJS&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"form"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"classifyNewTextInput"&lt;/span&gt; &lt;span class="na"&gt;placeholder=&lt;/span&gt;&lt;span class="s"&gt;"i.e. 'you suck'"&lt;/span&gt; 
&lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;button&amp;gt;&lt;/span&gt;Classify&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"result"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;WarpJS is a function-as-a-service platform for JavaScript. Instead of creating HTTP endpoints and use HTTP calls to do a remote inference, we just have to build a client for this inference function, deploy it on its FaaS, import it in the main application (via import statement in index.js or via script tag in html) and then call it like any JavaScript function.&lt;/p&gt;

&lt;p&gt;index.js (code to be deployed on WarpJS FaaS):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Server initialization&lt;/span&gt;
&lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;tensorflow&lt;/span&gt;&lt;span class="sr"&gt;/tfjs’&lt;/span&gt;&lt;span class="err"&gt;)
&lt;/span&gt;&lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;tensorflow&lt;/span&gt;&lt;span class="sr"&gt;/tfjs-node’&lt;/span&gt;&lt;span class="err"&gt;)
&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;toxicity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;tensorflow&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;models&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;toxicity&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// The minimum prediction confidence&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;threshold&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.9&lt;/span&gt;
&lt;span class="c1"&gt;// Load the model&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;modelLoaded&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
&lt;span class="nx"&gt;toxicity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tsModel&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;tsModel&lt;/span&gt;
   &lt;span class="nx"&gt;modelLoaded&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="c1"&gt;// Force waiting for the async TensorFlow model load.&lt;/span&gt;
&lt;span class="c1"&gt;// The “deasync” lib turns async function into sync via JS wrapper of Node event loop.&lt;/span&gt;
&lt;span class="c1"&gt;// The “loopWhile” function will wait for the condition resolution to continue.&lt;/span&gt;
&lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;deasync&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;loopWhile&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;modelLoaded&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// Prediction function&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;classify&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nx"&gt;inputs&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;// predict with tensorflow model&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;predictions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;classify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inputs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="c1"&gt;// check toxicity results&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;toxic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;predictions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;some&lt;/span&gt;&lt;span class="p"&gt;(({&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
                 &lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;toxic&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;classify&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;index.js (main, browser-based application):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
* Copyright 2020 ScaleDynamics SAS. All rights reserved.
* Licensed under the MIT license.
*/&lt;/span&gt;
&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt; &lt;span class="nx"&gt;strict&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;
&lt;span class="c1"&gt;// import WarpJS engine module&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;engine&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;warpjs&lt;/span&gt;&lt;span class="sr"&gt;/engine&lt;/span&gt;&lt;span class="err"&gt;’
&lt;/span&gt;&lt;span class="c1"&gt;// import deployed inference&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;classify&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;warp-server&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// on submit form&lt;/span&gt;
&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;submit&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
   &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Remote&lt;/span&gt; &lt;span class="nx"&gt;inference&lt;/span&gt; &lt;span class="nx"&gt;running&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h2&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;’
&lt;/span&gt;   &lt;span class="c1"&gt;// scan textbox&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;classifyNewTextInput&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;
   &lt;span class="c1"&gt;// invoke inference&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;toxic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;classify&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
   &lt;span class="c1"&gt;// render result&lt;/span&gt;
   &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;toxic&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&amp;lt;h2 style=”color:red”&amp;gt;Your sentence is TOXIC :(&amp;lt;/h2&amp;gt; &amp;lt;img src=”/img/Pdown.png” alt=””&amp;gt;`&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`
     &amp;lt;h2 style=”color:green”&amp;gt;Your sentence is NON TOXIC :)&amp;lt;/h2&amp;gt;
     &amp;lt;img src=”/img/Pup.png” alt=””&amp;gt;`&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Deploying to the WarpJS FaaS is straightforward, just use "npm run deploy" to get the url of the deployed site and start playing with TensorFlow.js.&lt;/p&gt;

&lt;p&gt;Feel free to access &lt;a href="https://warpjs-744h4bixx1x93pg3oxc3hr4cf.storage.googleapis.com/index.html" rel="noopener noreferrer"&gt;https://warpjs-744h4bixx1x93pg3oxc3hr4cf.storage.googleapis.com/index.html&lt;/a&gt; url to see the demo in action.&lt;/p&gt;

&lt;p&gt;#MadeWithTFJS&lt;/p&gt;

&lt;p&gt;Thanks!&lt;/p&gt;

&lt;h1&gt;
  
  
  About the author
&lt;/h1&gt;

&lt;p&gt;Dominique d'Inverno holds a MSC in telecommunications engineering. After 20 years of experience including embedded electronics design, mobile computing systems architecture and mathematical modeling, he joined ScaleDynamics team in 2018 as AI and algorithm development engineer.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to run Tensorflow.js on a serverless platform : reusing models</title>
      <dc:creator>ddif06</dc:creator>
      <pubDate>Mon, 16 Mar 2020 17:23:45 +0000</pubDate>
      <link>https://dev.to/oxygenit/how-to-run-tensorflow-js-on-a-serverless-platform-500j</link>
      <guid>https://dev.to/oxygenit/how-to-run-tensorflow-js-on-a-serverless-platform-500j</guid>
      <description>&lt;p&gt;In a &lt;a href="https://dev.to/warpjs/introduction-to-tensorflow-js-the-ai-stack-for-javascript-2fb0"&gt;previous article&lt;/a&gt;, we introduced neural networks and TensorFlow framework basics.&lt;/p&gt;

&lt;p&gt;Today, we present convert models developed with Python TensorFlow for use with  &lt;a href="https://www.tensorflow.org/js" rel="noopener noreferrer"&gt;TensorFlow.js&lt;/a&gt;, and discuss web-based versus server-based deployment.&lt;/p&gt;

&lt;h1&gt;
  
  
  TensorFlow, from Python to JavaScript
&lt;/h1&gt;

&lt;p&gt;As we introduced in the first article, the original Python TensorFlow consisted of a declarative-style API.&lt;/p&gt;

&lt;p&gt;The declarative style (requiring by nature a specific debug environment: Tensorboard) and the broad API functionalities induced a relatively long learning curve on the developer's side:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;As a first step, the user constructs a "graph" of all TensorFlow operations (from simple operators on tensors to operations with complete networks, including connections with data sources and sinks).&lt;/li&gt;
&lt;li&gt;then, he creates a "session", in which TensorFlow analyses the graph, resolves the operation schedule and executes all computations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Due to the long time needed to learn and master this API, Google introduced 2 significant python TF improvements toward user-friendliness:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An imperative execution mode ("eager execution"), that is way more intuitive for python (and other script-languages) programmers… and making debugging easier. It was however not fully compatible with all existing features.&lt;/li&gt;
&lt;li&gt;Keras API: a set of high-level operations (network assembly, inference and training), user-friendly, dedicated to neural networks, inherited from Keras by Google in 2017.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tensorflow.js, the JavaScript version of TensorFlow (imperative execution) does not include all TF functionalities available in "declarative" mode, but supports, amongst others, the full Keras API.&lt;/p&gt;

&lt;h1&gt;
  
  
  Ready to use TensorFlow.js models
&lt;/h1&gt;

&lt;p&gt;Pre-trained models are available for public use by non-experts in machine learning on TensorFlow.js model repository, for various applications:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Images processing: classification, objects detection, body/hand pose estimation, body segmentation, face meshing&lt;/li&gt;
&lt;li&gt;Text processing: toxicity detection, sentence encoding,&lt;/li&gt;
&lt;li&gt;Speech processing: command recognition.&lt;/li&gt;
&lt;li&gt;Language processing: the newly released mobileBERT model enables applications like chat bots, ...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All of these are also hosted on NPM. Feel free to visit the repository &lt;a href="https://www.tensorflow.org/js/models" rel="noopener noreferrer"&gt;https://www.tensorflow.org/js/models&lt;/a&gt; for more details.&lt;/p&gt;

&lt;p&gt;More than 1000 available TensorFlow models and variants are being centralized in the &lt;a href="https://www.tensorflow.org/hub" rel="noopener noreferrer"&gt;TensorFlow Hub&lt;/a&gt;, which includes models for Python and the models mentioned above, usable in JavaScript.&lt;/p&gt;

&lt;p&gt;As mentioned in our previous article, the &lt;a href="https://magenta.tensorflow.org/" rel="noopener noreferrer"&gt;Magenta project&lt;/a&gt; (music and art using ML), hosted on NPM as well, provides a JavaScript API using models, amongst which recursive neural networks (RNN).&lt;/p&gt;

&lt;h1&gt;
  
  
  Converting a Python TF model for JavaScript
&lt;/h1&gt;

&lt;p&gt;Although many ready-to-use models are available online, in most cases, re-training (at least, fine-tuning) is often required for a specific application case, when not re-architecting.&lt;/p&gt;

&lt;p&gt;As Python is widely used in model design and training, situations arise where a model developed with Python TF has to be used with JavaScript (browser or Node.js).&lt;/p&gt;

&lt;p&gt;Knowing the Python TF history that was briefly summarized above, when the time comes to save or export a trained model, one won't be surprised to see different formats:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;saved model format: includes a complete model architecture, weights and optimizer configuration in a single folder. Such a model can be used without access to the original python code. Training can be resumed from the checkpoint reached by the time it was saved,&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Keras saved model ('hdf5' format): models created using the Keras API can be saved in a single file ('.h5'). Basically, it contains the same info as the saved model,&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;frozen model ('.pb'): a variant of a saved model, but that cannot be trained anymore (only architecture and weights are saved). It is aimed at being used for inference only.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;TensorFlow provides a converter in python environment: tensorflowjs_converter.&lt;/p&gt;

&lt;p&gt;It can be installed easily using:&lt;/p&gt;

&lt;p&gt;$ pip install tensorflowjs&lt;/p&gt;

&lt;p&gt;This utility converts various model file formats generated by the TF python API into a JSON file with additional binary files containing weights.&lt;/p&gt;

&lt;p&gt;For details on model converter, see the links below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.tensorflow.org/js/guide/conversion" rel="noopener noreferrer"&gt;https://www.tensorflow.org/js/guide/conversion&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.tensorflow.org/js/tutorials/conversion/import_saved_model" rel="noopener noreferrer"&gt;https://www.tensorflow.org/js/tutorials/conversion/import_saved_model&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.tensorflow.org/js/tutorials/conversion/import_keras" rel="noopener noreferrer"&gt;https://www.tensorflow.org/js/tutorials/conversion/import_keras&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In addition, the TensorFlow.js team just released a model conversion wizard (announced at TensorFlow dev summit 2020).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Converting with python shell command-line utility&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Example for a frozen graph model's '.pb' file. The output node of the TensorFlow graph must be specified:&lt;/p&gt;

&lt;p&gt;&amp;gt;&amp;gt;&amp;gt; tensorflowjs_converter \&lt;br&gt;
    --input_format=tf_frozen_model \&lt;br&gt;
    --output_node_names='MobilenetV2/Predictions/Reshape_1' \&lt;br&gt;
    /mobilenet/frozen_model.pb \&lt;br&gt;
    /mobilenet/web_model&lt;/p&gt;

&lt;p&gt;Example for a '.h5' keras model file:&lt;/p&gt;

&lt;p&gt;&amp;gt;&amp;gt;&amp;gt; tensorflowjs_converter --input_format=keras /my_path/my_model.h5 /my_tfjsmodel_path&lt;/p&gt;

&lt;p&gt;Both examples create a JSON model file &amp;amp; binary weights&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Generating a converted model in python code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For Keras models, the tensorflow.js module includes APIs callable in python TF that directly output JSON format.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# In Python code where the model is created and trained
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;tensorflowjs&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;tfjs&lt;/span&gt;
&lt;span class="bp"&gt;...&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;train&lt;/span&gt;&lt;span class="p"&gt;(...):&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;keras&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Sequential&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;   &lt;span class="c1"&gt;# create a layered keras model
&lt;/span&gt;    &lt;span class="bp"&gt;...&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;(...)&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit&lt;/span&gt;&lt;span class="p"&gt;(...)&lt;/span&gt;                                 &lt;span class="c1"&gt;# train model
&lt;/span&gt;    &lt;span class="n"&gt;tfjs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;converters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save_keras_model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;my_tfjsmodel_path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once converted, depending on the model type (Graph or Keras), it can be loaded in a JavaScript environment with Tensorflow.js model loading utilities:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// in JavaScript code inferring the converted model&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;tf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loadGraphModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;myTfjsmodelPath/model.json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;tf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loadLayersModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;myTfjsmodelPath/model.json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then the model is usable for an inference:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;prediction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inputData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Operating a JavaScript model
&lt;/h1&gt;

&lt;p&gt;At some point, a neural network model is sufficiently stable to be used on significant data sets. Depending on the application case, this usage may consist of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;inference only: analyzing "production" data sets (texts, images or other media content, etc…) without further training (at least during the analysis).&lt;/li&gt;
&lt;li&gt;inference and training: part of the "production" data sets is also used for continuous network training in order to increase performance with application-specific experience.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If both browser-based and Node-based TensorFlow.js APIs are equivalent in terms of functionalities, multiple key decision aspects add to performance when selecting the best way to operate the model : data volumes, transfer bandwidth and privacy.&lt;/p&gt;

&lt;p&gt;Browser-based execution is interesting in highly-interactive applications, particularly when processing media that are streamed in or out locally (webcam, graphical user interfaces, sound, …), and for moderate-size NN whose load-time is not crippling for user experience.&lt;/p&gt;

&lt;p&gt;Using a browser-based execution has some drawbacks for standard size-models, impacting a lot the user experience:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The performance of the model is limited, and only moderate size NN modules can be used, despite TensorFlow.js' webGl and Wasm backends that provide acceleration capabilities,&lt;/li&gt;
&lt;li&gt;loading a model can take 15s or even a minute due to the size of models and the performance of the mobile network, which is a long time for the user,&lt;/li&gt;
&lt;li&gt;memory requirements to run the model are high. On small memory devices it restricts the use of the model, breaking application features,&lt;/li&gt;
&lt;li&gt;not all mobile phones/browsers are up to date and the model could not run on all devices.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Of course, this is a current state as Google progresses on some of these issues. In the short term, using a server-based execution using Node.js is an excellent solution that solves all these drawbacks.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Performance of the model is close to Python TF thanks to using native or GPU accelerated versions of TF.js for Node.js, there are no more limits to the model complexity;&lt;/li&gt;
&lt;li&gt;a server has a super fast network, and time to load a model is significantly decreased. Also, servers can be already ready to run with models preloaded;&lt;/li&gt;
&lt;li&gt;a server can be tuned with memory requirements to run any model size;&lt;/li&gt;
&lt;li&gt;the model is guaranteed to run on any server.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The new drawbacks are more related to the remote data transfers to the server, in particular moving sensitive data out of the device must be managed and defined in the service provider...&lt;/p&gt;

&lt;p&gt;It could also open the possibility to perform inference/training processes within or at the edge of the network boundary where the data is stored to reduce latency and data transfer times.&lt;/p&gt;

&lt;p&gt;Only the inference results (usually lighter than input data flows) have to be considered as payload from latency &amp;amp; infrastructure cost viewpoints.&lt;/p&gt;

&lt;p&gt;Finally, TensorFlow.js, on the server side, provides the TFX tool (Tensorflow extended) to deploy production machine-learning pipelines. The AutoML tool (provided by Google Cloud) also provides a GUI-based suite to train and deploy custom ML models without requiring extended machine-learning and NN expertize.&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://dev.to/warpjs/how-to-run-tensorflow-js-on-a-serverless-platform-deploying-models-3b1i"&gt;next article&lt;/a&gt;, we’ll show how to use an online TensorFlow.js model and deploy it rapidly using our &lt;a href="https://scaledynamics.io/warpjs" rel="noopener noreferrer"&gt;WarpJS&lt;/a&gt; JavaScript Serverless Function-as-a-Service (FaaS).&lt;/p&gt;

&lt;p&gt;#MadeWithTFJS&lt;/p&gt;

&lt;p&gt;Thanks!&lt;/p&gt;

&lt;h1&gt;
  
  
  About the author
&lt;/h1&gt;

&lt;p&gt;Dominique d'Inverno holds a MSC in telecommunications engineering. After 20 years of experience including embedded electronics design, mobile computing systems architecture and mathematical modeling, he joined ScaleDynamics team in 2018 as AI and algorithm development engineer.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Introduction to TensorFlow.js, the AI stack for JavaScript</title>
      <dc:creator>ddif06</dc:creator>
      <pubDate>Wed, 26 Feb 2020 17:37:24 +0000</pubDate>
      <link>https://dev.to/oxygenit/introduction-to-tensorflow-js-the-ai-stack-for-javascript-2fb0</link>
      <guid>https://dev.to/oxygenit/introduction-to-tensorflow-js-the-ai-stack-for-javascript-2fb0</guid>
      <description>&lt;p&gt;In my company we're JavaScript fans, and we're working on providing the JavaScript community with cool products to broaden its use to all computing areas. Artificial Intelligence (AI) is currently one of the areas mainly addressed by Python. JavaScript has everything you would need, in the browser or in the cloud thanks to tensorflow.js. This article is an introduction to AI for JavaScript users. You will find everything to get started with basic notions in mind. We will introduce AI concepts, Google's TensorFlow framework, and then the AI stack for JavaScript: TensorFlow.js.&lt;/p&gt;

&lt;h2&gt;
  
  
  AI concepts
&lt;/h2&gt;

&lt;p&gt;Artificial intelligence, and artificial neural networks (NN) in particular, gained increasing adoption in many applications over the last 5 years. It is the result of the convergence of 2 main evolutions: availability of efficient architectures in the cloud and key innovations in neural network training algorithms. It opened new ways like deep learning (a term designating neural networks that includes several hidden layers between inputs and outputs).&lt;/p&gt;

&lt;p&gt;A neural network is built with several neuron layers to constitute a ready-to-use AI model. Previously limited to a few layers, they gained in complexity, depth, efficiency and precision.&lt;/p&gt;

&lt;p&gt;A key step has been accomplished in machine vision with efficient convolutional neural networks, whose architecture is inspired from the human visual cortex.&lt;/p&gt;

&lt;p&gt;Machine vision and image processing are today primary fields for NN, and many pre-trained models of variable complexity exist, as well as collections of training images (like MNIST or imageNet models).&lt;/p&gt;

&lt;h3&gt;
  
  
  Perceptron and layers
&lt;/h3&gt;

&lt;p&gt;Without getting into details, the basic artificial neuron (perceptron) model encountered in most neural networks is shown in the figure below and operates as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it takes multiple input values&lt;/li&gt;
&lt;li&gt;it multiplies each input by a weight value&lt;/li&gt;
&lt;li&gt;it sums all these individual products&lt;/li&gt;
&lt;li&gt;it generates an output from this sum through an activation function, which generally normalizes output values and reduces their spread.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F37h5rysvih99x0dmabvx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F37h5rysvih99x0dmabvx.png" alt="Alt Text" width="800" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Single perceptron model&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Various network architectures are obtained by replicating, interconnecting and cascading such cells, as shown in the figure below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzsq5lig2n9xqeabr2qti.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzsq5lig2n9xqeabr2qti.png" alt="Alt Text" width="800" height="676"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;A fully-connected 15-perceptron topology&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Inference and training
&lt;/h3&gt;

&lt;p&gt;The "direct" operation of a neural network, that consists in applying series of unknown inputs to a network whose weights are defined in order to obtain outputs (such as a prediction of next data, a classification of an image, an indication on whether a specific pattern exists in an image, etc ….) is called " &lt;strong&gt;inference&lt;/strong&gt;".&lt;/p&gt;

&lt;p&gt;The operation that consists of computing the weight values of a neural network, usually by submitting series of inputs together with their expected output values is called " &lt;strong&gt;training&lt;/strong&gt;".&lt;/p&gt;

&lt;p&gt;Training of deep networks is performed by calculating the difference, or "gradient", between the output(s) of the network for a given input and the expected output. This gradient is then split in gradient contributions of every weight of the output layer, and then down to all layers of the network. This process is usually called "gradient backpropagation algorithm". The weights are then adjusted to minimize the gradients in an iterative algorithm on multiple inputs, with optimization policies that are tunable by the user.&lt;/p&gt;

&lt;h3&gt;
  
  
  Neural network model examples
&lt;/h3&gt;

&lt;p&gt;Many types and topologies of Neural Networks (NN) can be built, and ongoing researches continuously improve and enrich existing NN collections available. Deep networks can be built by assembling reused modules (pre-trained or not) proven to be efficient at a given task.&lt;/p&gt;

&lt;p&gt;However, typical module architectures exist that turn out to be efficient at specific tasks.&lt;/p&gt;

&lt;p&gt;In particular :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;fully connected networks: in these, all outputs of a layer are connected to all inputs of the next layer, which makes the "treillis" connection complex for deep structures of this type and makes training tricky to tune. They are often used as final decision layers on top of other structures.&lt;/li&gt;
&lt;li&gt;convolutional networks: directly inspired from the human visual cortex, they are very efficient at image analysis (filtering, features detection), can be easily replicated and grouped to analyze picture regions and separate channels, and stacked in deep structures without prohibitive complexity. Moreover, pre-trained submodules for a specific feature, for instance, can advantageously be reused in different networks, which reduces training times. They are usually topped by a few fully-connected layers depending on the required final outputs.&lt;/li&gt;
&lt;li&gt;recurrent networks: these introduce memory elements which make them able to analyze and predict time series in the broad sense, which can go from text analysis (sentiment classification) to music creation and stock prices trends prediction.&lt;/li&gt;
&lt;li&gt;residual networks: those are networks (or assemblies of networks) of any type, in which the results of a given layer are added to the result of a particular layer ("skip connections"), which accelerates training in very deep networks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  TensorFlow framework
&lt;/h2&gt;

&lt;p&gt;Increasing adoption of AI in big data processing pulled in the need for frameworks that are efficient at creating/editing complex networks, manipulating various types of data sets (multidimensional matrices being a baseline), and performing inference and training operations from an outline view without having to explicitly express every neuron operation and related algorithms.&lt;/p&gt;

&lt;p&gt;As Google has been pioneering deployment of AI in its infrastructures for a long time, it open-sourced its home-brewed internal framework in 2015 under the "TensorFlow" name (referred to here under as TF).&lt;/p&gt;

&lt;p&gt;Built on top of Python, an environment widely adopted by scientific and data analysts for its simplicity and plethoric availability of math and array-specific libraries, TF first consisted of a declarative-style API. It was very comprehensive and versatile, and allowed to solve almost any problem of tensor (multidimensional matrix) calculus, including of course complex neural networks.&lt;/p&gt;

&lt;p&gt;Later on, Google included an imperative mode (for more "intuitive" programming and more straightforward debugging) and Keras API, a third party higher-level function set that makes neural network development, training and inference easier.&lt;/p&gt;

&lt;p&gt;Note that Python TF provides a C++ optimized computing backend and a CUDA-based backend for platforms equipped with NVIDIA GPUs to provide performant inferences or trainings.&lt;/p&gt;

&lt;h2&gt;
  
  
  TensorFlow.js (using JavaScript)
&lt;/h2&gt;

&lt;p&gt;In 2018, a JavaScript version of TensorFlow was released: &lt;strong&gt;TensorFlow.js&lt;/strong&gt; , to enable its use in browsers or Node.js. When launched, it supported imperative execution mode and Keras API but was missing full support of "legacy" python TF functionalities. TensorFlow.js ramped up rapidly, release after release, &lt;a href="https://youtu.be/iH9CS-QYmZs?t=526" rel="noopener noreferrer"&gt;towards alignment with Keras python API&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Although TensorFlow.js supports all advanced functionalities and algorithms for both inference and training, it is mainly used for inference of pre-trained models in web browsers, which was improved by a webGL-based computation backend to take advantage of GPUs within the browser.&lt;/p&gt;

&lt;p&gt;Good examples are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Magenta.js (music and art using machine learning): a Google research project on creative neural networks (&lt;a href="https://magenta.tensorflow.org/" rel="noopener noreferrer"&gt;https://magenta.tensorflow.org/&lt;/a&gt;) that offers open-source tools, models and demos (interactive music composition, amongst others).&lt;/li&gt;
&lt;li&gt;Coco-ssd: Object detection in webcam-streamed images using mobilenet NN.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On Node.js side, TensorFlow.js is available in 2 versions. The whole stack of the first version is fully written in JavaScript while the second uses the same C++ backend and CUDA-based backend for NVIDIA GPUs as the python version of TensorFlow.&lt;/p&gt;

&lt;p&gt;The TensorFlow.js team recently released a Wasm backend (optimizing performance on browsers through native C++ kernels without using a GPU), and will release soon a WebGpu backend (evolution of webGL standard).&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://dev.to/ddif06/how-to-run-tensorflow-js-on-a-serverless-platform-500j"&gt;this second part article&lt;/a&gt;, we describe how to operate TensorFlow.js, using models coming from Python TensorFlow or ready-to-use models in the browser and in the cloud using our &lt;a href="https://scaledynamics.io/warpjs" rel="noopener noreferrer"&gt;WarpJS&lt;/a&gt; product. JavaScript fans will have everything to start entering this area. In the meantime, visit &lt;a href="https://www.tensorflow.org/js" rel="noopener noreferrer"&gt;https://www.tensorflow.org/js&lt;/a&gt; for API documentation and installation guidelines, including tutorials, guides and demos.&lt;/p&gt;

&lt;h2&gt;
  
  
  About the author
&lt;/h2&gt;

&lt;p&gt;Dominique d'Inverno holds a MSC in telecommunications engineering. After 20 years of experience including embedded electronics design, mobile computing systems architecture and mathematical modeling, he joined ScaleDynamics team in 2018 as AI and algorithm development engineer.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
