pytorch学习(九)—基本的层layers

作者: 侠之大者_7d3f | 来源:发表于2018-12-25 20:46 被阅读2次

卷积神经网络常见的层

类型 名称 作用
Conv 卷积层 提取特征
ReLU 激活层 激活
Pool 池化 ——
BatchNorm 批量归一化 ——
Linear(Full Connect) 全连接层 ——
Dropout —— ——
ConvTranspose 反卷积 ——

pytorch中各种层的用法

卷积 Convolution

参考:https://pytorch.org/docs/stable/_modules/torch/nn/modules/conv.html#Conv1d

卷积类型 作用
torrch.nn.Conv1d 一维卷积
torch.nn.Conv2d 二维卷积
torch.nn.Conv3d 三维卷积
torch.nn.ConvTranspose1d 一维反卷积
torch.nn.ConvTranspose2d 二维反卷积
torch.nn.ConvTranspose3d 三维反卷积

示例:

m = nn.Conv1d(in_channels=16,
              out_channels=33,
              kernel_size=3,
              padding=1,
              stride=1)
input = torch.randn(1, 16, 50)
output = m(input)
print(output.size())

# nn.Conv2d()
m = nn.Conv2d(in_channels=16,
              out_channels=33,
              kernel_size=3,
              stride=2)
input = torch.randn(20, 16, 50, 100)
output = m(input)
print(output.size())


m = nn.Conv2d(in_channels=16,
              out_channels=33,
              kernel_size=(3, 5),
              stride=(2, 1),
              padding=(4, 2),
              dilation=(3, 1))
output = m(input)
print(output.size())

# nn.ConvTranspose2d()
# 2d transpose convolution operator
m = nn.ConvTranspose2d(in_channels=16,
                       out_channels=33,
                       kernel_size=3,
                       stride=2)
input = torch.randn(20, 16, 50, 100)
output = m(input)   # (20,33,101,201)
print(output.size())

# exact output size can be also specified as an argument
input = torch.randn(1, 16, 12, 12)
downsample = nn.Conv2d(in_channels=16,
                       out_channels=16,
                       kernel_size=3,
                       stride=2,
                       padding=1)
upsample = nn.ConvTranspose2d(in_channels=16,
                              out_channels=16,
                              kernel_size=3,
                              stride=2,
                              padding=1,
                              output_padding=1)
output = downsample(input)  # 下采样, (1x16x6x6)
print(output.size())

output = upsample(output)   # 上采样, (1x16x12x12)
print(output.size())


输出:


image.png

相关文章

网友评论

    本文标题:pytorch学习(九)—基本的层layers

    本文链接:https://www.haomeiwen.com/subject/nmsqlqtx.html