当前位置 主页 > 网站技术 > 代码类 >

    浅谈pytorch卷积核大小的设置对全连接神经元的影响

    栏目:代码类 时间:2020-01-10 15:07

    3*3卷积核与2*5卷积核对神经元大小的设置

    #这里kerner_size = 2*5
    class CONV_NET(torch.nn.Module): #CONV_NET类继承nn.Module类
     def __init__(self):
      super(CONV_NET, self).__init__() #使CONV_NET类包含父类nn.Module的所有属性
      # super()需要两个实参,子类名和对象self
      self.conv1 = nn.Conv2d(1, 32, (2, 5), 1, padding=0)
      self.conv2 = nn.Conv2d(32, 128, 1, 1, padding=0)
      self.fc1 = nn.Linear(512, 128)
      self.relu1 = nn.ReLU(inplace=True)
      self.drop1 = nn.Dropout(0.5)
      self.fc2 = nn.Linear(128, 32)
      self.relu2 = nn.ReLU(inplace=True)
      self.fc3 = nn.Linear(32, 3)
      self.softmax = nn.Softmax(dim=1)
    
     def forward(self, x):
      x = self.conv1(x)
      x = self.conv2(x)
      x = x.view(x.size(0), -1)
      x = self.fc1(x)
      x = self.relu1(x)
      x = self.drop1(x)
      x = self.fc2(x)
      x = self.relu2(x)
      x = self.fc3(x)
      x = self.softmax(x)
      return x
    

    主要看对称卷积核以及非对称卷积核之间的计算方式

    #这里kerner_size = 3*3
    class CONV_NET(torch.nn.Module): #CONV_NET类继承nn.Module类
     def __init__(self):
      super(CONV_NET, self).__init__() #使CONV_NET类包含父类nn.Module的所有属性
      # super()需要两个实参,子类名和对象self
      self.conv1 = nn.Conv2d(1, 32, 3, 1, padding=1)
      self.conv2 = nn.Conv2d(32, 128, 1, 1, padding=0)
      self.fc1 = nn.Linear(3200, 128)
      self.relu1 = nn.ReLU(inplace=True)
      self.drop1 = nn.Dropout(0.5)
      self.fc2 = nn.Linear(128, 32)
      self.relu2 = nn.ReLU(inplace=True)
      self.fc3 = nn.Linear(32, 3)
      self.softmax = nn.Softmax(dim=1)
    
     def forward(self, x):
      x = self.conv1(x)
      x = self.conv2(x)
      x = x.view(x.size(0), -1)
      x = self.fc1(x)
      x = self.relu1(x)
      x = self.drop1(x)
      x = self.fc2(x)
      x = self.relu2(x)
      x = self.fc3(x)
      x = self.softmax(x)
      return x
    

    针对kerner_size=2*5,padding=0,stride=1以及kerner_size=3*3,padding=1,stride=1二者计算方式的比较如图所示

    以上这篇浅谈pytorch卷积核大小的设置对全连接神经元的影响就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持IIS7站长之家。