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

PyTorch torch.tile function

PyTorch torch.tile function


PyTorch torch reference manual PyTorch torch reference manual

torch.tile is a function in PyTorch used to repeat a tensor multiple times.

Function definition

torch.tile(input, dims)


Usage examples

Example

import

torch

Repeat a one-dimensional tensor

x

=

torch.

tensor

(

[

1

,

2

,

3

]

)

print

(

"Original 1D tensor:"

)

print

(

x

)

result

=

torch.

tile

(

x

,

3

)

print

(

"Repeated 3 times:"

)

print

(

result

)

Repeat a 2D tensor

y

=

torch.

tensor

(

[

[

1

,

2

]

,

[

3

,

4

]

]

)

print

(

"nOriginal 2D tensor:"

)

print

(

y

)

result

=

torch.

tile

(

y

,

(

2

,

3

)

)

print

(

"Repeated 2 times along rows and 3 times along columns:"

)

print

(

result

)

Repeat a 3D tensor

z

=

torch.

tensor

(

[

[

[

1

,

2

]

,

[

3

,

4

]

]

]

)

print

(

"nOriginal 3D tensor:"

)

print

(

z

)

result

=

torch.

tile

(

z

,

(

2

,

1

,

2

)

)

print

(

"Repeated with shape [2, 1, 2]:"

)

print

(

result

)

print

(

f

"Shape: {result.shape}"

)

The output is:

原始一维张量:

tensor([1, 2, 3])

Repeat 3 times:

tensor([1, 2, 3, 1, 2, 3, 1, 2, 3])



Original two-dimensional tensor:

tensor([[1, 2],

        [3, 4]])

Repeat 2 times along the row, repeat 3 times along the column:

tensor([[1, 2, 1, 2, 1, 2],

        [3, 4, 3, 4, 3, 4],

        [1, 2, 1, 2, 1, 2],

        [3, 4, 3, 4, 3, 4]])



Original three-dimensional tensor:

tensor([[[1, 2],

         [3, 4]]])

Shape [2, 1, 2] repeated:

tensor([[[1, 2, 1, 2],

         [3, 4, 3, 4]],



        [[1, 2, 1, 2],

         [3, 4, 3, 4]]])

形状: torch.Size([2, 2, 4])


PyTorch torch reference manual PyTorch torch reference manual