Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.meshgrid function

PyTorch torch.meshgrid function


PyTorch torch reference manual PyTorch torch reference manual

torch.meshgrid is a function in PyTorch used to create a grid from coordinate vectors. It generates coordinate tensors from multiple one-dimensional coordinate vectors.

Function definition

torch.meshgrid(*tensors, indexing='xy')

Parameter Description:


Usage examples

Example

import

torch

Create coordinate vectors

x

=

torch.

tensor

(

[

1

,

2

,

3

]

)

y

=

torch.

tensor

(

[

4

,

5

]

)

Create a grid (Cartesian indexing)

grid_x

,

grid_y

=

torch.

meshgrid

(

x

,

y

,

indexing

=

'xy'

)

print

(

"grid_x:"

)

print

(

grid_x

)

print

(

"grid_y:"

)

print

(

grid_y

)

The output is:

grid_x:

tensor([[1, 2, 3],

        [1, 2, 3]])

grid_y:

tensor([[4, 4, 4],

        [5, 5, 5]])

Example

import

torch

Create coordinate vectors

x

=

torch.

tensor

(

[

1

,

2

,

3

]

)

y

=

torch.

tensor

(

[

4

,

5

]

)

Create a grid (matrix indexing)

grid_x

,

grid_y

=

torch.

meshgrid

(

x

,

y

,

indexing

=

'ij'

)

print

(

"grid_x:"

)

print

(

grid_x

)

print

(

"grid_y:"

)

print

(

grid_y

)

The output is:

grid_x:

tensor([[1, 1],

        [2, 2],

        [3, 3]])

grid_y:

tensor([[4, 5],

        [4, 5],

        [4, 5]])


PyTorch torch reference manual PyTorch torch reference manual