Pytorch的常规组件
1.最大池化操作
import torch
import torch.nn as nn
import torch.nn.functional as F
m = nn.MaxPool2d(3, stride=2) #卷积核大小为3,步长为2
input = torch.randn(2,3,5,5) #batch_size,通道数,输入图像的size
print(input)
output = m(input)
print(output)
结果:
input:
tensor([[[[-0.2409, 0.7836, 0.1113, -1.1855, -0.3730],
[-0.6965, -0.9836, -0.5080, -0.9810, 0.4161],
[ 0.1138, -0.1593, -1.2696, 1.3774, -1.1309],
[-1.4596, 0.7006, 0.5704, 0.3858, -0.3996],
[-0.7248, -0.2028, -1.4492, 0.5397, 0.3330]],
[[ 0.8961, -0.6415, -0.2588, -1.2093, 0.7093],
[-1.2154, -0.7305, -0.3681, -0.4590, -1.1295],
[ 0.4562, 1.7638, -0.3772, 0.4643, 0.4107],
[-0.1303, -0.4466, 0.5162, -0.4200, -0.2190],
[-2.1213, 2.6915, 0.1852, -0.1598, 1.7159]],
[[ 0.0591, 1.0129, -0.7358, 1.3021, 1.2605],
[-0.4197, -0.4806, 0.2806, 1.8006, -0.6805],
[-1.5624, -0.4796, -1.1439, -0.2285, 0.3684],
[ 0.4258, -0.0192, 0.0949, 0.3739, 0.6403],
[-0.4932, -0.4713, -0.4298, 0.3945, -0.0246]]],
[[[-0.4580, -1.3090, -0.3775, -0.4790, 0.3332],
[-0.8751, 1.7364, -0.2541, 0.3684, -1.6448],
[-0.1632, -1.0534, 1.5021, -0.6321, 0.7377],
[-0.1905, -0.5582, -0.3769, 0.3073, 0.5226],
[-1.0129, -0.6852, 0.9519, 2.3748, 0.0715]],
[[-0.2448, 2.1534, -0.2194, 0.4034, 0.4436],
[ 1.3946, 0.1010, -1.9442, 0.9813, 0.6020],
[-0.0188, 0.4220, -0.8636, 0.6154, 0.4873],
[-0.5662, 0.0843, -1.0924, -0.0550, -0.1799],
[-1.0691, -0.1877, 0.2009, 1.3235, 0.1997]],
[[ 0.7056, 3.2700, -0.4490, -0.6675, -0.2758],
[-0.8802, -1.0851, 0.3877, -0.7076, 1.1639],
[-0.2761, -0.6234, -1.3386, -0.3939, 0.5892],
[ 0.8873, -0.3749, -2.1443, -0.1076, 0.2879],
[ 0.8976, -0.1897, 0.2131, -0.8082, -0.6531]]]])
output:
tensor([[[[0.7836, 1.3774],
[0.7006, 1.3774]],
[[1.7638, 0.7093],
[2.6915, 1.7159]],
[[1.0129, 1.8006],
[0.4258, 0.6403]]],
[[[1.7364, 1.5021],
[1.5021, 2.3748]],
[[2.1534, 0.9813],
[0.4220, 1.3235]],
[[3.2700, 1.1639],
[0.8976, 0.5892]]]])
2.上采样操作
import torch
import torch.nn as nn
import torch.nn.functional as F
input = torch.randn(1,1,2,2)
print(input)
m = nn.UpsamplingNearest2d(scale_factor=3) #最邻近插值上采样
z = m(input)
print(z)
y=torch.range(1,6) #生成浮点型1-6的tensor序列
print(y)
print(y.dtype)
z1=torch.arange(1,6) #生成整型1-5的tensor序列
print(z1)
print(z1.dtype)
input1 = torch.arange(1, 5, dtype=torch.float32).view(1, 1, 2, 2)
print(input1)
m = nn.UpsamplingBilinear2d(scale_factor=3) #双线性插值上采样
z2 = m(input)
print(z2)
结果:
input:
tensor([[[[-0.1819, -0.1995],
[-1.2164, 0.3258]]]])
z:
tensor([[[[-0.1819, -0.1819, -0.1819, -0.1995, -0.1995, -0.1995],
[-0.1819, -0.1819, -0.1819, -0.1995, -0.1995, -0.1995],
[-0.1819, -0.1819, -0.1819, -0.1995, -0.1995, -0.1995],
[-1.2164, -1.2164, -1.2164, 0.3258, 0.3258, 0.3258],
[-1.2164, -1.2164, -1.2164, 0.3258, 0.3258, 0.3258],
[-1.2164, -1.2164, -1.2164, 0.3258, 0.3258, 0.3258]]]])
y:
tensor([1., 2., 3., 4., 5., 6.])
torch.float32
z1:
tensor([1, 2, 3, 4, 5])
torch.int64
input1:
tensor([[[[1., 2.],
[3., 4.]]]])
z2:
tensor([[[[1.0000, 1.2000, 1.4000, 1.6000, 1.8000, 2.0000],
[1.4000, 1.6000, 1.8000, 2.0000, 2.2000, 2.4000],
[1.8000, 2.0000, 2.2000, 2.4000, 2.6000, 2.8000],
[2.2000, 2.4000, 2.6000, 2.8000, 3.0000, 3.2000],
[2.6000, 2.8000, 3.0000, 3.2000, 3.4000, 3.6000],
[3.0000, 3.2000, 3.4000, 3.6000, 3.8000, 4.0000]]]])
3.卷积操作
import torch
in_channels,out_channels=5,10
width,height=100,100
kernel_size=3
batch_size=1
input=torch.randn(batch_size,in_channels,width,height)
conv_layer=torch.nn.Conv2d(in_channels,out_channels,kernel_size=kernel_size) #不填充
output=conv_layer(input)
print(input.shape)
print(output.shape)
print(conv_layer.weight.shape)
结果:
input.shape:
torch.Size([1, 5, 100, 100])
output.shape:
torch.Size([1, 10, 98, 98])
conv_layer.weight.shape:
torch.Size([10, 5, 3, 3])
4.ReLU激活函数
import torch
import torch.nn as nn
import torch.nn.functional as F
#激活函数
m = nn.ReLU()
input = torch.randn(2,2)
print(input)
output = m(input)
print(output)
结果:
input:
tensor([[ 0.0532, -0.9213],
[-0.3774, 0.1108]])
output:
tensor([[0.0532, 0.0000],
[0.0000, 0.1108]])