Search code examples
deep-learningpytorchconcatenationconv-neural-networkdropout

How can I concatenate an additional input data in Alexnet with the output of the last dropout layer using Pytorch implementation?


Here is the implementation architecture

class AlexNet(nn.Module):
def __init__(self, num_classes=10):
    super(AlexNet, self).__init__()
    #1
    self.features= nn.Sequential(
    nn.Conv2d(3, 96, kernel_size=11, stride=4, padding=0),
    nn.ReLU(inplace=True),
    nn.MaxPool2d(kernel_size=3, stride=2),
    #2
    nn.Conv2d(96, 256, kernel_size=5, stride=1, padding=2),
    nn.ReLU(inplace=True),
    nn.MaxPool2d(kernel_size=3, stride=2),
    #3
    nn.Conv2d(256, 384, kernel_size=3, stride=1, padding=1),
    nn.ReLU(inplace=True),
    #4
    nn.Conv2d(384, 384, kernel_size=3, stride=1, padding=1),
    nn.ReLU(inplace=True),
    #5
    nn.Conv2d(384, 256, kernel_size=5, stride=1, padding=2),
    nn.ReLU(inplace=True),
    nn.MaxPool2d(kernel_size=3, stride=2),
    )
    self.avgpool= nn.AvgPool2d(6)
    self.classifier= nn.Sequential(
        nn.Dropout(), nn.Linear(256*6*6, 4096), #128*2*2, 1024
    nn.ReLU(inplace=True), nn.Dropout(),torch.cat((nn.Dropout(),PIs_features)),
    nn.Linear(4096, num_classes))
    
def forward(self, x):
    x= self.features(x)
    x=x.view(x.size(0), 256*6*6)
    x= self.classifier(x)
    return x

So I wanna implement say 'y' input data with the output of the last dropout layer 'nn.dropout()' in the self.classifier.

Thanks in advance.


Solution

  • You can do so in the forward definition by simply calling torch.cat((x, y), 1) to concatenate the two feature vectors together.

    class AlexNet(nn.Module):
      def __init__(self, num_classes=10):
          super().__init__()
            #1
          self.features= nn.Sequential(
            nn.Conv2d(3, 96, kernel_size=11, stride=4, padding=0),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            #2
            nn.Conv2d(96, 256, kernel_size=5, stride=1, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            #3
            nn.Conv2d(256, 384, kernel_size=3, stride=1, padding=1),
            nn.ReLU(inplace=True),
            #4
            nn.Conv2d(384, 384, kernel_size=3, stride=1, padding=1),
            nn.ReLU(inplace=True),
            #5
            nn.Conv2d(384, 256, kernel_size=5, stride=1, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2))
          
          self.avgpool= nn.AvgPool2d(6)
          self.classifier= nn.Sequential(
              nn.Dropout(), 
              nn.LazyLinear(4096),
              nn.ReLU(inplace=True), 
              nn.Dropout())
          
          self.fc = nn.LazyLinear(num_classes)
          
      def forward(self, x, y):
          x = self.features(x)
          x = self.avgpool(x)
          x = x.flatten(1)
          x = torch.cat((x, y), 1)
          x = self.classifier(x)
          return x
    

    Additionally, I have replaced the fully connected nn.Linear layers with LazyLayer. But you can replace them with fixed neurons if you prefer.