# Module `models.vgg_cifar`

VGG11/13/16/19 in Pytorch.

Expand source code

    #!/usr/bin/env python3

    """
    VGG11/13/16/19 in Pytorch.
    """
    import torch.nn as nn

    cfg = {
        'VGG11': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
        'VGG13': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
        'VGG16': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'],
        'VGG19': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'],
    }

    class VGG(nn.Module):
        def __init__(self, vgg_name, num_classes=10):
            super(VGG, self).__init__()
            self.features = self._make_layers(cfg[vgg_name])
            self.classifier = nn.Linear(512, num_classes)

        def forward(self, x):
            out = self.features(x)
            out = out.view(out.size(0), -1)
            out = self.classifier(out)
            return out

        def _make_layers(self, cfg):
            layers = []
            in_channels = 3
            for x in cfg:
                if x == 'M':
                    layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
                else:
                    layers += [nn.Conv2d(in_channels, x, kernel_size=3, padding=1),
                               nn.BatchNorm2d(x),
                               nn.ReLU(inplace=True)]
                    in_channels = x
            layers += [nn.AvgPool2d(kernel_size=1, stride=1)]
            return nn.Sequential(*layers)

    # TODO: Any way we can actually have an useful pretrained argument here?
    def vgg11(pretrained=False, num_classes=10):
        return VGG('VGG11', num_classes=num_classes)

    def vgg13(pretrained=False, num_classes=10):
        return VGG('VGG13', num_classes=num_classes)

    def vgg16(pretrained=False, num_classes=10):
        return VGG('VGG16', num_classes=num_classes)

    def vgg19(pretrained=False, num_classes=10):
        return VGG('VGG19', num_classes=num_classes)

## Functions

`def vgg11(pretrained=False, num_classes=10)`
  Expand source code

      def vgg11(pretrained=False, num_classes=10):
          return VGG('VGG11', num_classes=num_classes)

`def vgg13(pretrained=False, num_classes=10)`
  Expand source code

      def vgg13(pretrained=False, num_classes=10):
          return VGG('VGG13', num_classes=num_classes)

`def vgg16(pretrained=False, num_classes=10)`
  Expand source code

      def vgg16(pretrained=False, num_classes=10):
          return VGG('VGG16', num_classes=num_classes)

`def vgg19(pretrained=False, num_classes=10)`
  Expand source code

      def vgg19(pretrained=False, num_classes=10):
          return VGG('VGG19', num_classes=num_classes)

## Classes

`class VGG (vgg_name, num_classes=10)`

  Base class for all neural network modules.

  Your models should also subclass this class.

  Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes::

      import torch.nn as nn
      import torch.nn.functional as F

      class Model(nn.Module):
          def __init__(self):
              super(Model, self).__init__()
              self.conv1 = nn.Conv2d(1, 20, 5)
              self.conv2 = nn.Conv2d(20, 20, 5)

          def forward(self, x):
              x = F.relu(self.conv1(x))
              return F.relu(self.conv2(x))

  Submodules assigned in this way will be registered, and will have their parameters converted too when you call :meth:`to`, etc.

  :ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool

  Initializes internal Module state, shared by both nn.Module and ScriptModule.

  Expand source code

      class VGG(nn.Module):
          def __init__(self, vgg_name, num_classes=10):
              super(VGG, self).__init__()
              self.features = self._make_layers(cfg[vgg_name])
              self.classifier = nn.Linear(512, num_classes)

          def forward(self, x):
              out = self.features(x)
              out = out.view(out.size(0), -1)
              out = self.classifier(out)
              return out

          def _make_layers(self, cfg):
              layers = []
              in_channels = 3
              for x in cfg:
                  if x == 'M':
                      layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
                  else:
                      layers += [nn.Conv2d(in_channels, x, kernel_size=3, padding=1),
                                 nn.BatchNorm2d(x),
                                 nn.ReLU(inplace=True)]
                      in_channels = x
              layers += [nn.AvgPool2d(kernel_size=1, stride=1)]
              return nn.Sequential(*layers)

  ### Ancestors

  - torch.nn.modules.module.Module

  ### Class variables

  `var dump_patches : bool`
  `var training : bool`

  ### Methods

  `def forward(self, x) ‑> Callable[..., Any]`

    Defines the computation performed at every call.

    Should be overridden by all subclasses.

    Note

    Although the recipe for forward pass needs to be defined within this function, one should call the :class:`Module` instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

    Expand source code

        def forward(self, x):
            out = self.features(x)
            out = out.view(out.size(0), -1)
            out = self.classifier(out)
            return out