Entradas


In Deep Learning, we have to fill the pixels of an image in an array or in a matrix. To accomplish this, TensorFlow.js supports the tf.fill() function. It is used to set the same value in the tensor array or tensor matrix.

tf.fill() Meethod

The tf.fill() function is used to set the element in a tensor with specified value.

We can fill that value more than one time in a tensor.

Syntax:

tf.fill(shape,value,dtype)

 
It takes three parameters.

Parameters:

    1. The shape is used to set the value n times. If it is a two-dimensional tensor, we can specify the number of rows and number of columns.
    2. The value is the numeric or string element that is filled in a tensor.
    3. The dtype is used to specify the element type.

Example 1

Create a 1D tensor with numeric value -2 ,10 times in a tensor.

<html>
<!—   CDN Link that delivers the Tensorflow.js framework —>
<script src=«https://cdn.jsdelivr.net/npm/@tensorflow/tfjs»></script>
 
<body>
<center><h1>Linux Hint</h1></center>
<center><h2>Tensorflow.js – tf.fill() </h2></center>
<script>
 //create a tensor with value-2 and fill 10 times.
let values = tf.fill(shape = [10],value = 2)
//contemporáneo tensor
document.write(«Tensor: «,values);
 
</script>
 
</body>
</html>

 
Output:


2 is added 10 times to a tensor.

Example 2

Create a 1D tensor with string value, ‘Linux Hint’, 4 times in a tensor.

<html>
<!—   CDN Link that delivers the Tensorflow.js framework —>
<script src=«https://cdn.jsdelivr.net/npm/@tensorflow/tfjs»></script>
 
<body>
<center><h1>Linux Hint</h1></center>
<center><h2>Tensorflow.js – tf.fill() </h2></center>
<script>
 //create a tensor with element-‘Linux Hint’ and fill 4 times.
let values = tf.fill(shape = [4],value=‘Linux Hint’,dtype=‘string’)
//contemporáneo tensor
document.write(«Tensor: «+values);
 
</script>
 
</body>
</html>

 
Output:


‘Linux Hint’ is added 4 times to a tensor with string data type.

Example 3

Create a 2D tensor with numeric value – 20, 6 (2 rows and 3 columns) times using tf.fill().

<html>
<!—   CDN Link that delivers the Tensorflow.js framework —>
<script src=«https://cdn.jsdelivr.net/npm/@tensorflow/tfjs»></script>
 
<body>
<center><h1>Linux Hint</h1></center>
<center><h2>Tensorflow.js – tf.fill() </h2></center>
<script>
 //create a tensor with element-‘Linux Hint’ and fill 5 times.
let values = tf.fill(shape = [2,3],value=20)
//contemporáneo tensor
document.write(«Tensor: «+values);
 
</script>
 
</body>
</html>

 
Output:


The value, 20, is added in tensor with shape 2 rows and 3 columns.

Conclusion

We saw how to fill values in a tensor using the fill() method. Using this method, we can specify the datatype of the element and we can create a tensor with multiple dimensions. This article discussed three different examples with string and integer data types.



Source link


“tf.pow() in tensorflow.js is used to raise power with respect to the values in another tensor.”

Scenario 1: Work With Scalar

Scalar will store only one value. But anyway, it returns a tensor.

Syntax

Parameters
scalar1 and scalar2 are the tensors that can take only one value as a parameter.

Return
Return remainder of two scalar values.

Example
Create two scalars and raise the power to value present in the second scalar.

<html>
<!–   CDN Link that delivers the Tensorflow.js framework –>
<script src=«https://cdn.jsdelivr.net/npm/@tensorflow/tfjs»></script>

<body>
<script>
//scalar1
let  value1 = tf.scalar(3);

//scalar2
let  value2 = tf.scalar(4);

document.write(«Scalar-1: «,value1);

document.write(«<br>»);
document.write(«<br>»);

document.write(«Scalar-2: «,value2);
</script>
<h3>Tensorflow.js tf.pow() </h3>

<script>
//tf.pow(value1,value2)
document.write(tf.pow(value1,value2));
</script>

</body>
</html>

Output

Working
3 to the power of 4 => 3*3*3*3 = 81.

Scenario 2: Work With Tensor

A tensor can store multiple values; it can be single or multi-dimensional.

Syntax

Parameters
tensor1 and tensor2 are the tensors that can take only single or multiple values as a parameter.

Return
Power of values.

We must notice that the total number of elements in both the tensors must be equal.

Example 1
Create two one-dimensional tensors and return the power of elements in a first tensor concerning values in a second tensor.

<html>
<!–   CDN Link that delivers the Tensorflow.js framework –>
<script src=«https://cdn.jsdelivr.net/npm/@tensorflow/tfjs»></script>

<body>
<script>
//tensor1
let  values1 = tf.tensor1d([2,3,4]);

//tensor2
let  values2 = tf.tensor1d([1,2,3]);

document.write(«Tensor-1: «,values1);

document.write(«<br>»);
document.write(«<br>»);

document.write(«Tensor-2: «,values2);
</script>
<h3>Tensorflow.js tf.pow() </h3>

<script>
//tf.pow(values1,values2)
document.write(tf.pow(values1,values2));
</script>

</body>
</html>

Output

Working
[2 power 1,3 power 2,4 power 3,] => Tensor [2,9,64].

Example 2
Create 2 two-dimensional tensors with 2 rows and 3 columns and apply tf.pow().

<html>
<!–   CDN Link that delivers the Tensorflow.js framework –>
<script src=«https://cdn.jsdelivr.net/npm/@tensorflow/tfjs»></script>

<body>
<script>
//tensor1
let  values1 = tf.tensor2d([1,2,3,4,5,6],[2,3]);

//tensor2
let  values2 = tf.tensor2d([2,2,2,2,2,2],[2,3]);

document.write(«Tensor-1: «,values1);

document.write(«<br>»);
document.write(«<br>»);

document.write(«Tensor-2: «,values2);
</script>
<h3>Tensorflow.js tf.pow() </h3>

<script>
//tf.pow(values1,values2)
document.write(tf.pow(values1,values2));
</script>

</body>
</html>

Output

Scenario 3: Work With Tensor & Scalar

It can be possible to raise the power of each element in a tensor by a scalar.

Syntax

Example
Create a one-dimensional tensor,  a scalar, and raise each element in a tensor to a scalar value.

<html>
<!–   CDN Link that delivers the Tensorflow.js framework –>
<script src=«https://cdn.jsdelivr.net/npm/@tensorflow/tfjs»></script>

<body>
<script>
//tensor
let  values1 = tf.tensor1d([10,20,30,4,5,6]);

//scalar
let  value2 = tf.scalar(2);

document.write(«Tensor: «,values1);

document.write(«<br>»);
document.write(«<br>»);

document.write(«Scalar: «,value2);
</script>
<h3>Tensorflow.js tf.pow() </h3>

<script>
//tf.pow(values1,value2)
document.write(tf.pow(values1,value2));
</script>

</body>
</html>

Output

Conclusion

tf.pow() in tensorflow.js is used to raise power with respect to the values in another tensor. Also, we noticed that scalar will store only one value and returns a tensor. While performing tf.pow() on two tensors, ensure that the number of elements in two tensors must be the same.



Source link


We already know how to create a tensor in the tensorflow.js library and display all the values from it. Now, the task is to return only some portion/range of elements from a tensor.

How do you do that?

The answer is quiebro simple. Tensorflow.js library supports the tf.slice() function which returns the elements based on the index. The index starts with 0.

Let’s see how to get the elements from a tensor.

Tensorflow.js – tf.slice()

The tf.slice() function is used to return elements from a tensor within the range and return those range of elements in a new tensor. It takes three parameters.

Syntax:

tf.slice(tensor.start,size)

 
Parameters:

    1. Tensor can be single or two-dimensional.
    2. Start specifies the index position in which the starting range is specified.
    3. Size takes an integer that returns the elements from the starting range.

Example 1:

Create a one-dimensional tensor with 10 integer values and get the following range of values:

    1. From index-0 to index-6 (start should be 0 and size is 7)
    2. From index-0 to index-8 (start should be 0 and size is 9)
    3. From index-3 to index-8 (start should be 3 and size is 6)
    4. From index-4 to index-9 (start should be 4 and size is 6)
<html>
<!—   CDN Link that delivers the Tensorflow.js framework —>
<script src=«https://cdn.jsdelivr.net/npm/@tensorflow/tfjs»></script>
 
<body>
<center><h2>Tensorflow.js – tf.slice() </h2></center>
<script>
 
 //create a tensor
let values = tf.tensor1d([1,2,3,4,5,6,7,8,9,10]);
//presente tensor
document.write(«<b>Coetáneo Scalar: </b>»,values);
 
document.write(«<br>»);
 
// index-0 to index-6
document.write(«<b>Elements from index-0 to index-6: </b> «+tf.slice(values,[0],7));
document.write(«<br>»);

// index-0 to index-8
document.write(«<b>Elements from index-0 to index-8: </b> «+tf.slice(values,[0],9));
document.write(«<br>»);

// index-3 to index-8
document.write(«<b>Elements from index-3 to index-8: </b> «+tf.slice(values,[3],6));
document.write(«<br>»);

// index-4 to index-9
document.write(«<b>Elements from index-4 to index-9: </b> «+tf.slice(values,[4],6));
document.write(«<br>»);
</script>
 
</body>
</html>

 
Output:


We got the elements from index-0 to index-6. The total number of elements is 7.

Hence, we specified the size as 7.

Similarly:

    1. From index-0 to index-8, the size is 9.
    2. From index-3 to index-8, the size is 6.
    3. From index-4 to index-9, the size is 6.

Example 2:

Create a one-dimensional tensor with 5 integer values and get the following range of values:

    1. From index-0 to index-3 (start should be 0 and size is 4)
    2. From index-3 to index-4 (start should be 3 and size is 1)
<html>
<!—   CDN Link that delivers the Tensorflow.js framework —>
<script src=«https://cdn.jsdelivr.net/npm/@tensorflow/tfjs»></script>
 
<body>
<center><h2>Tensorflow.js – tf.slice() </h2></center>
<script>
 
 //create a tensor
let values = tf.tensor1d([1,2,3,4,5]);
//presente tensor
document.write(«<b>Coetáneo Scalar: </b>»,values);
 
document.write(«<br>»);
 
// index-0 to index-3
document.write(«<b>Elements from index-0 to index-3: </b> «+tf.slice(values,[0],4));
document.write(«<br>»);

// index-3 to index-4
document.write(«<b>Elements from index-3 to index-4: </b> «+tf.slice(values,[3],1));

</script>
 
</body>
</html>

 
Output:

Example 3:

Create a two-dimensional tensor with 5 rows and 4 columns (20 elements) and get the range of values from the row-index2 to row-index3.

<html>
<!—   CDN Link that delivers the Tensorflow.js framework —>
<script src=«https://cdn.jsdelivr.net/npm/@tensorflow/tfjs»></script>
 
<body>
<center><h2>Tensorflow.js – tf.slice() </h2></center>
<script>
 
 //create a tensor
let values = tf.tensor2d([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20],[5,4]);
//presente tensor
document.write(«<b>Coetáneo Scalar: </b>»,values);
 
document.write(«<br>»);
 
// row index-2 to row index-3
document.write(«<b>Elements from row index-2 to row index-3: </b> «+tf.slice(values,[2],2));
document.write(«<br>»);

</script>
 
</body>
</html>

 
Output:


Row index-2 => [9, 10, 11, 12] and Row index-3 => [13, 14, 15, 16].

Conclusion

At the end of this article, we learned that using the tf.slice()can be possible to get a range of elements from a tensor. We specified the three different examples to understand this concept better.

In the Deep Learning using the Tensorflow.js library, we will use this technique to get the image pixels from a particular position.



Source link


“tf.div() in tensorflow.js is used to perform element wise division on two tensors/scalars.”

Scenario 1: Work With Scalar

Scalar will store only one value. But anyway, it returns a tensor.

Syntax

Parameters
scalar1 and scalar2 are the tensors that can take only one value as a parameter.

Return
Return quotient of two scalar values.

Example
Create two scalars and perform a division of two scalars.

<html>
<!–   CDN Link that delivers the Tensorflow.js framework –>
<script src=«https://cdn.jsdelivr.net/npm/@tensorflow/tfjs»></script>

<body>
<script>
//scalar1
let  value1 = tf.scalar(30);

//scalar2
let  value2 = tf.scalar(70);

document.write(«Scalar-1: «,value1);

document.write(«<br>»);
document.write(«<br>»);

document.write(«Scalar-2: «,value2);
</script>
<h3>Tensorflow.js tf.div() </h3>

<script>
//tf.div(value1,value2)
document.write(tf.div(value1,value2));
</script>

</body>
</html>

Output

Working
30/70 = 0.4285714030265808.

Scenario 2: Work With Tensor

A tensor can store multiple values; it can be single or multi-dimensional.

Syntax

Parameters
tensor1 and tensor2 are the tensors that can take only single or multiple values as a parameter.

Return
Return quotient of two tensors concerning each element.

We must notice that the total number of elements in both the tensors must be equal.

Example 1
Create two one-dimensional tensors and return the quotient using tf.div().

<html>
<!–   CDN Link that delivers the Tensorflow.js framework –>
<script src=«https://cdn.jsdelivr.net/npm/@tensorflow/tfjs»></script>

<body>
<script>
//tensor1
let  values1 = tf.tensor1d([10,20,30,40,50]);

//tensor2
let  values2 = tf.tensor1d([1,2,3,4,5]);

document.write(«Tensor-1: «,values1);

document.write(«<br>»);
document.write(«<br>»);

document.write(«Tensor-2: «,values2);
</script>
<h3>Tensorflow.js tf.div() </h3>

<script>
//tf.div(values1,values2)
document.write(tf.div(values1,values2));
</script>

</body>
</html>

Output

Working
[10/1,20/2,30/3,40/4,50/5] => Tensor [10, 10, 10, 10, 10].

Example 2
Create 2 two-dimensional tensors with 2 rows and 3 columns and apply tf.div().

<html>
<!–   CDN Link that delivers the Tensorflow.js framework –>
<script src=«https://cdn.jsdelivr.net/npm/@tensorflow/tfjs»></script>

<body>
<script>
//tensor1
let  values1 = tf.tensor2d([1,2,3,4,5,6],[2,3]);

//tensor2
let  values2 = tf.tensor2d([34,10,20,30,40,50],[2,3]);

document.write(«Tensor-1: «,values1);

document.write(«<br>»);
document.write(«<br>»);

document.write(«Tensor-2: «,values2);
</script>
<h3>Tensorflow.js tf.div() </h3>

<script>
//tf.div(values1,values2)
document.write(tf.div(values1,values2));
</script>

</body>
</html>

Output

Working
[[1/34,2/10,3/20],[4/30,5/40,6/50]] => [[0.0294118, 0.2 , 0.15], [0.1333333, 0.125, 0.12]].

Scenario 3: Work With Tensor & Scalar

It can be possible to divide each element in a tensor by a scalar.

Syntax

Example
Create a one-dimensional tensor and a scalar and perform division using tf.div().

<html>
<!–   CDN Link that delivers the Tensorflow.js framework –>
<script src=«https://cdn.jsdelivr.net/npm/@tensorflow/tfjs»></script>

<body>
<script>
//tensor
let  values1 = tf.tensor1d([10,20,30,4,5,6]);

//scalar
let  value2 = tf.scalar(10);

document.write(«Tensor: «,values1);

document.write(«<br>»);
document.write(«<br>»);

document.write(«Scalar: «,value2);
</script>
<h3>Tensorflow.js tf.div() </h3>

<script>
//tf.div(values1,value2)
document.write(tf.div(values1,value2));
</script>

</body>
</html>

Output

Working
[10/10, 20/10, 30/10, 4/10, 5/10, 6/10] =>  [1, 2, 3, 0.4, 0.5, 0.6].

Conclusion

tf.div() in tensorflow.js is used to perform division and return element-wise quotients. We discussed three scenarios to divide a tensor by a scalar.

Also, we noticed that scalar will store only one value and returns a tensor. While performing tf.div() on two tensors, ensure that the number of elements in two tensors must be the same.



Source link


“tf.greater() returns true if the element in the first tensor is greater than the element in the second tensor. It takes two tensors as parameters that have the same number of values; otherwise, an error is thrown.

Scalar will store only one value. But anyway, it returns a tensor.”

Syntax

tf.greater(tensor1,tensor2)
tf.greater(scalar1,scalar2)

It is also possible to implement the greater() method, as shown below.

Syntax

tensor1.greater(tensor2)
scalar1.greater(scalar2)

Parameters
tensor1 and tensor2 are the tensors that can be single or multi-dimensional.
scalar1 and scalar2 are the tensors that can take only one value as a parameter.

Return
Return a Boolean Tensor.

Example 1
Create two one-dimensional tensors with integer elements and apply tf.greater() to check if the elements in the first tensor are greater than the elements in the second tensor.

<html>
<!–   CDN Link that delivers the Tensorflow.js framework –>
<script src=«https://cdn.jsdelivr.net/npm/@tensorflow/tfjs»></script>

<body>
<script>
//tensor1
let  values1 = tf.tensor1d([100,200,300,500]);

//tensor2
let  values2 = tf.tensor1d([50,345,675,120]);

document.write(«Tensor-1: «,values1);

document.write(«<br>»);
document.write(«<br>»);

document.write(«Tensor-2: «,values2);
</script>
<h3>Tensorflow.js tf.greater(tensor1,tensor2) </h3>

<script>
//tf.greater(values1,values2)
document.write(tf.greater(values1,values2));
</script>

<h3>Tensorflow.js tensor1.greater(tensor2) </h3>
<script>

//values1.greater(values2)
document.write(values1.greater(values2));

</script>
</body>
</html>

Output

Working
Tensor-1: Tensor [100, 200, 300, 500]
Tensor-2: Tensor [50, 345, 675, 120]

Element-wise comparison:
100>50 – true
200>345 – false
300>675 – false
500>120 – true

Example 2
Create two values using scalar() and apply tf.greater() to check if the value is greater than the value present in the second scalar.

<html>
<!–   CDN Link that delivers the Tensorflow.js framework –>
<script src=«https://cdn.jsdelivr.net/npm/@tensorflow/tfjs»></script>

<body>
<script>
//scalar1
let  value1 = tf.scalar(34);

//scalar2
let  value2 = tf.scalar(23);

document.write(«Scalar-1: «,value1);

document.write(«<br>»);
document.write(«<br>»);

document.write(«Scalar-2: «,value2);
</script>
<h3>Tensorflow.js tf.greater(scalar1,scalar2) </h3>

<script>
//tf.greater(value1,value2)
document.write(tf.greater(value1,value2));
</script>

<h3>Tensorflow.js scalar1.greater(scalar2) </h3>
<script>

//value1.greater(value2)
document.write(value1.greater(value2));

</script>
</body>
</html>

Output

34 is greater than  23. So It returned true.

Example 3
Create 2 two-dimensional tensors with 2 rows and 2 columns and apply tf.greater() to check if the elements in the first tensor are greater than the elements in the second tensor.

<html>
<!–   CDN Link that delivers the Tensorflow.js framework –>
<script src=«https://cdn.jsdelivr.net/npm/@tensorflow/tfjs»></script>

<body>
<script>
//tensor1
let  values1 = tf.tensor2d([90,56,78,12],[2,2]);

//tensor2
let  values2 = tf.tensor2d([10,56,34,45],[2,2]);

document.write(«Tensor-1: «,values1);

document.write(«<br>»);
document.write(«<br>»);

document.write(«Tensor-2: «,values2);
</script>
<h3>Tensorflow.js tf.greater(tensor1,tensor2) </h3>

<script>
//tf.greater(values1,values2)
document.write(tf.greater(values1,values2));
</script>

<h3>Tensorflow.js tensor1.greater(tensor2) </h3>
<script>

//values1.greater(values2)
document.write(values1.greater(values2));

</script>
</body>
</html>

Output

Working
Tensor-1: Tensor [[90, 56], [78, 12]]
Tensor-2: Tensor [[10, 56], [34, 45]]

Element-wise comparison:
90>10 – true
56>56 – false
78>34 – true
12>45 – false

Conclusion

tf.greater() in Tensorflow.js is used to compare the elements that return true if the element in the first tensor is greater than the element in the second tensor. It is also possible to implement the greater() method in two ways. We discussed three different examples, using tensors one and two dimensions and scalars.



Source link


“tf.less() returns true if the element in the first tensor is less than the element in the second tensor. It takes two tensors as parameters that have the same number of values; otherwise, an error is thrown.

Scalar will store only one value. But anyway, it returns a tensor.”

Syntax

tf.less(tensor1,tensor2)
tf.less(scalar1,scalar2)

It is also possible to implement the less() method, as shown below.

Syntax

tensor1.less(tensor2)
scalar1.less(scalar2)

Parameters
tensor1 and tensor2 are the tensors that can be single or multi-dimensional.
scalar1 and scalar2 are the tensors that can take only one value as a parameter.

Return
Return a Boolean Tensor.

Example 1
Create two one-dimensional tensors with integer elements and apply tf.less() to check if the elements in the first tensor are less than the elements in the second tensor.

<html>
<!–   CDN Link that delivers the Tensorflow.js framework –>
<script src=«https://cdn.jsdelivr.net/npm/@tensorflow/tfjs»></script>

<body>
<script>
//tensor1
let  values1 = tf.tensor1d([100,200,300,500]);

//tensor2
let  values2 = tf.tensor1d([50,345,675,120]);

document.write(«Tensor-1: «,values1);

document.write(«<br>»);
document.write(«<br>»);

document.write(«Tensor-2: «,values2);
</script>
<h3>Tensorflow.js tf.less(tensor1,tensor2) </h3>

<script>
//tf.less(values1,values2)
document.write(tf.less(values1,values2));
</script>

<h3>Tensorflow.js tensor1.less(tensor2) </h3>
<script>

//values1.less(values2)
document.write(values1.less(values2));

</script>
</body>
</html>

Output

Working
Tensor-1: Tensor [100, 200, 300, 500]
Tensor-2: Tensor [50, 345, 675, 120]

Element-wise comparison:
100<50 – false
200<345 – true
300<675 – true
500<120 – false

Example 2
Create two values using scalar() and apply tf.less() to check if the value is less than the value present in the second scalar.

<html>
<!–   CDN Link that delivers the Tensorflow.js framework –>
<script src=«https://cdn.jsdelivr.net/npm/@tensorflow/tfjs»></script>

<body>
<script>
//scalar1
let  value1 = tf.scalar(34);

//scalar2
let  value2 = tf.scalar(23);

document.write(«Scalar-1: «,value1);

document.write(«<br>»);
document.write(«<br>»);

document.write(«Scalar-2: «,value2);
</script>
<h3>Tensorflow.js tf.less(scalar1,scalar2) </h3>

<script>
//tf.less(value1,value2)
document.write(tf.less(value1,value2));
</script>

<h3>Tensorflow.js scalar1.less(scalar2) </h3>
<script>

//value1.less(value2)
document.write(value1.less(value2));

</script>
</body>
</html>

Output

34 is not less than  23. So It returned false.

Example 3
Create 2 two-dimensional tensors with 2 rows and 2 columns and apply tf.less() to check if the elements in the first tensor are less than the elements in the second tensor.

<html>
<!–   CDN Link that delivers the Tensorflow.js framework –>
<script src=«https://cdn.jsdelivr.net/npm/@tensorflow/tfjs»></script>

<body>
<script>
//tensor1
let  values1 = tf.tensor2d([90,56,78,12],[2,2]);

//tensor2
let  values2 = tf.tensor2d([10,56,34,45],[2,2]);

document.write(«Tensor-1: «,values1);

document.write(«<br>»);
document.write(«<br>»);

document.write(«Tensor-2: «,values2);
</script>
<h3>Tensorflow.js tf.less(tensor1,tensor2) </h3>

<script>
//tf.less(values1,values2)
document.write(tf.less(values1,values2));
</script>

<h3>Tensorflow.js tensor1.less(tensor2) </h3>
<script>

//values1.less(values2)
document.write(values1.less(values2));

</script>
</body>
</html>

Output

Working
Tensor-1: Tensor [[90, 56], [78, 12]]
Tensor-2: Tensor [[10, 56], [34, 45]]

Element-wise comparison:
90<10 – false
56<56 – false
78<34 – false
12<45 – true

Conclusion

tf.less() in Tensorflow.js is used to compare the elements that return true if the element in the first tensor is less than the element in the second tensor. It is also possible to implement the less() method in two ways. We discussed three different examples, using tensors one and two dimensions and scalars.



Source link


“tf.notEqual() returns true if both the elements are not equal; otherwise false is returned. It takes two tensors as parameters that have the same number of values; otherwise, an error is thrown.

Scalar will store only one value. But anyway, it returns a tensor.”

Syntax

tf.notEqual(tensor1,tensor2)
tf.notEqual(scalar1,scalar2)

It is also possible to implement the notEqual() method, as shown below.

Syntax

tensor1.notEqual(tensor2)
scalar1.notEqual(scalar2)

Parameters
tensor1 and tensor2 are the tensors that can be single or multi-dimensional.
scalar1 and scalar2 are the tensors that can take only one value as a parameter.

Return
Return a Boolean Tensor.

Example 1
Create two one-dimensional tensors with integer elements and apply tf.notEqual() to check if the elements are not the same.

<html>
<!–   CDN Link that delivers the Tensorflow.js framework –>
<script src=«https://cdn.jsdelivr.net/npm/@tensorflow/tfjs»></script>

<body>
<script>
//tensor1
let  values1 = tf.tensor1d([34,12,34,11,10,34]);

//tensor2
let  values2 = tf.tensor1d([34,12,2,3,10,23]);

document.write(«Tensor-1: «,values1);

document.write(«<br>»);
document.write(«<br>»);

document.write(«Tensor-2: «,values2);
</script>
<h3>Tensorflow.js tf.notEqual(tensor1,tensor2) </h3>

<script>
//tf.notEqual(values1,values2)
document.write(tf.notEqual(values1,values2));
</script>

<h3>Tensorflow.js tensor1.notEqual(tensor2) </h3>
<script>

//values1.notEqual(values2)
document.write(values1.notEqual(values2));

</script>
</body>
</html>

Output

Working
Tensor-1: Tensor [34, 12, 34, 11, 10, 34]
Tensor-2: Tensor [34, 12, 2, 3, 10, 23]

Element wise comparison:
34!=34 – false
12!=12 – false
34!=2 – true
11!=3 – true
10!=10 – false
34!=23 – true

Example 2
Create two values using scalar() and apply tf.notEqual() to check if the values are the same or not.

<html>
<!–   CDN Link that delivers the Tensorflow.js framework –>
<script src=«https://cdn.jsdelivr.net/npm/@tensorflow/tfjs»></script>

<body>
<script>
//scalar1
let  value1 = tf.scalar(34);

//scalar2
let  value2 = tf.scalar(23);

document.write(«Scalar-1: «,value1);

document.write(«<br>»);
document.write(«<br>»);

document.write(«Scalar-2: «,value2);
</script>
<h3>Tensorflow.js tf.notEqual(scalar1,scalar2) </h3>

<script>
//tf.notEqual(value1,value2)
document.write(tf.notEqual(value1,value2));
</script>

<h3>Tensorflow.js scalar1.notEqual(scalar2) </h3>
<script>

//value1.notEqual(value2)
document.write(value1.notEqual(value2));

</script>
</body>
</html>

Output

34 is not equal to 23. So It returned true.

Example 3
Create 2 two-dimensional tensors with 2 rows and 2 columns and apply tf.notEqual() to check if the elements are the same or not.

<html>
<!–   CDN Link that delivers the Tensorflow.js framework –>
<script src=«https://cdn.jsdelivr.net/npm/@tensorflow/tfjs»></script>

<body>
<script>
//tensor1
let  values1 = tf.tensor2d([90,56,78,12],[2,2]);

//tensor2
let  values2 = tf.tensor2d([90,56,34,45],[2,2]);

document.write(«Tensor-1: «,values1);

document.write(«<br>»);
document.write(«<br>»);

document.write(«Tensor-2: «,values2);
</script>
<h3>Tensorflow.js tf.notEqual(tensor1,tensor2) </h3>

<script>
//tf.notEqual(values1,values2)
document.write(tf.notEqual(values1,values2));
</script>

<h3>Tensorflow.js tensor1.notEqual(tensor2) </h3>
<script>

//values1.notEqual(values2)
document.write(values1.notEqual(values2));

</script>
</body>
</html>

Output

Working

Tensor-1: Tensor [[90, 56], [78, 12]]
Tensor-2: Tensor [[90, 56], [34, 45]]

Element wise comparison:
90!=90 – false
56!=56 – false
78!=34 – true
12!=45 – true

Conclusion

tf.notEqual() in Tensorflow.js is used to compare the elements that return true; if both the elements are not  equal, otherwise false is returned. It takes two tensors as parameters that have the same number of values; otherwise, an error is thrown. It is also possible to implement the notEqual() method in two ways. We discussed three different examples, using tensors one and two dimensions and scalars.



Source link


“tf.lessEqual() returns true if the element in the first tensor is less than or equal to the element in the second tensor. It takes two tensors as parameters that have the same number of values; otherwise, an error is thrown.

Scalar will store only one value. But anyway, it returns a tensor.”

Syntax

tf.lessEqual(tensor1,tensor2)
tf.lessEqual(scalar1,scalar2)

It is also possible to implement the lessEqual() method, as shown below.

Syntax

tensor1.lessEqual(tensor2)
scalar1.lessEqual(scalar2)

Parameters
tensor1 and tensor2 are the tensors that can be single or multi-dimensional.
scalar1 and scalar2 are the tensors that can take only one value as a parameter.

Return
Return a Boolean Tensor.

Example 1
Create two one-dimensional tensors with integer elements and apply tf.lessEqual() to check if the elements in the first tensor are less than or equal to the elements in the second tensor.

<html>
<!–   CDN Link that delivers the Tensorflow.js framework –>
<script src=«https://cdn.jsdelivr.net/npm/@tensorflow/tfjs»></script>

<body>
<script>
//tensor1
let  values1 = tf.tensor1d([100,200,300,500]);

//tensor2
let  values2 = tf.tensor1d([50,345,675,120]);

document.write(«Tensor-1: «,values1);

document.write(«<br>»);
document.write(«<br>»);

document.write(«Tensor-2: «,values2);
</script>
<h3>Tensorflow.js tf.lessEqual(tensor1,tensor2) </h3>

<script>
//tf.lessEqual(values1,values2)
document.write(tf.lessEqual(values1,values2));
</script>

<h3>Tensorflow.js tensor1.lessEqual(tensor2) </h3>
<script>

//values1.lessEqual(values2)
document.write(values1.lessEqual(values2));

</script>
</body>
</html>

Output

Working
Tensor-1: Tensor [100, 200, 300, 500]
Tensor-2: Tensor [50, 345, 675, 120]

Element-wise comparison:
100<=50 – false
200<=345 – true
300<=675 – true
500<=120 – false

Example 2
Create two values using scalar() and apply tf.lessEqual() to check if the value is less than or equal to the value present in the second scalar.

<html>
<!–   CDN Link that delivers the Tensorflow.js framework –>
<script src=«https://cdn.jsdelivr.net/npm/@tensorflow/tfjs»></script>

<body>
<script>
//scalar1
let  value1 = tf.scalar(34);

//scalar2
let  value2 = tf.scalar(23);

document.write(«Scalar-1: «,value1);

document.write(«<br>»);
document.write(«<br>»);

document.write(«Scalar-2: «,value2);
</script>
<h3>Tensorflow.js tf.lessEqual(scalar1,scalar2) </h3>

<script>
//tf.lessEqual(value1,value2)
document.write(tf.lessEqual(value1,value2));
</script>

<h3>Tensorflow.js scalar1.lessEqual(scalar2) </h3>
<script>

//value1.lessEqual(value2)
document.write(value1.lessEqual(value2));

</script>
</body>
</html>

Output

34 is not less than or equal to  23. So It returned false.

Example 3
Create 2 two-dimensional tensors with 2 rows and 2 columns and apply tf.lessEqual() to check if the elements in the first tensor are less than or equal to the elements in the second tensor.

<html>
<!–   CDN Link that delivers the Tensorflow.js framework –>
<script src=«https://cdn.jsdelivr.net/npm/@tensorflow/tfjs»></script>

<body>
<script>
//tensor1
let  values1 = tf.tensor2d([90,56,78,12],[2,2]);

//tensor2
let  values2 = tf.tensor2d([10,56,34,45],[2,2]);

document.write(«Tensor-1: «,values1);

document.write(«<br>»);
document.write(«<br>»);

document.write(«Tensor-2: «,values2);
</script>
<h3>Tensorflow.js tf.lessEqual(tensor1,tensor2) </h3>

<script>
//tf.lessEqual(values1,values2)
document.write(tf.lessEqual(values1,values2));
</script>

<h3>Tensorflow.js tensor1.lessEqual(tensor2) </h3>
<script>

//values1.lessEqual(values2)
document.write(values1.lessEqual(values2));

</script>
</body>
</html>

Output

Working
Tensor-1: Tensor [[90, 56], [78, 12]]
Tensor-2: Tensor [[10, 56], [34, 45]]

Element-wise comparison:
90<=10 – false
56<=56 – true
78<=34 – false
12<=45 – true

Conclusion

tf.lessEqual() in Tensorflow.js is used to compare the elements that return true if the element in the first tensor is less than or equal to the element in the second tensor. It is also possible to implement the lessEqual() method in two ways. We discussed three different examples, using tensors one and two dimensions and scalars.



Source link