Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Artificial Intelligence

TensorFlow

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

TensorFlow Tensors have 3 properties:

  • Type
  • Rank
  • Shape

Using TensorFlow

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

Example

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@3.6.0/dist/tf.min.js"></script>

To make sure you always use the latest version, you can use this:

Example 2

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

Tensors

The central data unit in TensorFlow.js is the Tensor.

A Tensor is much the same as an multidimensional array.

A Tensor has the following properties:

PropertyDescription
dtypeThe data type
rankThe number of dimensions
shapeThe size of each dimension

Creating a Tensor

A Tensor can be created from any N-dimensional array:

Example 1

const tensorA = tf.tensor([[1, 2], [3, 4]]);

Try it Yourself »

Example 2

const tensorA = tf.tensor([[1, 2], [3, 4], [5, 6]]);

Try it Yourself »


Tensor Shape

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

Example1

const shape = [2, 2];
const tensorA = tf.tensor([1, 2, 3, 4], shape);

Try it Yourself »

Example2

const tensorA = tf.tensor([1, 2, 3, 4], [2, 2]);

Try it Yourself »

Example3

const tensorA = tf.tensor([[1, 2], [3, 4]], [2, 2]);

Try it Yourself »


Tensor Data Types

A Tensor can have the following data types:

  • bool
  • int32
  • float32 (default)
  • complex64
  • string

When you create a tensor, you can specify the datatype as the third parameter:

Example

const tensorA = tf.tensor([1, 2, 3, 4], [2, 2], "int32");
/*
Results:
tensorA.rank = 2
tensorA.shape = 2,2
tensorA.dtype = int32
*/

Try it Yourself »


Tensor Addition

You can add two tensors using tensorA.add(tensorB):

Example

const tensorA = tf.tensor([[1, 2], [3, 4], [5, 6]]);
const tensorB = tf.tensor([[1,-1], [2,-2], [3,-3]]);

// Tensor Addition
const tensorAdd = tensorA.add(tensorB);

// Result: [ [2, 1], [5, 2], [8, 3] ]

Try it Yourself »


Tensor Subtraction

You can subtract two tensors using tensorA.sub(tensorB):

Example

const tensorA = tf.tensor([[1, 2], [3, 4], [5, 6]]);
const tensorB = tf.tensor([[1,-1], [2,-2], [3,-3]]);

// Tensor Subtraction
const tensorAdd = tensorA.sub(tensorB);

// Result: [ [0, 3], [1, 6], [2, 9] ]

Try it Yourself »


Tensor Square

You can square a tensor using tensor.square():

Example

const tensorA = tf.tensor([1, 2, 3, 4]);

// Tensor Square
const tensorSquare = tensorA.square();

// Result [ 2, 6, 12, 20 ]

Try it Yourself »


Tensor Reshape

The number of elements in a tensor is the product of the sizes in the shape.

Since there can be different shapes with the same size, it is often useful to reshape a tensor to other shapes with the same size.

You can reschape a tensor using tensor.reshape():

Example

const tensorA = tf.tensor([[1, 2], [3, 4]]);
const tensorB = tensorA.reshape([4, 1]);

// Result: [ [1], [2], [3], [4] ]

Try it Yourself »


Retrieve Tensor Values

You can get the data behind the tensor using tensor.data():

Example

const tensorA = tf.tensor([[1, 2], [3, 4]]);
tensorA.data().then(data => display(data));

// Result: 1,2,3,4
function display(data) {
  document.getElementById("demo").innerHTML = data;
}

Try it Yourself »

You can get the array behind the tensor using tensor.array():

Example

const tensorA = tf.tensor([[1, 2], [3, 4]]);
tensorA.array().then(array => display(array));

// Result: 1,2
function display(array) {
  document.getElementById("demo").innerHTML = array[0];
}

Try it Yourself »


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.