torch.meshgrid¶
- torch.meshgrid(*tensors, indexing=None)[原始碼][原始碼]¶
根據 attr:tensors 中的一維輸入建立座標網格。
當你想在某個輸入範圍內視覺化資料時,這很有用。請參閱下方繪圖示例。
給定 個一維張量 作為輸入,其對應大小分別為 ,這將建立 個 N 維張量 ,每個張量的形狀為 ,其中輸出 透過將 擴充套件到結果形狀來構建。
注意
0D 輸入被視為具有單個元素的一維輸入。
警告
torch.meshgrid(*tensors) 當前行為與呼叫 numpy.meshgrid(*arrays, indexing=’ij’) 相同。
將來,torch.meshgrid 將把 indexing=’xy’ 作為預設值。
https://github.com/pytorch/pytorch/issues/50276 跟蹤此問題,目標是遷移到 NumPy 的行為。
另請參閱
torch.cartesian_prod()效果相同,但它將資料收集在一個向量張量中。- 引數
- 返回
如果輸入包含 個大小為 的張量,則輸出也將包含 個張量,其中每個張量的形狀為 。
- 返回型別
seq (張量序列)
示例
>>> 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()