Beispiel #1
0
    def forward(self, input):
        x = F.leaky_relu(self.cbam1(self.conv1(input)), 0.2)
        x = F.dropout(x, 0.5)
        x = F.leaky_relu(self.cbam2(self.conv2_bn(self.conv2(x))), 0.2)
        x = F.dropout(x, 0.5)
        x = F.leaky_relu(self.cbam3(self.conv3_bn(self.conv3(x))), 0.2)
        x = torch.sigmoid(self.conv4(x)).squeeze()

        return x
Beispiel #2
0
 def forward(self, x):
     x = self.avgpooling(x)
     x = self.conv(x)
     x = torch.flatten(x, start_dim=1)
     x = F.dropout(x, 0.5, training=self.training)
     x = F.relu(self.fc1(x), inplace=True)
     x = F.dropout(x, 0.5, training=self.training)
     x = self.fc2(x)
     return x
Beispiel #3
0
    def forward(self, input):
        x = input
        # This is a hack to get around the fact that the inception net
        # provided by torchvision does not provide an attribute of max_pool
        # in the inception net constructor
        for name, module in self.inception_net.named_children():
            if name == 'Conv2d_2b_3x3':
                x = self.inception_net.Conv2d_2b_3x3(x)
                x = F.max_pool2d(x, kernel_size=3, stride=2)
                continue

            if name == 'Conv2d_4a_3x3':
                x = self.inception_net.Conv2d_4a_3x3(x)
                x = F.max_pool2d(x, kernel_size=3, stride=2)
                continue
            x = module(x)

        # Adaptive average pooling for inceptionV3
        x = F.adaptive_avg_pool2d(x, (1, 1))
        # N x 2048 x 1 x 1
        x = F.dropout(x, training=self.training)
        # N x 2048 x 1 x 1
        x = torch.flatten(x, 1)
        # N x 2048
        v_out = self.output(self.vowel_output(x))
        c_out = self.output(self.consonants_output(x))

        return v_out, c_out
Beispiel #4
0
 def forward(self, x):
     x = F.relu(F.max_pool2d(self.conv1(x), 2))
     x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
     x = x.view(-1, 320)
     x = F.relu(self.fc1(x))
     feature = F.dropout(x, training=self.training)
     return self.fc2(feature), feature
Beispiel #5
0
    def forward(self, x1, x1_f, x1_pos, x1_ner, x1_mask, x2, x2_mask):
        """Inputs:
        x1 = document word indices             [batch * len_d]
        x1_f = document word features indices  [batch * len_d * nfeat]
        x1_pos = document POS tags             [batch * len_d]
        x1_ner = document entity tags          [batch * len_d]
        x1_mask = document padding mask        [batch * len_d]
        x2 = question word indices             [batch * len_q]
        x2_mask = question padding mask        [batch * len_q]
        """

        x1_emb = self.embedding(x1)
        x2_emb = self.embedding(x2)

        # Dropout on embeddings
        if self.opt['dropout_emb'] > 0:
            x1_emb = F.dropout(x1_emb, p=self.opt['dropout_emb'],
                               training=self.training)
            x2_emb = F.dropout(x2_emb, p=self.opt['dropout_emb'],
                               training=self.training)

        # Context input 만들기
        drnn_input_list = [x1_emb, x1_f]
        if self.opt['use_qemb']:
            x2_weighted_emb = self.qemb_match(x1_emb, x2_emb, x2_mask)
            drnn_input_list.append(x2_weighted_emb)
        if self.opt['pos']:
            drnn_input_list.append(x1_pos)
        if self.opt['ner']:
            drnn_input_list.append(x1_ner)
        drnn_input = torch.cat(drnn_input_list, 2)

        # Context - Bi-LSTM 3Layers
        doc_hiddens = self.doc_rnn(drnn_input, x1_mask)

        # Qeustion - Bi-LSTM 3Layers, 중요한 토큰에 가중
        question_hiddens = self.question_rnn(x2_emb, x2_mask)
        if self.opt['question_merge'] == 'avg':
            q_merge_weights = layers.uniform_weights(question_hiddens, x2_mask)
        elif self.opt['question_merge'] == 'self_attn':
            q_merge_weights = self.self_attn(question_hiddens, x2_mask)
        question_attn_hidden = layers.weighted_avg(question_hiddens, q_merge_weights)

        # start/end points 예측
        start_scores = self.start_attn(doc_hiddens, question_attn_hidden, x1_mask)
        end_scores = self.end_attn(doc_hiddens, question_attn_hidden, x1_mask)
        return start_scores, end_scores
Beispiel #6
0
 def _setWeights(self):
     """if we want to preserve the CuDNN speed and not reimplement the cell from scratch.
     We add a parameter that will contain the raw weights,
     and we replace the weight matrix in the LSTM at the beginning of the forward pass."""
     for layer in self.layer_names:
         raw_w = getattr(self, f'{layer}_raw')
         self.module._parameters[layer] = F.dropout(raw_w,
                                                    p=self.weight_pro,
                                                    training=self.training)
Beispiel #7
0
    def forward(self, x):
        # transform the input
        x = self.stn(x)

        # Perform the usual forward pass
        x = F.relu(F.max_pool2d(self.conv1(x), 2))
        x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
        x = x.view(-1, 320)
        x = F.relu(self.fc1(x))
        x = F.dropout(x, training=self.training)
        x = self.fc2(x)
        return F.log_softmax(x, dim=1)
def Conv1d(inputs, conv, batch_norm, is_training, activation=None):
    # the Conv1d of pytroch chanages the channels at the 1 dim
    # [batch_size, max_time, feature_dims] -> [batch_size, feature_dims, max_time]
    inputs = torch.transpose(inputs, 1, 2)
    conv1d_output = conv(inputs)
    batch_norm_output = batch_norm(conv1d_output)
    batch_norm_output = torch.transpose(batch_norm_output, 1, 2)
    if activation is not None:
        batch_norm_output = activation(batch_norm_output)
    return F.dropout(batch_norm_output,
                     p=hp.dropout_rate,
                     training=is_training)
Beispiel #9
0
 def __init__(self, module, weight_prob=[0.], layer_names=[WEIGHT_HH]):
     super().__init__()
     self.module = module
     self.weight_prob = weight_prob
     self.layer_names = layer_names
     for layer in self.layer_names:
         # Makes a copy of the weights of the selected layers.
         w = getattr(self.module, layer)
         self.register_parameter(f'{layer}_raw', nn.Parameter(w.data))
         self.module._parameters[layer] = F.dropout(w,
                                                    p=self.weight_prob,
                                                    training=False)
    def forward(self, samples, **kwargs):
        head = samples[:, 0].long()
        rel = samples[:, 1].long()
        tail = samples[:, 2].long()
        year = samples[:, 3]
        month = samples[:, 4]
        day = samples[:, 5]

        h_emb1, r_emb1, t_emb1, h_emb2, r_emb2, t_emb2 = self.get_embedding(
            head, rel, tail, year, month, day)

        p = self.config.get('model.dropout')

        scores = ((h_emb1 * r_emb1) * t_emb1 +
                  (h_emb2 * r_emb2) * t_emb2) / 2.0
        scores = F.dropout(scores, p=p,
                           training=self.training)  # TODO training
        scores = torch.sum(scores, dim=1)

        return scores, None