Beispiel #1
0
    def forward(self, x):
        N, C, T, V, M = x.size()
        x = x.permute(0, 4, 3, 1, 2).contiguous().view(N, M * V * C, T)

        if self.use_data_bn:
            x = self.data_bn(x)

        x = self.conv0(x)
        x = self.unit1(x)
        x = self.unit2(x)
        x = self.unit3(x)
        x = self.unit4(x)
        x = self.unit5(x)
        x = self.unit6(x)
        x = self.unit7(x)
        x = self.unit8(x)
        x = self.unit9(x)
        x = self.bn(x)
        x = self.relu(x)

        x = F.avg_pool1d(x, kernel_size=x.size()[2])

        x = self.fcn(x)
        x = x.view(-1, self.num_class)

        return x
    def avg_pool1d(x, seq_lens):  # shape is same as below
        out = []
        for index, t in enumerate(x):
            t = t[:seq_lens[index], :]
            t = torch.t(t).unsqueeze(0)
            out.append(F.avg_pool1d(t, t.size(2)))

        out = torch.cat(out).squeeze(2)
        return out
    def avg_pool1d(self, x, seq_lens):
        # x:[N,L,O_in]
        out = []
        for index, t in enumerate(x):
            t = t[:seq_lens[index], :]
            t = torch.t(t).unsqueeze(0)
            out.append(F.avg_pool1d(t, t.size(2)))

        out = torch.cat(out).squeeze(2)
        return out
    def forward(self, q1, q2):

        q1 = self.embed(q1)
        q1 = q1.unsqueeze(1)  # (N, Ci, W, D)
        q1 = [F.tanh(conv(q1)).squeeze(3) for conv in self.convs1]  # [(N, Co, W), ...]*len(Ks)
        q1 = [i.size(2) * F.avg_pool1d(i, i.size(2)).squeeze(2) for i in q1]  # [(N, Co), ...]*len(Ks)
        q1 = [F.tanh(i) for i in q1]
        q1 = torch.cat(q1, 1) # 64 * 300
        
        q2 = self.embed(q2)
        q2 = q2.unsqueeze(1)  # (N, Ci, W, D)
        q2 = [F.tanh(conv(q2)).squeeze(3) for conv in self.convs1]  # [(N, Co, W), ...]*len(Ks)
        q2 = [i.size(2) * F.avg_pool1d(i, i.size(2)).squeeze(2) for i in q2]  # [(N, Co), ...]*len(Ks)
        q2 = [F.tanh(i) for i in q2]
        q2 = torch.cat(q2, 1)
        
        cos_ans = F.cosine_similarity(q1, q2)

        # cos_ans = F.sigmoid(cos_ans)
        return cos_ans
Beispiel #5
0
 def agg_channel(self, x, pool="max"):
     b, c, h, w = x.size()
     x = x.view(b, c, h * w)
     x = x.permute(0, 2, 1)
     if pool == "max":
         x = F.max_pool1d(x, c)
     elif pool == "avg":
         x = F.avg_pool1d(x, c)
     x = x.permute(0, 2, 1)
     x = x.view(b, 1, h, w)
     return x
Beispiel #6
0
    def forward(self, x):
        dropout = nn.Dropout(p=0)

        out = self.cv0(x)
        res1 = out
        out = dropout(self.cv1_1(F.relu(self.bn1_1(out))))
        out = self.cv1_2(F.relu(self.bn1_2(out)))
        res2 = out + res1
        out = dropout(self.cv1_3(F.relu(self.bn1_3(res2))))
        out = self.cv1_4(F.relu(self.bn1_4(out)))
        res3 = out + res2
        out = dropout(self.cv1_5(F.relu(self.bn1_5(res3))))
        out = self.cv1_6(F.relu(self.bn1_6(out)))
        res4 = out + res3
        out = dropout(self.cv1_7(F.relu(self.bn1_7(res4))))
        out = self.cv1_8(F.relu(self.bn1_8(out)))
        res4_1 = res4 + out
        res4_2 = self.res_cv1to2(res4_1)  # Residual link down sampled

        out = dropout(self.cv2_1(F.relu(self.bn2_1(res4_1))))
        out = self.cv2_2(F.relu(self.bn2_2(out)))
        res5 = out + res4_2
        out = dropout(self.cv2_3(F.relu(self.bn2_3(res5))))
        out = self.cv2_4(F.relu(self.bn2_4(out)))
        res6 = out + res5
        out = dropout(self.cv2_5(F.relu(self.bn2_5(res6))))
        out = self.cv2_6(F.relu(self.bn2_6(out)))
        res7 = out + res6
        out = dropout(self.cv2_7(F.relu(self.bn2_7(res7))))
        out = self.cv2_8(F.relu(self.bn2_8(out)))
        res8 = out + res7
        res8_2 = self.res_cv2to3(res8)  # Residual link down sampled

        out = dropout(self.cv3_1(F.relu(self.bn3_1(res8))))
        out = self.cv3_2(F.relu(self.bn3_2(out)))
        res9 = out + res8_2
        out = dropout(self.cv3_3(F.relu(self.bn3_3(res9))))
        out = self.cv3_4(F.relu(self.bn3_4(out)))
        res10 = out + res9
        out = dropout(self.cv3_5(F.relu(self.bn3_5(res10))))
        out = self.cv3_6(F.relu(self.bn3_6(out)))
        res11 = out + res10
        out = dropout(self.cv3_7(F.relu(self.bn3_7(res11))))
        out = self.cv3_8(F.relu(self.bn3_8(out)))
        res12 = out + res11

        out = F.relu(self.bn4(res12))
        out = F.avg_pool1d(out, kernel_size=out.shape[2], stride=1, padding=0)

        out = self.conv4(out)

        out = out.view(out.size(0), -1)

        return F.log_softmax(out, dim=1)
Beispiel #7
0
 def forward(self, x, **kwargs):
     if self.mode == 'rand':
         x = self.embed(x)  # (batch, sent_len, embed_dim)
     elif self.mode == 'static':
         x = self.static_embed(x)  # (batch, sent_len, embed_dim)
     elif self.mode == 'non-static':
         x = self.non_static_embed(x)  # (batch, sent_len, embed_dim)
     x = F.avg_pool1d(x.transpose(1, 2),
                      x.shape[1]).squeeze(2)  # (batch, embed_dim)
     logit = self.fc1(x)  # (batch, target_size)
     return logit
Beispiel #8
0
 def channel_avg_pool0d(self, x, face):
     assert self.ndim(x) == 2
     in_channels = self.shape(x)[1]
     assert 0 <= face <= in_channels
     assert in_channels % face == 0
     x = self.expand_dims(x, 1)
     pool_face = face
     pool_stride = pool_face
     pool_padding = 0
     x = F.avg_pool1d(x, pool_face, pool_stride, pool_padding)
     return self.squeeze(x, 1)
    def feature(self, x):
        x = F.max_pool2d(self.conv1(x), 2)
        x = F.max_pool2d((self.conv2(x)), 2)

        x = F.avg_pool2d(x, 2)
        # print x.size()
        x = x.view(-1, 1, 80)
        x = F.avg_pool1d(x, kernel_size=38, stride=1)
        # print x.size()
        x = x.view(-1, 43)
        return x
    def mean_std(self, mode='bm'):
        mean = torch.mean(self.buffer[self.start:self.end]).item()

        if mode == 'bm':        # batch mean variance
            b_n = int(math.floor(math.sqrt(self.count)))
            Yks = F.avg_pool1d(self.buffer[self.start:self.end].unsqueeze(0).unsqueeze(0), kernel_size=b_n, stride=b_n).view(-1)
            diffs = Yks - mean
            std = math.sqrt(b_n /(len(Yks)-1))*torch.norm(diffs).item()
            dof = b_n - 1
        elif mode == 'olbm':    # overlapping batch mean
            b_n = int(math.floor(math.sqrt(self.count)))
            Yks = F.avg_pool1d(self.buffer[self.start:self.end].unsqueeze(0).unsqueeze(0), kernel_size=b_n, stride=1).view(-1)
            diffs = Yks - mean
            std = math.sqrt(b_n*self.count/(len(Yks)*(len(Yks)-1)))*torch.norm(diffs).item()
            dof = self.count - b_n
        else:                   # otherwise use mode == 'iid'
            std = torch.std(self.buffer[self.start:self.end]).item()
            dof = self.count - 1

        return mean, std, dof
Beispiel #11
0
    def forward(self, x, cond):
        residual = self.skip_conv(x)  # [B, input_dim, T] -> [B, output_dim, T]
        residual = F.avg_pool1d(
            residual, kernel_size=self.scale
        ) if self.scale != 1 else residual  # [B, output_dim, T] -> [B, output_dim, T/x]

        x = F.avg_pool1d(
            x, kernel_size=self.scale
        ) if self.scale != 1 else x  # [B, input_dim, T] -> [B, input_dim, x*T]
        for i, resblock in enumerate(
                self.resblocks):  # [B, input_dim, T] -> [B, output_dim, x*T]
            x = resblock(x.clone())
            if i == self.cond_block_id and hasattr(self, 'cond_conv'):
                cond = self.cond_conv(
                    cond)  # [B, cond_dim, T] -> [B, output_dim, T]
                if cond.shape[2] != x.shape[2]:
                    cond = F.interpolate(cond, size=x.shape[2], mode='linear')
                x = x + cond

        return x + residual
Beispiel #12
0
 def forward(self, x, z=None, output_lengths=None):  # [B, in_dim, T]
     if hasattr(self, 'bn'):
         x = self.bn(x, z)
     x = self.act_func(x)
     if hasattr(self, 'scale'):
         if self.downsample:
             F.avg_pool1d(x, kernel_size=self.scale)
         else:
             x = F.interpolate(
                 x, scale_factor=self.scale,
                 mode='linear')  # [B, in_dim, T]   -> [B, in_dim, x*T]
     if output_lengths is not None:
         scale_factor = x.shape[2] / output_lengths.sum().max()
         if scale_factor != 1.0:
             output_lengths = (output_lengths.float() *
                               (scale_factor)).long()
         mask = get_mask_from_lengths(output_lengths).unsqueeze(1)
         x.masked_fill_(~mask, 0.0)
     x = self.conv(x)  # [B, in_dim, x*T] -> [B, out_dim, x*T]
     return x  # [B, out_dim, x*T]
Beispiel #13
0
    def forward(self, input):
        x = input # (batch_size, 1, time_steps, mel_bins)

        frames_num = x.shape[2]

        x = x.transpose(1, 3)
        x = self.bn0(x)
        x = x.transpose(1, 3)

        if self.training:
            if random.random() < 0.25:
                x = self.spec_augmenter(x)

        x = x.transpose(2, 3)
        # (batch_size, channels, freq, frames)
        x = self.encoder(x)

        # (batch_size, channels, frames)
        x = torch.mean(x, dim=2)

        # channel smoothing
        x1 = F.max_pool1d(x, kernel_size=3, stride=1, padding=1)
        x2 = F.avg_pool1d(x, kernel_size=3, stride=1, padding=1)
        x = x1 + x2

        x = F.dropout(x, p=0.5, training=self.training)
        x = x.transpose(1, 2)
        x = F.relu_(self.fc1(x))
        x = x.transpose(1, 2)
        x = F.dropout(x, p=0.5, training=self.training)
        (clipwise_output, norm_att, segmentwise_output) = self.att_block(x)
        logit = torch.sum(norm_att * self.att_block.cla(x), dim=2)
        segmentwise_logit = self.att_block.cla(x).transpose(1, 2)
        segmentwise_output = segmentwise_output.transpose(1, 2)

        interpolate_ratio = frames_num // segmentwise_output.size(1)

        # Get framewise output
        framewise_output = interpolate(segmentwise_output,
                                       interpolate_ratio)
        framewise_output = pad_framewise_output(framewise_output, frames_num)

        framewise_logit = interpolate(segmentwise_logit, interpolate_ratio)
        framewise_logit = pad_framewise_output(framewise_logit, frames_num)

        output_dict = {
            "framewise_output": framewise_output,
            "segmentwise_output": segmentwise_output,
            "logit": logit,
            "framewise_logit": framewise_logit,
            "clipwise_output": clipwise_output
        }

        return output_dict
Beispiel #14
0
    def forward(self, embeddings, mask):

        #embeddings = [batch, emb dim, seq len]
        #mask = [batch, seq len]

        _, _, seq_len = embeddings.shape

        mask = mask.unsqueeze(1)

        #mask = [batch, 1, seq len]

        if self.pool_mode == 'mean':
            embeddings = embeddings.masked_fill(mask == 0, 0)
            pooled = F.avg_pool1d(embeddings, kernel_size=seq_len)

        elif self.pool_mode == 'max':
            embeddings = embeddings.masked_fill(mask == 0, -1e10)
            pooled = F.max_pool1d(embeddings, kernel_size=seq_len)

        elif self.pool_mode == 'weighted_mean':
            _embeddings = embeddings.permute(0, 2, 1)
            #_embeddings = [batch, seq len, emb dim]
            weights = torch.sigmoid(self.fc(_embeddings))
            #weighs = [batch, seq len, 1]
            weights = weights.permute(0, 2, 1)
            #weights = [batch, 1, seq len]
            weighted = embeddings * weights
            weighted = weighted.masked_fill(mask == 0, 0)
            #weighted = [batch, emb dim, seq len]
            pooled = F.avg_pool1d(weighted, kernel_size=seq_len)

        else:
            raise ValueError(f'Unknown pool mode: {self.pool_mode}')

        #pooled = [batch size, emb dim, 1]

        pooled = pooled.squeeze(-1)

        #pooled = [batch size, emb dim]

        return pooled
Beispiel #15
0
    def forward(self, x):
        N, C, T, V, M = x.size()

        # data bn
        if self.use_data_bn:
            if self.M_dim_bn:
                x = x.permute(0, 4, 3, 1, 2).contiguous().view(N, M * V * C, T)
            else:
                x = x.permute(0, 4, 3, 1, 2).contiguous().view(N * M, V * C, T)

            x = self.data_bn(x)
            # to (N*M, C, T, V)
            x = x.view(N, M, V, C,
                       T).permute(0, 1, 3, 4,
                                  2).contiguous().view(N * M, C, T, V)
        else:
            # from (N, C, T, V, M) to (N*M, C, T, V)
            x = x.permute(0, 4, 1, 2, 3).contiguous().view(N * M, C, T, V)

        # model
        x = self.gcn0(x)
        x = self.tcn0(x)
        for m in self.backbone:
            x = m(x)

        # V pooling
        x = F.avg_pool2d(x, kernel_size=(1, V))

        # M pooling
        x = x.view(N, M, x.size(1), x.size(2))
        x = x.mean(dim=1)

        # T pooling
        x = F.avg_pool1d(x, kernel_size=x.size()[2])

        # C fcn
        x = self.fcn(x)
        x = F.avg_pool1d(x, x.size()[2:])
        x = x.view(N, self.num_class)

        return x
    def forward(self,
                xs: torch.Tensor,
                x_masks: Optional[torch.Tensor] = None
                ) -> torch.Tensor:

        for f in self.conv:
            xs = f(xs)  # (B, C, Tmax)

        # NOTE: calculate in log domain
        xs = F.avg_pool1d(xs, xs.size(-1))  # (B, C, 1)

        return xs
Beispiel #17
0
    def forward(self, x):

        # create lists for both outputs and attentions
        out_list = []
        att_list = []

        # the resnet backbone
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)

        # grouping module upon the feature maps outputed by the backbone
        region_feature, assign = self.grouping(x)
        region_feature = region_feature.contiguous().unsqueeze(3)

        # generate attention
        for i in range(self.num_classes):
            att = self.attconv[i](region_feature)
            att = F.softmax(att, dim=2)
            att_list.append(att)

        # non-linear layers over the region features
        region_feature = self.post_block(region_feature)

        # attention-based classification
        for i in range(self.num_classes):

            # apply the attention on the features
            out = region_feature.clone() * att_list[i]
            out = out.contiguous().squeeze(3)

            # average all region features into one vector based on the attention
            out = F.avg_pool1d(out, self.n_parts) * self.n_parts
            out = out.contiguous().unsqueeze(3)

            # final batchnorm
            out = self.groupingbn(out)

            # nonlinear block for each head
            out = self.nonlinear[i](out)

            # linear classifier
            out = out.contiguous().view(out.size(0), -1)
            out = self.mylinear[i](out)

            # append the output
            out_list.append(out)

        return out_list, att_list, assign
 def forward(self, q1, q2):
     mask1, mask2 = q1.eq(0), q2.eq(0)
     res = [[], []]
     q1_encode = self.embed(q1)
     q2_encode = self.embed(q2)
     # eg: s1 => res[0]
     # (batch_size, seq_len, dim) => (batch_size, dim)
     # if num_layer == 0
     res[0].append(F.avg_pool1d(q1_encode.transpose(1, 2), kernel_size=q1_encode.size(1)).squeeze(-1))
     res[1].append(F.avg_pool1d(q2_encode.transpose(1, 2), kernel_size=q2_encode.size(1)).squeeze(-1))
     for i, conv in enumerate(self.conv):
         o1, o2 = conv(q1_encode, q2_encode, mask1, mask2)
         res[0].append(F.avg_pool1d(o1.transpose(1, 2), kernel_size=o1.size(1)).squeeze(-1))
         res[1].append(F.avg_pool1d(o2.transpose(1, 2), kernel_size=o2.size(1)).squeeze(-1))
         o1, o2 = attention_avg_pooling(o1, o2, mask1, mask2)
         q1_encode, q2_encode = o1 + q1_encode, o2 + q2_encode
     # batch_size * (dim*(1+num_layer)*2) => batch_size * linear_size
     x = torch.cat([torch.cat(res[0], 1), torch.cat(res[1], 1)], 1)
     sim = self.fc(x)
     probabilities = nn.functional.softmax(sim, dim=-1)
     return sim, probabilities
Beispiel #19
0
    def forward(self, x):
        # x: [token, last_token, user]
        tokens = self.vocab_embedding(x[:, :2])
        users = self.user_embedding(x[:, 2]).unsqueeze(1)
        x = torch.cat([tokens, users], dim=1)

        x = self.conv1(x)
        x = self.dropout(x)

        x = F.avg_pool1d(x, x.size(2)).squeeze(2)
        x = self.out(x)
        return x
Beispiel #20
0
 def forward(self, x, self_idx=-1, mask=None):
     x, self_x = self.embedding(x, self_idx)
     for i in range(self._attn_N):
         x = self.layers[i](x, mask)
     x = self.norm(x)
     if self._use_average_pool:
         x = torch.transpose(x, 1, 2)
         x = F.avg_pool1d(x, kernel_size=x.size(-1)).view(x.size(0), -1)
         if self._cat_self:
             x = torch.cat((x, self_x), dim=-1)
     x = x.view(x.size(0), -1)
     return x
 def forward(self, text):
     """
     :param text: result of text being mapped to numebr
     :return: tensor of same size of number of classes for classification
     """
     embedded = self.embedding(text)
     c = embedded.size(0) // BATCH_SIZE
     embedded = embedded[:BATCH_SIZE * c]
     embedded = embedded.transpose(1, 0).unsqueeze(0)
     embedded = f.avg_pool1d(embedded, kernel_size=c)
     out = self.fc2(f.relu(self.fc1(embedded[0].transpose(1, 0))))
     return out
Beispiel #22
0
 def forward(self, x, target, desc_data=None, get_attention=False):
     #get embeddings and apply dropout
     x = self.embed(x)
     x = self.embed_drop(x)
     x = x.transpose(1, 2)
     if self.pool == 'max':
         x = F.max_pool1d(x)
     else:
         x = F.avg_pool1d(x)
     logits = torch.sigmoid(self.final(x))
     loss = self._get_loss(logits, target, diffs)
     return yhat, loss, None
Beispiel #23
0
 def apply_pooling(self, input):
     """
     在seq——len的维度进行池化
     :param input: batch_size,seq_len,hidden_size
     :return:
     """
     avg_output = F.avg_pool1d(input.transpose(
         1, 2), input.size(1)).squeeze(-1)  #[batch_size,hidden_size]
     max_output = F.max_pool1d(input.transpose(1, 2),
                               input.size(1)).squeeze(-1)
     return torch.cat([avg_output, max_output],
                      dim=-1)  #[batch_size,hidden_size*2]
Beispiel #24
0
 def forward(self,x , mode = 'train'):
     out = self.prelayer(x) 
     out = self.layer1(out)
     out = self.layer2(out)
     out = self.layer3(out)
     out = self.layer4(out)
     out = F.avg_pool1d(out,7)
     out = out.view(out.size(0),-1)
     out = self.fc(out)
     if(mode == 'eval'):
         out = self.softmax(out)
     return out
Beispiel #25
0
 def conv_block(self, x, conv_layers, norm_layers, res=True):
     out = x
     for layer in conv_layers:
         out = pad_layer(out, layer)
         out = F.leaky_relu(out, negative_slope=self.ns)
     for layer in norm_layers:
         out = layer(out)
     if res:
         x_pad = F.pad(x, pad=(0, x.size(2) % 2), mode='reflect')
         x_down = F.avg_pool1d(x_pad, kernel_size=2)
         out = x_down + out 
     return out
Beispiel #26
0
    def forward_rnn(self, inputs, state, seq_lens):
        horizon = self.horizon

        worker_states = state[:2]
        manager_states = state[2:2 + horizon * 3]
        horizon_goals = state[2 + horizon * 3:]

        self.z = inputs

        latent_state, goal, manager_states = self.manager(
            inputs, manager_states)

        if inputs.shape[1] == 1:
            random_select = torch.rand(goal.shape[0], device=goal.device) > -1
            random_goal = torch.normal(goal.clone().detach().fill_(0),
                                       goal.clone().detach().fill_(1))
            random_goal = random_goal / random_goal.norm(dim=-1, keepdim=True)
            random_goal = random_goal * ~random_select.unsqueeze(-1).unsqueeze(
                -1)
            goal = goal * random_select.unsqueeze(-1).unsqueeze(-1)
            goal += random_goal
            self.random_select = random_select
            self.random_goal = random_goal
        else:
            goal = goal * self.random_select.unsqueeze(-1)
            goal += self.random_goal

        self.latent_state = latent_state
        self.goal = goal

        # Sample Batch
        if inputs.shape[1] == 1:
            # Update horizon like a queue
            for i in range(horizon - 1):
                horizon_goals[i] = horizon_goals[i + 1]
            horizon_goals[-1] = goal.detach()

            # Pool goal over past horizon
            horizon_goal = torch.sum(torch.stack(horizon_goals), dim=0)
        # Train Batch
        else:
            # Pool goal over past horizon
            # 1) Concat past goal horizon with goals
            # 2) Run 1D sum pooling to pool goals
            horizon_goal = torch.cat(
                (torch.cat(horizon_goals[1:], dim=1), goal.detach()), dim=1)
            horizon_goal = horizon_goal.permute(0, 2, 1)
            horizon_goal = F.avg_pool1d(horizon_goal, horizon, 1) * horizon
            horizon_goal = horizon_goal.permute(0, 2, 1)

        action, worker_states = self.worker(inputs, horizon_goal,
                                            worker_states)
        return action, worker_states + manager_states + horizon_goals
Beispiel #27
0
 def forward(self, x: torch.Tensor) -> torch.Tensor:
     """
     Embeddes the input signal into a latent vector
     """
     if self.downsample:
         if x.ndim == 3:
             x = F.avg_pool1d(x, self.downsample, self.downsample)
         else:
             x = F.avg_pool2d(x, self.downsample, self.downsample)
     x = self.conv(x)
     x = x.reshape(-1, self.reshape_)
     return self.fc(x)
    def forward(self, features):
        '''
        :param features: N,n,in_features
        :return:
        '''
        lay1 = self.linear1(
            F.avg_pool1d(features, features.size(2)).squeeze(dim=2))
        lay1 = F.tanh(lay1)
        coeffi = F.softmax(self.linear2(lay1), dim=1)
        # coeffi = F.sigmoid(self.linear2(lay1))

        return (coeffi.unsqueeze(dim=-1) * features).sum(dim=1)
Beispiel #29
0
 def forward(self, send, recv):
     x = torch.cat((send, recv), dim=-1)
     x = x.permute(0, 2, 1)
     x = self.conv1(x)
     x = F.relu(x)
     x = F.avg_pool1d(x, 2)
     x = self.conv2(x)
     x = F.relu(x)
     x = torch.mean(x, -1)
     x = self.fc(x)
     x = F.tanh(x)
     return x
Beispiel #30
0
 def forward(self, x):
     x = self.conv1(x)
     x = self.conv2(x)
     x = self.conv3(x)
     x = self.conv4(x)
     x = self.conv5(x)
     x = F.avg_pool1d(x, 175)
     x = x.view(
         x.size(0),
         -1)  # flatten the output of conv2 to (batch_size, 32 * 7 * 7)
     output = self.out(x)
     return output
Beispiel #31
0
    def forward(self, x, z):
        b = x.size(0)
        t = x.size(1)
        bd = z.size(0)
        bd = z.size(1)
        #RGB
        x = x.view(b * t, x.size(2), x.size(3), x.size(4))
        x = self.base(x)
        x = F.avg_pool2d(x, x.size()[2:])
        x = x.view(b, t, -1)
        output, (h_n, c_n) = self.lstm(x)
        output = output.permute(0, 2, 1)
        f = F.avg_pool1d(output, t)
        f = f.view(b, self.hidden_dim)
        #Depth
        z = z.view(bd * td, z.size(2), z.size(3), z.size(4))
        z = self.base(z)
        z = F.avg_pool2d(z, z.size()[2:])
        z = z.view(bd, td, -1)
        outputd, (h_n, c_n) = self.lstm(z)
        outputd = outputd.permute(0, 2, 1)
        fd = F.avg_pool1d(outputd, td)
        fd = fd.view(bd, self.hidden_dim)
        if not self.training:
            return f, fd

        y = self.bilinear(f, fd)  #Uniamo
        #riaggiustiamo dimensioni
        f = f.view(16, -1)
        fd = fd.view(16, -1)
        y = y.view(16, -1)

        if self.loss == {'xent'}:
            return y
        elif self.loss == {'xent', 'htri'}:
            return y, f, fd
        elif self.loss == {'cent'}:
            return y, f, fd
        else:
            raise KeyError("Unsupported loss: {}".format(self.loss))
Beispiel #32
0
    def forward(self, inputs):
        # import pdb; pdb.set_trace()
        input_p, input_h = inputs[0], inputs[1]

        batch_size = len(inputs[0])

        # Input Encoding
        xp = self.emb(input_p)
        xh = self.emb(input_h)

        # Sentence embedding
        op, _ = self.input_encoding(xp)  # (batch, sentence_len, embedding_dim)
        oh, _ = self.input_encoding(xh)

        # Local Inference Modeling (with soft-align attention)
        E_ph = torch.matmul(op, oh.transpose(
            -1, -2))  # We calculate the similarity matrix of op and oh
        wp = torch.matmul(F.softmax(E_ph, dim=2), oh)
        wh = torch.matmul(F.softmax(E_ph, dim=1).transpose(-1, -2), op)

        # Enhancement of local inference information
        mp = torch.cat([op, wp, op - wp, op * wp], dim=1)
        mh = torch.cat([oh, wh, oh - wh, oh * wh], dim=1)

        # Pooling
        embedding_dim = mp.size(-1)
        vp_a = F.avg_pool1d(mp.transpose(-1, -2),
                            embedding_dim).view(batch_size, -1)
        vp_m = F.max_pool1d(mp.transpose(-1, -2),
                            embedding_dim).view(batch_size, -1)
        vh_a = F.avg_pool1d(mh.transpose(-1, -2),
                            embedding_dim).view(batch_size, -1)
        vh_m = F.max_pool1d(mh.transpose(-1, -2),
                            embedding_dim).view(batch_size, -1)

        v = torch.cat([vp_a, vp_m, vh_a, vh_m], dim=1)

        y_p = self.nn(v)
        return y_p
Beispiel #33
0
    def avg_pool1d(self, x, seq_lens):
        # x:[N,L,O_in]
        out = []
        for index, t in enumerate(x):
            t = t[:seq_lens[index], :]  #[L,O_in]
            t = torch.t(t).unsqueeze(0)  #[1,O,L]
            out.append(F.avg_pool1d(
                t, t.size(2)))  #list các tensor 3 chiều : [1,O,1]

        out = torch.cat(out).squeeze(
            2
        )  #cắt chiều ngoài cùng và cat lại theo hàng thành 1 tensor 3 chiều
        return out  #=> [N,O_in]
Beispiel #34
0
    def forward(self, x_in, apply_softmax=False):
        x_embedded = self.emb(x_in).permute(0, 2, 1)

        features = self.convnet(x_embedded)

        remaining_size = features.size(dim=2)
        features = F.avg_pool1d(features, remaining_size).squeeze(dim=2)
        features = F.dropout(features, p=self._dropout_p)

        intermediate_vector = F.relu(F.dropout(self.fc1(features),
                                               p=self._dropout_p))
        prediction_vector = self.fc2(intermediate_vector)

        if apply_softmax:
            prediction_vector = F.softmax(prediction_vector, dim=1)

        return prediction_vector
Beispiel #35
0
    def forward(self,tree,block_texts):

        all_blocks = []  
        nb_blocks=0
        if True:
            for block_text in block_texts:
                block_len=len(block_text)
                if len(block_text)==0:    
                    all_blocks.append(self.zero_var)
                    continue
                block_embs = self.emb(block_text)
                block_rep = torch.max(block_embs,0)[0]
                #print(type(block_rep))
                #print(block_rep.size())
                #block_rep = torch.mean(block_embs,dim=0)
                all_blocks.append(block_rep.unsqueeze(0))
            nb_blocks=len(all_blocks)    
            all_blocks=torch.cat(all_blocks).unsqueeze(0).transpose(1,2)
        else:
            for block_text in block_texts:
                block_len=len(block_text)
                if len(block_text)<3:    
                    continue
                    
                block_embs = self.emb(block_text)
                block_embs=block_embs.transpose(0,1).unsqueeze(0)
                #block_rep = torch.mean(block_embs,dim=0)
                #all_blocks.append(block_rep.unsqueeze(0))
                cs = []
                for i in range(len(self.filter_frames_block)):    
                    ci = F.relu(self.conv_models_block[i](block_embs))
                    ci = F.avg_pool1d(ci,block_len -self.filter_frames_block[i]+1).view(-1, self.filter_dims_block[i])
                    cs.append(ci)
                
                cs = torch.cat(cs, 1)
        
                all_blocks.append(cs)
            nb_blocks=len(all_blocks)
            if nb_blocks<3:
                return None
            all_blocks=torch.cat(all_blocks).unsqueeze(0).transpose(1,2)
            
            #print(all_blocks.size())
            
        
        cs = []
        for i in range(len(self.filter_frames)):    
            ci = F.relu(self.conv_models[i](all_blocks))
            ci = F.avg_pool1d(ci,nb_blocks -self.filter_frames[i]+1).view(-1, self.filter_dims[i])
            cs.append(ci)
        
        cs = torch.cat(cs, 1)

        output = self.output(cs)
        return output

        
        if self.mean_only:
            mean_rep=torch.mean(all_blocks,dim=0)
            mean_rep=mean_rep.unsqueeze(0)
            
            output = self.output(mean_rep)

        else:
            self.childsumtreelstm.clear_states()
            state, hidden = self.childsumtreelstm(tree, all_blocks)
            hidden_states=self.childsumtreelstm.get_states()
            
            hidden_states=torch.cat(hidden_states)

            
            output = self.output(state)
        
        return output
Beispiel #36
0
 def temporal_pooling(self, x, kernel_size, stride):
     x = F.avg_pool1d(x, kernel_size, stride, padding=kernel_size // 2)
     return x
Beispiel #37
0
 def forward(self, input_tensor):
     return functional.avg_pool1d(input_tensor, input_tensor.size()[2:]).view(input_tensor.size()[:2])