TensorFlow.js Tutorial

 

TensorFlow

What is TensorFlow.js?

Tensorflow is popular JavaScript library for Machine Learning.

Tensorflow lets us train and deploy machine learning in the Browser.

Tensorflow lets us add machine learning functions to any Web Application.

Using TensorFlow

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

If you always want to use the latest version, drop the version number:

Example 2

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

To use TensorFlow.js, add the following script tag to your HTML file(s):

TensorFlow was developed by the Google Brain Team for internal Google use, but was released as open software in 2015.

In January 2019, Google developers released TensorFlow.js, the JavaScript Implementation of TensorFlow.

Tensorflow.js was designed to provide the same features as the original TensorFlow library written in Python.


Tensors

TensorFlow.js is a JavaScript library to define and operate on Tensors.

The main data type in TensorFlow.js is the Tensor.

A Tensor is much the same as a multidimensional array.

A Tensor contains values in one or more dimensions:

Tensor

 

A Tensor has the following main properties:

Property Description
dtype The data type
rank The number of dimensions
shape The size of each dimension

Sometimes in machine learning, the term "dimension" is used interchangeably with "rank.

[10, 5] is a 2-dimensional tensor or a 2-rank tensor.

In addition the term "dimensionality" can refer to the size of a one dimension.

Example: In the 2-dimensional tensor [10, 5], the dimensionality of the first dimension is 10.



Creating a Tensor

The main data type in TensorFlow is the Tensor.

A Tensor is created from any N-dimensional array with the tf.tensor() method:

Example 1

<!DOCTYPE html>
<html>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<body>

<h1>TensorFlow JavaScript</h1>
<h3>Creating a tensor:</h3>

<div id="demo"></div>

<script>
const myArr = [[1, 2, 3, 4]];
const tensorA = tf.tensor(myArr);
document.getElementById("demo").innerHTML = tensorA;
</script>

</body>
</html>

 

Example 2

<!DOCTYPE html>
<html>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<body>

<h1>TensorFlow JavaScript</h1>
<h3>Creating a tensor:</h3>

<div id="demo"></div>

<script>
const myArr = [[1, 2], [3, 4]]
const tensorA = tf.tensor(myArr);
document.getElementById("demo").innerHTML = tensorA;
</script>

</body>
</html>

 

Example 3

<!DOCTYPE html>
<html>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<body>

<h1>TensorFlow JavaScript</h1>
<h3>Creating a tensor:</h3>

<div id="demo"></div>

<script>
const tensorA = tf.tensor([[1, 2], [3, 4], [5, 6]]);
document.getElementById("demo").innerHTML = tensorA;
</script>

</body>
</html>

 


Tensor Shape

A Tensor can also be created from an array and a shape parameter:

Example1

<!DOCTYPE html>
<html>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<body>

<h1>TensorFlow JavaScript</h1>
<h3>Creating a tensor with a shape:</h3>

<div id="demo"></div>

<script>
const myArr = [1, 2, 3, 4];
const shape = [2, 2];
const tensorA = tf.tensor(myArr, shape);
document.getElementById("demo").innerHTML = tensorA;
</script>

</body>
</html>

 

Example2

<!DOCTYPE html>
<html>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<body>

<h1>TensorFlow JavaScript</h1>
<h3>Creating a tensor with a shape:</h3>

<div id="demo"></div>

<script>
const tensorA = tf.tensor([1, 2, 3, 4], [2, 2]);
document.getElementById("demo").innerHTML = tensorA;
</script>

</body>
</html>

TFJS Tutorial
Login
ADS CODE