Tensorflow.JS - usage notes

Tags: #<Tag:0x00007f06aed27178> #<Tag:0x00007f06aed27060> #<Tag:0x00007f06aed25148> #<Tag:0x00007f06aed24f68> #<Tag:0x00007f06aed24dd8>

This is a growing Wiki article about Tensorflow.JS that is part of a series on Machine Learning for Information Security domains.

If you are interested in the topic it’s important to focus on the actual development rather than the snake-oil.

Our problems are man-made;
therefore they may be solved by man.
(John F. Kennedy in a speech, 1963)

Preliminary references:

Arvind Narayanan, How to recognize AI snake oil
PDF - presentation Princeton University


MIT-STS-AI-snakeoil.pdf (1.1 MB)

We don’t need to add more Fear Uncertainty and Doubt (FUD) into the Information Security domains.

Real-word Information Security may provide the fidelity for Heuristics rather than for Machine Learning; in the sense that we can only apply discovered practical methods to speed up the process of finding good-enough solutions[1][2].

– It’s surprisingly difficult to build concluding definitions of the involved terms such as “Machine Learning”, “Heuristics” or of the specific Information Security aspects. That is because a lot of groundwork is missing.

Tensorflow.JS - usage notes

The goal of Machine Learning is to train a model from input data.

A model then can be used to predict or infer output data. Tensorflow.JS is developed to deploy ML models into a browser (and / or JavaScript runtime such as Node.JS).

Short note on terminology and jargon


There are different schools of thought about the definitions of terms such as

  • Machine Learning and DataScience - “without being explicitly programmed”[3]
  • Deep Learning - supervised, semi-supervised and unsupervised
  • (Deep) Reinforcement Learning

Rather than reciting an opinion, why don’t you dive into the field using some of the MIT Deep Learning lectures[4]?

The first wave of approaches pioneered in the 1940s[5] with the work of McCulloch and Pitts on the character and relations of nervous activity and neural events.

The second [wave] occurred in the 1960s with Rosenblatt’s perceptron convergence theorem and Minsky and Papert’s work showing the limitations of s simple perceptron.[6]

In the 1980s there was renewing interest in Artifical Neural Networks.1982 Werbos proposed back-propagation learning algorithms for multi-layer perceptrons.[7]

Today’s most progressive fields in Machine Learning are Natural Language Processing (NLP) and Deep Neural Networks.

  • It’s important to understand the underlying concepts in relation to Tensorflow’s programming paradigm.[8] Otherwise, it’s not possible to use the results.
  • In a couple of years it’s going to be 100 years of Machine Learning and look what they have learned :stuck_out_tongue:

Technology preface: Node.JS

Here the Wiki sections are reduced to bare usage notes and code listings.

node --version

ES 6 imports

» npm install --save esm
» grep start package.json
    "start": "node -r esm index.js"
» npm start

> [email protected] start /Users/marius.ciepluch/Source/tensorflow/firststeps
> node -r esm index.js

This way you get to use modern JS import syntax:

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

// Define a model for linear regression.
const model = tf.sequential();

Note: ignore that this import does not feature the package with Tensorflow C++ bindings[9].
This is primarily useful in order to bridge the gap between various pieces of Tensorflow.JS code that target runtimes.

Tabular integration with modern JavaScript

DataScience libraries consume either JSON or CSV formatted data, sometimes from files, sometimes from variables. Sometimes they invent custom formats. It’s messy.

Custom CSV loader with lodash - Shape and Dimension of a Tensor

Key Node.js library Used version Use case
Lodash 4.17 data cleanup
ESM 3.2 ES 6 code

We may need custom loaders because tools and libs for statistical analysis (or analysts) have different styles. For example SPSS, Cognos, Tableau versus Excel, Power BI…

const FileSystem = require("fs");
const _ = require('lodash');

 * Uses lodash for a custom CSV loader
 * @param filename - to read the CSV from
 * @param options
function loadCSV(filename, options) {

    let data = FileSystem.readFileSync(filename, { encoding:"utf-8" });

    // get an array of string objects
    data = data.split("\n").map(
        row => row.split(",")

    // remove empty elements
    data = data.filter(function (el) {
        return el != '';

    // cleanup empty columns
    data = data.map(row => _.dropRightWhile(row, val => val === ''));


The code does not use ES6 imports here. The synchronous file-read into data is blocking (not very Node.JS like) and the implementation is not complete. This is boilerplate code for a custom CSV loader.

Load a CSV file
  [ 'sku', 'title', 'hardware', 'price' ],
  [ '12345', 'Hollow Knight', 'Nintendo Switch', '15.99' ],
  [ '35342', 'Dark Souls Remastered', 'Nintendo Switch', '39.99' ]

Thinking in terms of Tensors: this particular structure is an array of arrays. In Computer Science the usual mathematical term is a vector or a matrix. All elements have the same data-type.

  • We can see 2 dimensions (nested array of arrays) - count the number of opening [s before the first element
  • The shape is [ 3, 4 ]. The number of horizontal elements once per dimension.

This is important to understand Tensor(flow) operations[10].

A generic and simple CSV to JSON converter without hardcoded fields

This is rudimentary, but it’s important to exchange data. CSV is a tabular format and JSON is not.

Key Node.js library Used version Use case
csvtosjon 2.0 parser, converter
ESM 3.2 ES 6 code
 * A custom converter CSV to JSON

const csv = require("csvtojson");

function runCSVtoJSON() {

    let myCSVdata =
        `col1, col2, col3
        val1, val2, val3`;

        noheader: false,
        output: "json"
        .then((csvRow) => {

The result:

[ { col1: 'val1', col2: 'val2', col3: 'val3' } ]

A generic and simple JSON to CSV converter without hardcoded fields

This example excludes flattening etc.

Key Node.js library Used version Use case
jsonexport 2.4 parser, converter
ESM 3.2 ES 6 code
 * A custom generic converter for JSON data to a columnar CSV
 * @param jsonData
const jsonexport = require('jsonexport');

function runJSONtoCSV(jsonData) {

    var myJSONdata = JSON.parse(`{ "col1" : "val1", "col2" : "val2", "col3" : "val3" }`);

    jsonexport(myJSONdata, {verticalOutput : false}, function(err, csv){
        if(err) return console.log(err);


The result:


This way you can convert a JSON data-set (maybe from a REST endpoint) to other formats, such as Apache Arrow[11][12].

  • basic code to ensure interoperability by converting data to different formats
  • modern ES6 imports can be used in a Node.JS project (with npm or yarn)
  • Example of a Tensor, shape, and dimension

Tensorflow.JS - one step deeper

  1. https://stats.stackexchange.com/questions/300350/is-machine-learning-an-heuristic-method ↩︎

  2. Dr. Gerhard Wickler and Prof. Austin Tate, School of Informatics, University of Edinburgh - Refer to Domain-Specific vs. Domain-Independent Planning. What can we plan in Information Security? ↩︎

  3. https://ieeexplore.ieee.org/document/5391906 ↩︎

  4. https://deeplearning.mit.edu ↩︎

  5. https://www.cs.cmu.edu/~./epxing/Class/10715/reading/McCulloch.and.Pitts.pdf - McCulloch and Pitts - A Logical Calculus of Ideas Immanent in Nervous Activity - Department of Psychiatry - University of Chicago - 1943 ↩︎

  6. IEEE Theme feature - Artifical Neural Networks: A Tutorial - 1996 ↩︎

  7. P. Werbos "Beyond Regression: New Tools for Prediction and Analysis in Behavioral Sciences - 1984 ↩︎

  8. https://stackoverflow.com/questions/44210561/how-do-backpropagation-works-in-tensorflow ↩︎

  9. https://www.tensorflow.org/js/guide/nodejs - Vanilla CPU ↩︎

  10. https://www.tensorflow.org/js/guide/tensors_operations ↩︎

  11. https://github.com/trxcllnt/csv-to-arrow-js ↩︎

  12. https://arrow.apache.org ↩︎