Creates grids of coordinates specified by the 1D inputs in attr:tensors.
This is helpful when you want to visualize data over some
range of inputs. See below for a plotting example.
Given N 1D tensors T0…TN−1 as
inputs with corresponding sizes ,
this creates N-dimensional tensors , each with shape where
the output is constructed by expanding
to the result shape.
S0…SN−1
N
G0…GN−1
(S0,...,SN−1)
Gi
Ti
Parameters:
tensors (list of Tensor) – list of scalars or 1 dimensional tensors. Scalars will be
treated as tensors of size (1,) automatically
(str, optional): the indexing mode, either “xy”
or “ij”, defaults to “ij”. See warning for future changes.
If “xy” is selected, the first dimension corresponds
to the cardinality of the second input and the second
dimension corresponds to the cardinality of the first
input.
If “ij” is selected, the dimensions are in the same
order as the cardinality of the inputs.
Returns:
If the input has N
tensors of size S0…SN−1‘, then the
output will also have N tensors, where each tensor
is of shape .
Return type:
seq (sequence of Tensors)
Example:
>>> x=torch.tensor([1,2,3])>>> y=torch.tensor([4,5,6])Observe the element-wise pairings across the grid, (1, 4),(1, 5), ..., (3, 6). This is the same thing as thecartesian product.>>> grid_x,grid_y=torch.meshgrid(x,y,indexing='ij')>>> grid_xtensor([[1, 1, 1], [2, 2, 2], [3, 3, 3]])>>> grid_ytensor([[4, 5, 6], [4, 5, 6], [4, 5, 6]])This correspondence can be seen when these grids arestacked properly.>>> torch.equal(torch.cat(tuple(torch.dstack([grid_x,grid_y]))),... torch.cartesian_prod(x,y))True`torch.meshgrid` is commonly used to produce a grid forplotting.>>> importmatplotlib.pyplotasplt>>> xs=torch.linspace(-5,5,steps=100)>>> ys=torch.linspace(-5,5,steps=100)>>> x,y=torch.meshgrid(xs,ys,indexing='xy')>>> z=torch.sin(torch.sqrt(x*x+y*y))>>> ax=plt.axes(projection='3d')>>> ax.plot_surface(x.numpy(),y.numpy(),z.numpy())>>> plt.show()