JavaScript

Tensorflow.js – tf.outerProduct()

The tf.outerProduct() in Tensorflow.js is used to return an outer product performed on two tensor objects.

Computation:

Each element in the first tensor is multiplied with all the elements in the second tensor.

Consider the tensors – [1,2,3] and [2,3,4]:

1* [2,3,4]=> [2, 3, 4 ]

2* [2,3,4]=> [4, 6, 8 ]

3* [2,3,4]=> [6, 9, 12]

Syntax:

tf.outerProduct(tensor1,tensor2)

Parameter:

  1. Tensor1 is the first tensor with numeric values.
  2. Tensor2 is the first tensor with numeric values.

Example 1:

Create two tensors with 4 elements each and return the outer product.

<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.outerProduct()</h2></center>
<script>

//create two tensors
let tensor1 = tf.tensor1d([10,20,30,40]);
let tensor2 = tf.tensor1d([1,2,3,4]);

//actual tensor
document.write("<br>Actual Tensor-1:</br> ",tensor1);
document.write("<br>");
document.write("<br>Actual Tensor-2:</br> ",tensor2);
document.write("<br>");
document.write("<br>Outer Product:</br>
"
,tf.outerProduct(tensor1,tensor2));

</script>

</body>
</html>

Output:

Working:

1
2
3
4
10* [1,2,3,4] => [10, 20, 30, 40 ]
20* [1,2,3,4] => [20, 40, 60, 80 ]
30* [1,2,3,4] => [30, 60, 90, 120]
40* [1,2,3,4] => [40, 80, 120, 160].

Example 2:

Create two tensors with 8 elements each and return the outer product.

<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.outerProduct()</h2></center>
<script>

//create two tensors
let tensor1 = tf.tensor1d([10,21,34,56,78,90,43,5]);
let tensor2 = tf.tensor1d([0,-1,2,3,4,5,6,7]);

//actual tensor
document.write("<br>Actual Tensor-1:</br> ",tensor1);
document.write("<br>");
document.write("<br>Actual Tensor-2:</br> ",tensor2);
document.write("<br>");
document.write("<br>Outer Product:</br>
"
,tf.outerProduct(tensor1,tensor2));

</script>

</body>
</html>

Output:

Working:

1
2
3
4
5
6
7
8
10 * [0, -1, 2, 3, 4, 5, 6, 7] => [0, -10, 20 , 30 , 40 , 50 , 60 , 70 ]
21 * [0, -1, 2, 3, 4, 5, 6, 7]  => [0, -21, 42 , 63 , 84 , 105, 126, 147]
34 * [0, -1, 2, 3, 4, 5, 6, 7] => [0, -34, 68 , 102, 136, 170, 204, 238]
56 * [0, -1, 2, 3, 4, 5, 6, 7] => [0, -56, 112, 168, 224, 280, 336, 392]
78 * [0, -1, 2, 3, 4, 5, 6, 7] => [0, -78, 156, 234, 312, 390, 468, 546]
90 * [0, -1, 2, 3, 4, 5, 6, 7] => [0, -90, 180, 270, 360, 450, 540, 630]
43 * [0, -1, 2, 3, 4, 5, 6, 7] => [0, -43, 86 , 129, 172, 215, 258, 301]
5 * [0, -1, 2, 3, 4, 5, 6, 7] => [0, -5 , 10 , 15 , 20 , 25 , 30 , 35 ]

Conclusion

In this Tensorflow.js tutorial, we learned how to perform the outer product operation on tensors using the tf.outerProduct() function. Each element in the first tensor is multiplied with all the elements in the second tensor. Make sure that both tensors have equal number of elements. Otherwise, the computation is not performed.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain