TensorFlow – How to broadcasts parameters for evaluation on an N-D grid
Last Updated :
10 Feb, 2022
TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning neural networks.
While working with TensorFlow some operations automatically broadcast parameters and sometimes we have to explicitly broadcast parameters. To explicitly broadcast parameters meshgrid method is used.
Method Used:
- meshgrid: This method is used to broadcasts parameters for evaluation on an N-D grid. It accepts rank-1 tensors and broadcast all of them to same shape and returns a list of N Tensors with rank N. Default indexing for this method is ‘xy’.
Example 1: In this method default indexing is used.
Python3
import tensorflow as tf
x = [ 1 , 2 , 3 ]
y = [ 4 , 5 , 6 , 7 ]
print ( "x: " , x)
print ( "y: " , y)
X, Y = tf.meshgrid(x, y)
print ( "X: " , X)
print ( "Y: " , Y)
|
Output:
x: [1, 2, 3]
y: [4, 5, 6, 7]
X: tf.Tensor(
[[1 2 3]
[1 2 3]
[1 2 3]
[1 2 3]], shape=(4, 3), dtype=int32)
Y: tf.Tensor(
[[4 4 4]
[5 5 5]
[6 6 6]
[7 7 7]], shape=(4, 3), dtype=int32)
Example 2: In this example indexing is changed to ‘ij’.
Python3
import tensorflow as tf
x = [ 1 , 2 , 3 ]
y = [ 4 , 5 , 6 , 7 ]
print ( "x: " , x)
print ( "y: " , y)
X, Y = tf.meshgrid(x, y, indexing = 'ij' )
print ( "X: " , X)
print ( "Y: " , Y)
|
Output:
x: [1, 2, 3]
y: [4, 5, 6, 7]
X: tf.Tensor(
[[1 1 1 1]
[2 2 2 2]
[3 3 3 3]], shape=(3, 4), dtype=int32)
Y: tf.Tensor(
[[4 5 6 7]
[4 5 6 7]
[4 5 6 7]], shape=(3, 4), dtype=int32)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...