torch.functional.meshgrid#
- torch.functional.meshgrid(*tensors, indexing=None)[源码]#
创建由 attr:tensors 中一维输入指定的坐标网格。
这对于您想要可视化某些输入范围上的数据时非常有用。请参见下面的绘图示例。
给定 个 1D 张量 作为输入,其对应的尺寸为 ,此函数会创建 个 N 维张量 ,每个张量的形状为 ,其中输出 是通过将 扩展到结果形状而创建的。
注意
0D 输入被等同于具有单个元素的 1D 输入。
警告
torch.meshgrid(*tensors) 当前的行为与调用 numpy.meshgrid(*arrays, indexing=’ij’) 相同。
未来 torch.meshgrid 将默认过渡到 indexing=’xy’。
pytorch/pytorch#50276 跟踪此问题,目标是迁移到 NumPy 的行为。
另请参阅
torch.cartesian_prod()
具有相同的效果,但它以张量向量的形式收集数据。- 参数
- 返回
如果输入有 个尺寸为 的张量,则输出也将有 个张量,其中每个张量的形状为 。
- 返回类型
seq (Tensors序列)
示例
>>> 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 the cartesian product. >>> grid_x, grid_y = torch.meshgrid(x, y, indexing='ij') >>> grid_x tensor([[1, 1, 1], [2, 2, 2], [3, 3, 3]]) >>> grid_y tensor([[4, 5, 6], [4, 5, 6], [4, 5, 6]]) This correspondence can be seen when these grids are stacked 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 for plotting. >>> import matplotlib.pyplot as plt >>> 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()