Python torch.nn.functional.adaptive_max_pool1d() Examples

The following are 6 code examples of torch.nn.functional.adaptive_max_pool1d(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module torch.nn.functional , or try the search function .
Example #1
Source File: main.py    From HUAWEIOCR-2019 with MIT License 6 votes vote down vote up
def forward(self, x, phase='train'):
        feats = self.densenet121(x)  # (32, 1024, 2, 16)
        if not args.small:
            feats = F.max_pool2d(feats, kernel_size=2, stride=2)  # (32, 1024, 1, 8)
        out = self.classifier_font(feats)  # (32, 1824, 1, 8)
        out_size = out.size()
        # print out.size()
        out = out.view(out.size(0), out.size(1), -1)  # (32, 1824, 8)
        # print out.size()
        if phase == 'train':
            out = F.adaptive_max_pool1d(out, output_size=(1)).view(out.size(0), -1)  # (32, 1824)
            return out
        else:
            out = out.transpose(1, 2).contiguous()
            out = out.view(out_size[0], out_size[2], out_size[3], out_size[1])  # (32, 1, 8, 1824)
            return out, feats 
Example #2
Source File: layers.py    From TorchFusion with MIT License 5 votes vote down vote up
def pool(self, input):
        return F.adaptive_max_pool1d(input, 1) 
Example #3
Source File: test_pyprof_nvtx.py    From apex with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_adaptive_max_pool1d(self):
        inp = torch.randn(1, 16, 28, device='cuda', dtype=self.dtype)
        out = F.adaptive_max_pool1d(inp, output_size=5, return_indices=True) 
Example #4
Source File: bpv.py    From ecom-rakuten with MIT License 5 votes vote down vote up
def pool(self, x, bs, is_max):
        f = F.adaptive_max_pool1d if is_max else F.adaptive_avg_pool1d
        return f(x.permute(1, 2, 0), (1,)).view(bs, -1) 
Example #5
Source File: vector_model.py    From starsem2018-entity-linking with Apache License 2.0 4 votes vote down vote up
def forward(self, sent_m, mention_m,
                relations_m, relations_words_m,
                entities_m, candidates_m, candidates_labels_m, features_m):
        choices = candidates_labels_m.size(1)  # number of possible candidates per one mention
        real_choices_num = torch.sum((candidates_m > 0).float(), dim=1).unsqueeze(1)

        sent_emb = self._words2vector(sent_m).unsqueeze(1)
        mention_emb = self._chars2vector(mention_m).unsqueeze(1)

        sent_emb_expanded = sent_emb.expand(sent_emb.size(0), choices, sent_emb.size(2)).contiguous()
        mention_emb_expanded = mention_emb.expand(mention_emb.size(0), choices, mention_emb.size(2)).contiguous()

        relations_words_m = relations_words_m.view(relations_words_m.size(0) *
                                                   relations_words_m.size(1) * relations_words_m.size(2), -1)
        relations_m = relations_m.view(relations_m.size(0) * relations_m.size(1), -1)
        entities_m = entities_m.view(entities_m.size(0) * entities_m.size(1), -1)
        candidates_labels_m = candidates_labels_m.view(candidates_labels_m.size(0) * candidates_labels_m.size(1), -1, 2)

        candidate_vectors = self.compute_candidate_vectors(relations_m, relations_words_m,
                                           entities_m, candidates_m, candidates_labels_m)
        candidate_vectors = candidate_vectors.view(-1, choices, candidate_vectors.size(-1))

        features_m = features_m.float()

        concatenated_embed = torch.cat((sent_emb_expanded,
                                        mention_emb_expanded,
                                        candidate_vectors,
                                        features_m
                                        ), dim=-1).contiguous()
        concatenated_embed = concatenated_embed.view(-1, concatenated_embed.size(-1))
        sem_vector = self.sem_layers(concatenated_embed)
        sem_vector = sem_vector.view(-1, choices, sem_vector.size(-1))

        sem_vector_pooled_over_choices = sem_vector.transpose(-2, -1)
        sem_vector_pooled_over_choices = self._pool(sem_vector_pooled_over_choices)
        sem_vector_pooled_over_choices = sem_vector_pooled_over_choices.transpose(-2, -1)
        sem_vector = torch.cat((sem_vector, sem_vector_pooled_over_choices.expand_as(sem_vector)), dim=-1)
        sem_vector = sem_vector.view(-1, sem_vector.size(-1))

        candidate_scores = self.score_weights(sem_vector).squeeze(dim=-1)
        candidate_scores = candidate_scores.view(-1, choices)

        negative_vector = torch.cat((sent_emb.squeeze(dim=1),
                                     mention_emb.squeeze(dim=1)), dim=1)
        negative_vector = self.negative_layers(negative_vector)
        real_choices_num = self._nonlinearity(real_choices_num)
        choices_pooled_for_negative = sem_vector_pooled_over_choices.squeeze(dim=1)
        negative_vector = torch.cat((negative_vector,
                                     choices_pooled_for_negative,
                                     F.adaptive_max_pool1d(candidate_scores.unsqueeze(1), 1).squeeze(dim=-1),
                                     real_choices_num
                                     ), dim=-1)
        negative_score = self.negative_weight(negative_vector)

        candidate_scores = self._nonlinearity(candidate_scores)
        return F.sigmoid(negative_score.squeeze(dim=-1)), candidate_scores 
Example #6
Source File: feature_model.py    From starsem2018-entity-linking with Apache License 2.0 4 votes vote down vote up
def forward(self, e_x, e_sig, x, x_sig):
        e_x = e_x.long()
        x = x.float()
        x_sig = x_sig.float()
        e_sig = e_sig.float()
        choices = x.size(1)

        e_x = self._pos_embeddings(e_x)
        e_x = e_x.transpose(1, 2)
        e_x = F.adaptive_avg_pool1d(e_x, 1).view(*e_x.size()[:2])
        e_x = e_x.unsqueeze(1)
        e_x = e_x.expand(e_x.size(0), choices, e_x.size(2)).contiguous()
        e_sig = e_sig.unsqueeze(1)
        e_sig = e_sig.expand(e_sig.size(0), choices, e_sig.size(2)).contiguous()

        x = torch.cat((
            x,
            x_sig,
            e_x,
            e_sig), dim=-1)
        x = x.view(-1, x.size(-1))

        i = self.individual_weights(x)
        i = F.relu(i)
        i = self.hidden_weights(i)
        i = F.relu(i)
        i = i.view(-1, choices, i.size(-1))

        s = i.transpose(1, 2)
        s = F.adaptive_max_pool1d(s, 1)
        s = s.transpose(1, 2)

        v = s.expand_as(i)
        v = torch.cat((i, v), dim=-1)
        v = v.view(-1, v.size(-1))

        v = self._dropout(v)
        x = self.score_weights(v)
        x = x.view(-1, choices)
        # x = F.relu(x)

        z = s.view(-1,  s.size(-1))

        z = self._dropout(z)
        z = self.negative_weights(z)

        # x = torch.cat((z, x), dim=-1)

        return F.sigmoid(z.squeeze(dim=-1)), F.softmax(x, dim=-1)