logo

NJP

TensorFlow.JS (Prediction)+ ServiceNow

Import · Jun 18, 2018 · article

A few months back when I was wondering how can we use D3JS and found interesting article by @mitchellstutler.

Tried few examples and was quiet happy as now we knew how to use D3 in ServiceNow (not limited to ChartJS or HighCharts).

Yesterday I was going through the recent stuff posted by sentdx and he happened to be working on TensorFlow JS (for a change). And now I wanted to see put a simple TF.JS program on SN platform. So lets begin this:

TensorFlow.js

A JavaScript library for training and deploying ML models in the browser and on Node.js

Source : Wikipedia

TensorFlow is an open-source software library for dataflow programming across a range of tasks. It is a symbolic math library, and is also used for machine learning applications such as neural networks. It is used for both research and production at Google,often replacing its closed-source predecessor, DistBelief.

TensorFlow was developed by the Google Brain team for internal Google use. It was released under the Apache 2.0 open source license on November 9, 2015.

Add the dependency and run first widget(Yes As simple as that :P)

Lets first add a dependency library :

ServicePortal -> Dependency

image

image

URL is

https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.11.6

image

Now lets get the widget up and running image

image

image

Lets move to the widget editor :

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.11.6"> </script>

    <!-- Place your code in the script tag below. You can also use an external .js file -->
    <script>
      // Notice there is no 'import' statement. 'tf' is available on the index-page
      // because of the script tag above.

      // Define a model for linear regression.
      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.
      const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
      const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);

      // Train the model using the data.
      model.fit(xs, ys, {epochs: 10}).then(() => {
        // Use the model to do inference on a data point the model hasn't seen before:
        // Open the browser devtools to see the output
        model.predict(tf.tensor2d([5], [1, 1])).print();
      });
    </script>

image

Open the browser console to see the output :

image

Okay that prediction worked.

Basic usage but thought to share with you all :).

View original source

https://www.servicenow.com/community/now-platform-articles/tensorflow-js-prediction-servicenow/ta-p/2310325