Pytorch-Padding Layers

发布于:2024-06-16 ⋅ 阅读:(13) ⋅ 点赞:(0)


1.nn.ReflectionPad2d()

torch.nn.ReflectionPad2d() 是 PyTorch 中的一个模块,用于执行二维反射填充操作。反射填充是一种常用的填充方法,它在输入张量的边界周围以对称方式复制边缘值来扩展输入的尺寸。

torch.nn.ReflectionPad2d(padding)
"""
参数:padding:指定填充的大小。可以是一个整数,表示在每个边界上应用相同的填充大小。也可以是一个长度为 4 的元组 (pad_left, pad_right, pad_top, pad_bottom),分别表示左、右、上、下四个方向的填充大小。
返回值:返回一个执行反射填充操作的模块。
"""
import torch
import torch.nn as nn

# 创建一个输入大小为 (batch_size=1, channels=3, height=4, width=4) 的张量
input_tensor = torch.randn(1, 3, 4, 4)

# 创建一个反射填充层,指定填充大小为 1
reflection_pad = nn.ReflectionPad2d(1)

# 应用反射填充操作
output = reflection_pad(input_tensor)

print("Input shape:", input_tensor.shape)
print("Output shape:", output.shape)
print("Output tensor:")
print(output)
Input shape: torch.Size([1, 3, 4, 4])
Output shape: torch.Size([1, 3, 6, 6])
Output tensor:
tensor([[[[-0.4726, -0.9318, -0.4726,  0.0358, -1.3965,  0.0358],
          [ 0.6456, -0.0962,  0.6456,  0.1090,  1.0219,  0.1090],
          [-0.4726, -0.9318, -0.4726,  0.0358, -1.3965,  0.0358],
          [ 2.1055,  0.1147,  2.1055, -2.0517,  1.6568, -2.0517],
          [ 0.2304,  0.2164,  0.2304, -0.8812, -0.2803, -0.8812],
          [ 2.1055,  0.1147,  2.1055, -2.0517,  1.6568, -2.0517]],

         [[ 0.3650,  0.5981,  0.3650,  0.2102,  1.7905,  0.2102],
          [-0.0200, -0.0328, -0.0200,  0.2043, -0.0937,  0.2043],
          [ 0.3650,  0.5981,  0.3650,  0.2102,  1.7905,  0.2102],
          [ 0.5632,  1.2948,  0.5632, -0.0395,  0.5825, -0.0395],
          [ 0.7248, -0.5541,  0.7248, -0.2897,  0.5524, -0.2897],
          [ 0.5632,  1.2948,  0.5632, -0.0395,  0.5825, -0.0395]],

         [[ 1.3675, -1.0789,  1.3675,  0.1664,  0.4726,  0.1664],
          [ 0.5879, -1.2825,  0.5879, -0.0681, -1.1589, -0.0681],
          [ 1.3675, -1.0789,  1.3675,  0.1664,  0.4726,  0.1664],
          [-0.2540, -0.5877, -0.2540,  0.3633, -0.5502,  0.3633],
          [-1.0074,  0.4622, -1.0074, -0.0378,  0.8484, -0.0378],
          [-0.2540, -0.5877, -0.2540,  0.3633, -0.5502,  0.3633]]]])

Process finished with exit code 0

2.torch.nn.ReplicationPad2d()

torch.nn.ReplicationPad2d() 是 PyTorch 中的一个模块,用于执行二维复制填充操作。复制填充是一种常用的填充方法,它在输入张量的边界周围以复制边缘值的方式进行填充,以扩展输入的尺寸。

torch.nn.ReplicationPad2d(padding)
"""
参数:
padding:指定填充的大小。可以是一个整数,表示在每个边界上应用相同的填充大小。也可以是一个长度为 4 的元组 (pad_left, pad_right, pad_top, pad_bottom),分别表示左、右、上、下四个方向的填充大小。
返回值:返回一个执行复制填充操作的模块。
"""
import torch
import torch.nn as nn

# 创建一个输入大小为 (batch_size=1, channels=3, height=4, width=4) 的张量
input_tensor = torch.randn(1, 3, 4, 4)

# 创建一个复制填充层,指定填充大小为 1
replication_pad = nn.ReplicationPad2d(1)

# 应用复制填充操作
output = replication_pad(input_tensor)

print("Input shape:", input_tensor.shape)
print("Output shape:", output.shape)
print("Output tensor:")
print(output)
Input shape: torch.Size([1, 3, 4, 4])
Output shape: torch.Size([1, 3, 6, 6])
Output tensor:
tensor([[[[-0.8918, -0.8918,  0.8248, -1.1001,  0.5579,  0.5579],
          [-0.8918, -0.8918,  0.8248, -1.1001,  0.5579,  0.5579],
          [-0.9271, -0.9271,  0.3101, -2.0943, -0.5726, -0.5726],
          [ 0.9032,  0.9032, -1.2071, -0.7429,  0.0117,  0.0117],
          [-0.6993, -0.6993,  0.6873,  0.4220,  0.0609,  0.0609],
          [-0.6993, -0.6993,  0.6873,  0.4220,  0.0609,  0.0609]],

         [[-1.0946, -1.0946,  1.0368, -0.6620, -2.5191, -2.5191],
          [-1.0946, -1.0946,  1.0368, -0.6620, -2.5191, -2.5191],
          [-0.7896, -0.7896, -0.3133,  1.6266,  0.4542,  0.4542],
          [ 1.5996,  1.5996,  0.5224, -0.5475,  0.8748,  0.8748],
          [ 1.0462,  1.0462,  0.4509,  0.7843, -1.5898, -1.5898],
          [ 1.0462,  1.0462,  0.4509,  0.7843, -1.5898, -1.5898]],

         [[ 0.4897,  0.4897, -1.4179,  0.7020,  0.1468,  0.1468],
          [ 0.4897,  0.4897, -1.4179,  0.7020,  0.1468,  0.1468],
          [-0.9953, -0.9953,  0.4198,  0.7341,  1.1312,  1.1312],
          [ 0.2715,  0.2715,  0.4392, -0.6059,  0.9667,  0.9667],
          [-2.0218, -2.0218, -1.4344,  1.3383,  0.0892,  0.0892],
          [-2.0218, -2.0218, -1.4344,  1.3383,  0.0892,  0.0892]]]])

Process finished with exit code 0

3.torch.nn.ZeroPad2d()

torch.nn.ZeroPad2d() 是 PyTorch 中的一个模块,用于执行二维零填充操作。零填充是一种常用的填充方法,它在输入张量的边界周围以零值填充来扩展输入的尺寸。

torch.nn.ZeroPad2d(padding)
"""
参数:
padding:指定填充的大小。可以是一个整数,表示在每个边界上应用相同的填充大小。也可以是一个长度为 4 的元组 (pad_left, pad_right, pad_top, pad_bottom),分别表示左、右、上、下四个方向的填充大小。
返回值:返回一个执行零填充操作的模块。
"""
import torch
import torch.nn as nn

# 创建一个输入大小为 (batch_size=1, channels=3, height=4, width=4) 的张量
input_tensor = torch.randn(1, 3, 4, 4)

# 创建一个零填充层,指定填充大小为 1
zero_pad = nn.ZeroPad2d(2)

# 应用零填充操作
output = zero_pad(input_tensor)

print("Input shape:", input_tensor.shape)
print("Output shape:", output.shape)
print("Output tensor:")
print(output)
Input shape: torch.Size([1, 3, 4, 4])
Output shape: torch.Size([1, 3, 8, 8])
Output tensor:
tensor([[[[ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,
            0.0000],
          [ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,
            0.0000],
          [ 0.0000,  0.0000,  1.0102,  1.2083,  1.6256,  0.3018,  0.0000,
            0.0000],
          [ 0.0000,  0.0000,  0.2626,  1.6651,  0.8745,  0.6719,  0.0000,
            0.0000],
          [ 0.0000,  0.0000, -0.3205, -0.2860,  0.6161,  1.1064,  0.0000,
            0.0000],
          [ 0.0000,  0.0000,  0.5857, -0.1732,  1.7060,  1.2323,  0.0000,
            0.0000],
          [ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,
            0.0000],
          [ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,
            0.0000]],

         [[ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,
            0.0000],
          [ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,
            0.0000],
          [ 0.0000,  0.0000,  0.8972, -0.5355, -0.4680, -0.9288,  0.0000,
            0.0000],
          [ 0.0000,  0.0000,  0.0075,  0.5603,  1.7817, -0.8891,  0.0000,
            0.0000],
          [ 0.0000,  0.0000,  1.0890,  0.4493, -0.6523, -0.0255,  0.0000,
            0.0000],
          [ 0.0000,  0.0000,  0.3344, -1.0198, -1.8107, -1.3935,  0.0000,
            0.0000],
          [ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,
            0.0000],
          [ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,
            0.0000]],

         [[ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,
            0.0000],
          [ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,
            0.0000],
          [ 0.0000,  0.0000, -1.5016, -0.2146,  0.6888, -1.0898,  0.0000,
            0.0000],
          [ 0.0000,  0.0000, -0.7264, -2.0722,  0.0202, -0.4005,  0.0000,
            0.0000],
          [ 0.0000,  0.0000, -0.0256,  0.5652, -0.3572,  1.6135,  0.0000,
            0.0000],
          [ 0.0000,  0.0000,  0.0191,  1.1539,  1.0094, -0.6924,  0.0000,
            0.0000],
          [ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,
            0.0000],
          [ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000,
            0.0000]]]])

Process finished with exit code 0

4.torch.nn.ConstantPad2d()

torch.nn.ConstantPad2d() 是 PyTorch 中的一个模块,用于执行二维常数填充操作。常数填充是一种常用的填充方法,它在输入张量的边界周围以给定的常数值进行填充,以扩展输入的尺寸。

torch.nn.ConstantPad2d(padding, value)
"""
参数:
padding:指定填充的大小。可以是一个整数,表示在每个边界上应用相同的填充大小。也可以是一个长度为 4 的元组 (pad_left, pad_right, pad_top, pad_bottom),分别表示左、右、上、下四个方向的填充大小。
value:指定填充的常数值。
返回值:返回一个执行常数填充操作的模块。
"""
import torch
import torch.nn as nn

# 创建一个输入大小为 (batch_size=1, channels=3, height=4, width=4) 的张量
input_tensor = torch.randn(1, 3, 4, 4)

# 创建一个常数填充层,指定填充大小为 1,填充值为 0.5
constant_pad = nn.ConstantPad2d(1, 0.5)

# 应用常数填充操作
output = constant_pad(input_tensor)

print("Input shape:", input_tensor.shape)
print("Output shape:", output.shape)
print("Output tensor:")
print(output)
Input shape: torch.Size([1, 3, 4, 4])
Output shape: torch.Size([1, 3, 6, 6])
Output tensor:
tensor([[[[ 0.5000,  0.5000,  0.5000,  0.5000,  0.5000,  0.5000],
          [ 0.5000,  0.1896,  0.6049,  1.2257, -0.0834,  0.5000],
          [ 0.5000, -1.4060,  1.2950,  0.1327,  0.7166,  0.5000],
          [ 0.5000, -0.3263,  0.5647,  1.0163, -0.1992,  0.5000],
          [ 0.5000,  2.1257, -0.0110,  1.0363,  0.8082,  0.5000],
          [ 0.5000,  0.5000,  0.5000,  0.5000,  0.5000,  0.5000]],

         [[ 0.5000,  0.5000,  0.5000,  0.5000,  0.5000,  0.5000],
          [ 0.5000,  0.2015, -0.7174, -0.1867,  0.0453,  0.5000],
          [ 0.5000,  0.3571, -2.7764,  1.8158, -0.3598,  0.5000],
          [ 0.5000, -0.9038,  0.7168, -1.8930, -0.1428,  0.5000],
          [ 0.5000, -0.3898, -0.3794, -1.7476,  0.2262,  0.5000],
          [ 0.5000,  0.5000,  0.5000,  0.5000,  0.5000,  0.5000]],

         [[ 0.5000,  0.5000,  0.5000,  0.5000,  0.5000,  0.5000],
          [ 0.5000,  1.9865,  1.1290, -0.5834, -1.1631,  0.5000],
          [ 0.5000,  0.9054, -0.3848,  0.6861, -0.7824,  0.5000],
          [ 0.5000, -0.2373, -1.3416,  0.0430, -0.8926,  0.5000],
          [ 0.5000, -0.2171,  1.5384,  0.2721, -2.4094,  0.5000],
          [ 0.5000,  0.5000,  0.5000,  0.5000,  0.5000,  0.5000]]]])

Process finished with exit code 0


网站公告

今日签到

点亮在社区的每一天
去签到