天天看点

torch常用操作—— torch.nn.functional.padtorch.nn.functional.pad

torch.nn.functional.pad

作用:

填充tensor

目前为止,只支持2d和3d填充

用法:

torch.nn.functional.pad(input, pad, mode='constant', value=0)[source]
           

input

需要填充的tensor

pad

决定填充的方向

以下面的例子观察可知:

pad为tuple (pad_l, pad_r, pad_t, pad_b )

其中四个元素的位置代表了填充的位置,大小为填充的行数,如果需要填充指定数值的话,可以指定

values

为你想要的数据

code1:

import torch
import torch.nn.functional as F
a = torch.rand(1, 3, 3)
print(a)
b = F.pad(a, [0, 0, 0, 1])
print(b)
           

output:

torch常用操作—— torch.nn.functional.padtorch.nn.functional.pad

code2:

import torch
import torch.nn.functional as F
a = torch.rand(1, 3, 3)
print(a)
b = F.pad(a, [0, 0, 1, 0])
print(b)
           

output:

torch常用操作—— torch.nn.functional.padtorch.nn.functional.pad

code3:

import torch
import torch.nn.functional as F
a = torch.rand(1, 3, 3)
print(a)
b = F.pad(a, [0, 1, 0, 0])
print(b)
           

output:

torch常用操作—— torch.nn.functional.padtorch.nn.functional.pad

code4:

import torch
import torch.nn.functional as F
a = torch.rand(1, 3, 3)
print(a)
b = F.pad(a, [1, 0, 0, 0])
print(b)
           

output:

torch常用操作—— torch.nn.functional.padtorch.nn.functional.pad