zoukankan      html  css  js  c++  java
  • 动手学深度学习PyTorch版-task06








    稠密块

    def conv_block(in_channels, out_channels):
        blk = nn.Sequential(nn.BatchNorm2d(in_channels), 
                            nn.ReLU(),
                            nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1))
        return blk
    
    class DenseBlock(nn.Module):
        def __init__(self, num_convs, in_channels, out_channels):
            super(DenseBlock, self).__init__()
            net = []
            for i in range(num_convs):
                in_c = in_channels + i * out_channels
                net.append(conv_block(in_c, out_channels))
            self.net = nn.ModuleList(net)
            self.out_channels = in_channels + num_convs * out_channels # 计算输出通道数
    
        def forward(self, X):
            for blk in self.net:
                Y = blk(X)
                X = torch.cat((X, Y), dim=1)  # 在通道维上将输入和输出连结
            return X
    
    • 运行
    blk = DenseBlock(2, 3, 10)
    X = torch.rand(4, 3, 8, 8)
    Y = blk(X)
    Y.shape # torch.Size([4, 23, 8, 8])
    
    • 输出
      torch.Size([4, 23, 8, 8])

  • 相关阅读:
    订餐系统
    throw和throws
    CF999E Solution
    CF1142B Solution
    CF965C Solution
    CF963B Solution
    CF999F Solution
    CF975D Solution
    CF997B Solution
    hdu 2553 N皇后
  • 原文地址:https://www.cnblogs.com/HongjianChen/p/12364090.html
Copyright © 2011-2022 走看看