天天看點

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