PyTorch ---- torch.nn.function.pad 函数用法(补充维度上的数值)

1.二维数组:对最内部元素左侧增加元素(例如 1 的左侧)

a = torch.tensor([[1, 2, 3, 4], [1, 2, 3, 4]])
a1 = torch.nn.functional.pad(a, pad=(1, 0, 0, 0), mode='constant', value=1)
print("a = ", a)
print("a1 = ", a1)

运行结果:

a =  tensor([[1, 2, 3, 4],
        [1, 2, 3, 4]])
a1 =  tensor([[1, 1, 2, 3, 4],
        [1, 1, 2, 3, 4]])

2.二维数组:对最内部元素右侧增加元素(例如 4 右侧)

a = torch.tensor([[1, 2, 3, 4], [1, 2, 3, 4]])
a1 = torch.nn.functional.pad(a, pad=(0, 1, 0, 0), mode='constant', value=1)
print("a = ", a)
print("a1 = ", a1)

运行结果:

a =  tensor([[1, 2, 3, 4],
        [1, 2, 3, 4]])
a1 =  tensor([[1, 2, 3, 4, 1],
        [1, 2, 3, 4, 1]])

3.二维数组:对最内部一维数组左侧增加元素(例如 [1, 2, 3, 4] 左侧)

a = torch.tensor([[1, 2, 3, 4], [1, 2, 3, 4]])
a1 = torch.nn.functional.pad(a, pad=(0, 0, 1, 0), mode='constant', value=1)
print("a = ", a)
print("a1 = ", a1)

运行结果:

a =  tensor([[1, 2, 3, 4],
        [1, 2, 3, 4]])
a1 =  tensor([[1, 1, 1, 1],
        [1, 2, 3, 4],
        [1, 2, 3, 4]])

4.二维数组:对最内部一维数组右侧增加元素(例如 [1, 2, 3, 4] 右侧)

a = torch.tensor([[1, 2, 3, 4], [1, 2, 3, 4]])
a1 = torch.nn.functional.pad(a, pad=(0, 0, 0, 1), mode='constant', value=1)
print("a = ", a)
print("a1 = ", a1)

运行结果:

a =  tensor([[1, 2, 3, 4],
        [1, 2, 3, 4]])
a1 =  tensor([[1, 2, 3, 4],
        [1, 2, 3, 4],
        [1, 1, 1, 1]])

5.三维数组:对最内部元素左侧增加元素(例如 1 左侧)

a = torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]], [[1, 2, 3, 4], [5, 6, 7, 8]]])
a1 = torch.nn.functional.pad(a, pad=(1, 0, 0, 0, 0, 0), mode='constant', value=1)
print("a = ", a)
print("a1 = ", a1)

运行结果:

a =  tensor([[[1, 2, 3, 4],
         [5, 6, 7, 8]],


        [[1, 2, 3, 4],
         [5, 6, 7, 8]]])
a1 =  tensor([[[1, 1, 2, 3, 4],
         [1, 5, 6, 7, 8]],


        [[1, 1, 2, 3, 4],
         [1, 5, 6, 7, 8]]])

6.三维数组:对最内部元素右侧增加元素(例如 4 右侧)

a = torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]], [[1, 2, 3, 4], [5, 6, 7, 8]]])
a1 = torch.nn.functional.pad(a, pad=(0, 1, 0, 0, 0, 0), mode='constant', value=1)
print("a = ", a)
print("a1 = ", a1)

运行结果:

a =  tensor([[[1, 2, 3, 4],
         [5, 6, 7, 8]],


        [[1, 2, 3, 4],
         [5, 6, 7, 8]]])
a1 =  tensor([[[1, 2, 3, 4, 1],
         [5, 6, 7, 8, 1]],


        [[1, 2, 3, 4, 1],
         [5, 6, 7, 8, 1]]])

7.三维数组:对最内部一维数组左侧增加元素(例如 [1, 2, 3, 4] 左侧)

a = torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]], [[1, 2, 3, 4], [5, 6, 7, 8]]])
a1 = torch.nn.functional.pad(a, pad=(0, 0, 1, 0, 0, 0), mode='constant', value=1)
print("a = ", a)
print("a1 = ", a1)

运行结果:

a =  tensor([[[1, 2, 3, 4],
         [5, 6, 7, 8]],


        [[1, 2, 3, 4],
         [5, 6, 7, 8]]])
a1 =  tensor([[[1, 1, 1, 1],
         [1, 2, 3, 4],
         [5, 6, 7, 8]],


        [[1, 1, 1, 1],
         [1, 2, 3, 4],
         [5, 6, 7, 8]]])

8.三维数组:对最内部一维数组右侧增加元素(例如 [5, 6, 7, 8] 右侧)

a = torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]], [[1, 2, 3, 4], [5, 6, 7, 8]]])
a1 = torch.nn.functional.pad(a, pad=(0, 0, 0, 1, 0, 0), mode='constant', value=1)
print("a = ", a)
print("a1 = ", a1)

运行结果:

a =  tensor([[[1, 2, 3, 4],
         [5, 6, 7, 8]],
        [[1, 2, 3, 4],
         [5, 6, 7, 8]]])
a1 =  tensor([[[1, 2, 3, 4],
         [5, 6, 7, 8],
         [1, 1, 1, 1]],
        [[1, 2, 3, 4],
         [5, 6, 7, 8],
         [1, 1, 1, 1]]])

9.三维数组:对最内部二维数组左侧增加元素(例如 [[1, 2, 3, 4], [5, 6, 7, 8]] 左侧)

a = torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]], [[1, 2, 3, 4], [5, 6, 7, 8]]])
a1 = torch.nn.functional.pad(a, pad=(0, 0, 0, 0, 1, 0), mode='constant', value=1)
print("a = ", a)
print("a1 = ", a1)
运行结果:

a =  tensor([[[1, 2, 3, 4],
         [5, 6, 7, 8]],


        [[1, 2, 3, 4],
         [5, 6, 7, 8]]])
a1 =  tensor([[[1, 1, 1, 1],
         [1, 1, 1, 1]],


        [[1, 2, 3, 4],
         [5, 6, 7, 8]],


        [[1, 2, 3, 4],
         [5, 6, 7, 8]]])

10.三维数组:对最内部二维数组左侧增加元素 x2(例如 [[1, 2, 3, 4], [5, 6, 7, 8]] 左侧)

a = torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]], [[1, 2, 3, 4], [5, 6, 7, 8]]])
a1 = torch.nn.functional.pad(a, pad=(0, 0, 0, 0, 2, 0), mode='constant', value=1)
print("a = ", a)
print("a1 = ", a1)

运行结果:

a =  tensor([[[1, 2, 3, 4],
         [5, 6, 7, 8]],


        [[1, 2, 3, 4],
         [5, 6, 7, 8]]])
a1 =  tensor([[[1, 1, 1, 1],
         [1, 1, 1, 1]],


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


        [[1, 2, 3, 4],
         [5, 6, 7, 8]],


        [[1, 2, 3, 4],
         [5, 6, 7, 8]]])

11.三维数组:对最内部二维数组右侧增加元素 (例如 [[11, 22, 33, 44], [55, 66, 77, 88]] 右侧)

a = torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]], [[11, 22, 33, 44], [55, 66, 77, 88]]])
a1 = torch.nn.functional.pad(a, pad=(0, 0, 0, 0, 0, 1), mode='constant', value=1)
print("a = ", a)
print("a1 = ", a1)

运行结果:

a =  tensor([[[ 1,  2,  3,  4],
         [ 5,  6,  7,  8]],


        [[11, 22, 33, 44],
         [55, 66, 77, 88]]])
a1 =  tensor([[[ 1,  2,  3,  4],
         [ 5,  6,  7,  8]],


        [[11, 22, 33, 44],
         [55, 66, 77, 88]],


        [[ 1,  1,  1,  1],
         [ 1,  1,  1,  1]]])

12.三维数组:对最内部二维数组右侧增加元素 x2 (例如 [[11, 22, 33, 44], [55, 66, 77, 88]] 右侧)

a = torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]], [[11, 22, 33, 44], [55, 66, 77, 88]]])
a1 = torch.nn.functional.pad(a, pad=(0, 0, 0, 0, 0, 2), mode='constant', value=1)
print("a = ", a)
print("a1 = ", a1)

运行结果:

a =  tensor([[[ 1,  2,  3,  4],
         [ 5,  6,  7,  8]],


        [[11, 22, 33, 44],
         [55, 66, 77, 88]]])
a1 =  tensor([[[ 1,  2,  3,  4],
         [ 5,  6,  7,  8]],


        [[11, 22, 33, 44],
         [55, 66, 77, 88]],


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


        [[ 1,  1,  1,  1],
         [ 1,  1,  1,  1]]])

你可能感兴趣的:(PyTorch ---- torch.nn.function.pad 函数用法(补充维度上的数值))