﻿ Basic Concepts in Tensorflow.js - XpertUp

# Basic Concepts in Tensorflow.js

TensorFlow.js is an open source WebGL-accelerated JavaScript library for machine intelligence. You can design, train and test machine learning algorithms right on your browser. If you haven’t checked our previous post on Tensorflow,js introduction and how to configure Tensorflow.js to get started please see our post Introduction to Tensorflow.js.

In this post we will discuss some of the basic concepts which we need to know to better understand Tensorflow.js.

### Tensors

Tensors are the basic building block in this framework. It is the central unit of data. If you are familiar deep learning framework Tensorflow you should know that it is a n dimensional array that takes in numerical values. Shape attribute of an Tensor instance defines the array shape.

Basic constructor used to create tensor is tf.tensor

The tensors are immutable; once created, you cannot change their values. You need to perform operations on them to generate new tensors.

Scalar Tensor

`const tensor = tf.scalar(2);`

Tensor of specific shape

```const shape = [3, 2]; // define shape; 2 rows, 3 columns

const a = tf.tensor([5.0, 1.0, 3.5, 1.0, 2.4, 3.0], shape); // create tensor```

Size of the Tensor

`const tensor_s = tf.tensor([2,2]).shape; // gives the size of the tensor`

Tensor 0f shape 3 X 2 without the shape

`const b = tf.tensor([[5.0, 1.0], [ 3.5, 1.0], [2.4, 3.0]);`

### Operators

Operations (ops) allow you to manipulate that data stored in tensors. Because tensors are immutable, these operations do not change their values; instead, return new tensors.

Square Operation

Its a unary operation

```const a = tf.tensor([1,2,3]);

const d_squared = d.square();

d_squared.print() // returns [1, 4, 9]```

Its a binary operation

```const a = tf.tensor([[1.0, 2.0], [3.0, 4.0]]);

const b = tf.tensor([[5.0, 6.0], [7.0, 8.0]]);

c.print(); // Output [[6, 8], [10, 12]]```

Chaining Operations

TensorFlow.js has a chainable API; you can call ops on the result of ops:

```// Like from the previous tensors a and b we can call square on add operator.

sq_sum.print(); // Output: [[36 , 64 ], [100, 144]]

//or

### Variables

Variables are mutable and are initialized with a tensor of values.

```const initialValues = tf.zeros();

const biases = tf.variable(initialValues); // initialize biases

biases.print(); // output: [0, 0, 0, 0, 0]```

Assign Method

To assign a new tensor to an existing variable we have the assign method.

```const updatedValues = tf.tensor([0, 1, 0, 1, 0]);

biases.assign(updatedValues); // update values of biases

biases.print(); // output: [0, 1, 0, 1, 0]```

### Memory Management

TensorFlow.js comes with two functions to help with memory management: dispose and tf.tidy. Since TensorFlow.js uses the GPU  it’s necessary to manage GPU memory when working with it.

dispose

We can call `dispose` on a tensor or variable to purge it this helps free up GPU memory.

```const x = tf.tensor([1,2,3]);

x.dispose();```

tf.tidy

tf.tidy executes a function and purges any intermediate tensors created, freeing up their GPU memory. The return value of the inner function is not purged.

```function f(x)
{
return tf.tidy(()=>{
const y = x.square(); // y will be pursed since its not needed after we calculate z.
const z = x.mul(y);
return z
});
}```

### Models and Layers

A model in simple terms a function. If given some input it will return some desired output. There are two ways to create models in Tensorflow.js. You can use ops directly to represent the work the model or use the model function.

Using Operations

```// Define function
function predict(input) {
// y = a * x ^ 2 + b * x + c
return tf.tidy(() => {
const x = tf.scalar(input);
const ax2 = a.mul(x.square());
const bx = b.mul(x);
return y; });
}

// Define constants: y = 2x^2 + 4x + 8
const a = tf.scalar(2);
const b = tf.scalar(4);
const c = tf.scalar(8);
// Predict output for input of 2
const result = predict(2);
result.print()
// Output: 24```

Using Model function:

```The high-level API tf.model  is used construct a model out of layers, which are a popular abstraction in deep learning. Here we construct a sequential model with tf.sequential function.

const model = tf.sequential();
tf.layers.simpleRNN({
units: 20,
recurrentInitializer: 'GlorotNormal',
inputShape: [80, 4]
})
);
const optimizer = tf.train.sgd(LEARNING_RATE);
model.compile({optimizer, loss: 'categoricalCrossentropy'});
model.fit({x: data, y: labels});

```

### Conclusion

Thanks for reading, more post on working with Tensorflow.js, designing, creating and running models coming soon!! Follow our website to learn the latest technologies, and concepts. You can also continue reading our post on Tensorflow vs Pytorch

For any queries feel free to comment down below. 