Exemple #1
0
    def __init__(self,
                 n_words,
                 emb_dim,
                 hidden_dim,
                 seq_length,
                 bidirectional=False,
                 supervised=False):
        super(RNN, self).__init__()

        self.n_words = n_words
        self.emb_dim = emb_dim
        self.hidden_dim = hidden_dim
        self.bidirectional = bidirectional
        self.supervised = supervised
        self.seq_length = seq_length
        self.embedding = nn.Embedding(n_words, emb_dim)
        self.GRU = nn.GRU(emb_dim, hidden_dim)
        self.linear = nn.Linear(seq_length * hidden_dim, hidden_dim)
        self.attention = nn.Linear(emb_dim, 1)
        self.softmax = nn.Softmax()

        if bidirectional == True:
            self.GRU = nn.GRU(emb_dim, hidden_dim, bidirectional=True)
            self.linear = nn.Linear(2 * seq_length * hidden_dim, hidden_dim)

        if self.supervised == True:
            self.linear2 = nn.Linear(hidden_dim, 2)
            self.sm = nn.LogSoftMax()

        return
Exemple #2
0
    def __init__(self, num_classes=10):
        super(VGG_Cifar10, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(3, 128, kernel_size=3, stride=1, padding=1, bias=False),
            nn.BatchNorm2d(128),
            nn.ReLU(inplace=True),
            nn.Conv2d(128, 128, kernel_size=3, padding=1, bias=False),
            nn.MaxPool2d(kernel_size=2, stride=2),
            nn.ReLU(inplace=True),
            nn.BatchNorm2d(128),
            nn.Conv2d(128, 256, kernel_size=3, padding=1, bias=False),
            nn.ReLU(inplace=True),
            nn.BatchNorm2d(256),
            nn.Conv2d(256, 256, kernel_size=3, padding=1, bias=False),
            nn.MaxPool2d(kernel_size=2, stride=2),
            nn.ReLU(inplace=True),
            nn.BatchNorm2d(256),
            nn.Conv2d(256, 512, kernel_size=3, padding=1, bias=False),
            nn.ReLU(inplace=True),
            nn.BatchNorm2d(512),
            nn.Conv2d(512, 512, kernel_size=3, padding=1, bias=False),
            nn.MaxPool2d(kernel_size=2, stride=2),
            nn.ReLU(inplace=True),
            nn.BatchNorm2d(512),
        )
        self.classifier = nn.Sequential(
            nn.Linear(512 * 4 * 4, 1024, bias=False),
            nn.BatchNorm1d(1024),
            nn.ReLU(inplace=True),
            # nn.Dropout(0.5),
            nn.Linear(1024, 1024, bias=False),
            nn.BatchNorm1d(1024),
            nn.ReLU(inplace=True),
            # nn.Dropout(0.5),
            nn.Linear(1024, num_classes),
            nn.BatchNorm1d(num_classes, affine=False),
            nn.LogSoftMax())

        self.regime = {
            0: {
                'optimizer': 'SGD',
                'lr': 1e-2,
                'weight_decay': 5e-4,
                'momentum': 0.9
            },
            10: {
                'lr': 5e-3
            },
            15: {
                'lr': 1e-3,
                'weight_decay': 0
            },
            20: {
                'lr': 5e-4
            },
            25: {
                'lr': 1e-4
            }
        }
Exemple #3
0
 def __init__(self, self, ratio_code, num_classes=10):
     super(VGG_Cifar10, self).__init__()
     in_channels = [3, 128, 128, 256, 256, 512]
     out_channels = [128, 128, 256, 256, 512, 512]
     for i in range(6):
         if i != 5:
             in_channels[i + 1] = int(in_channels[i + 1] * ratio_code[i])
         out_channels[i] = int(out_channels[i] * ratio_code[i])
     self.in_planes = int(512 * 4 * 4 * ratio_code[5])
     self.features = nn.Sequential(
         nn.Conv2d(in_channels[0],
                   out_channels[0],
                   kernel_size=3,
                   stride=1,
                   padding=1,
                   bias=False),
         nn.BatchNorm2d(out_channels[0]),
         Conv(in_channels[1],
              out_channels[1],
              kernel_size=3,
              padding=1,
              bias=False),
         nn.MaxPool2d(kernel_size=2, stride=2),
         nn.BatchNorm2d(out_channels[1]),
         Conv(in_channels[2],
              out_channels[2],
              kernel_size=3,
              padding=1,
              bias=False),
         nn.BatchNorm2d(out_channels[2]),
         Conv(in_channels[3],
              out_channels[3],
              kernel_size=3,
              padding=1,
              bias=False),
         nn.MaxPool2d(kernel_size=2, stride=2),
         nn.BatchNorm2d(out_channels[3]),
         Conv(in_channels[4],
              out_channels[4],
              kernel_size=3,
              padding=1,
              bias=False),
         nn.BatchNorm2d(out_channels[4]),
         Conv(in_channels[5],
              out_channels[5],
              kernel_size=3,
              padding=1,
              bias=False),
         nn.MaxPool2d(kernel_size=2, stride=2),
         nn.BatchNorm2d(out_channels[5]),
     )
     self.classifier = nn.Sequential(
         nn.Linear(self.in_planes, 1024, bias=False), nn.LogSoftMax())