Exemplo n.º 1
0
    def __init__(self,
                 input_size,
                 channel_last=True,
                 hidden_size=50,
                 **kwargs):

        # Defaults
        self.input_length = 20
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.num_classes = 2
        self.label_embedding_size = self.num_classes
        self.prob_classes = torch.ones(self.num_classes)
        self.dropout = 0.5
        self.label_type = 'required'
        self.channel_last = channel_last

        # Set kwargs (might overried above attributes)
        for key, value in kwargs.items():
            setattr(self, key, value)

        super(CNNCGANDiscriminator, self).__init__(self.input_size,
                                                   self.label_type)

        # Build CNN layer
        self.label_embeddings = nn.Embedding(self.num_classes,
                                             self.label_embedding_size)

        Conv1d_ = lambda k: Conv1d(hidden_size, hidden_size, k)
        layers_input_size = self.input_size + self.label_embedding_size
        layers_output_size = 5 * hidden_size
        ## Build CNN layer
        self.layers = nn.Sequential(
            Conv1d(layers_input_size, hidden_size, 1),
            LeakyReLU(0.2),
            # output size: (-1, 50, 20)
            Conv1d_(3),
            ReplicationPad1d(1),
            LeakyReLU(0.2),
            Conv1d_(3),
            ReplicationPad1d(1),
            LeakyReLU(0.2),
            AvgPool1d(2, 2),
            # output size: (-1, 50, 10)
            Conv1d_(3),
            ReplicationPad1d(1),
            LeakyReLU(0.2),
            Conv1d_(3),
            ReplicationPad1d(1),
            LeakyReLU(0.2),
            AvgPool1d(2, 2),
            # output size: (-1, 50, 5)
            Flatten(),
            Linear(layers_output_size, 1)
            # output size: (-1, 1)
        )

        # Initialize all weights.
        self._weight_initializer()
Exemplo n.º 2
0
 def __init__(self):
     super(MultiScaleDiscriminator, self).__init__()
     self.discriminators = nn.ModuleList([
         DiscriminatorS(use_spectral_norm=True),
         DiscriminatorS(),
         DiscriminatorS(),
     ])
     self.meanpools = nn.ModuleList(
         [AvgPool1d(4, 2, padding=2),
          AvgPool1d(4, 2, padding=2)])
Exemplo n.º 3
0
 def __init__(self, debug=False):
     super().__init__()
     self.discriminators = nn.ModuleList(
         [
             DiscriminatorS(use_spectral_norm=True, debug=debug),
             DiscriminatorS(debug=debug),
             DiscriminatorS(debug=debug),
         ]
     )
     self.meanpools = nn.ModuleList([AvgPool1d(4, 2, padding=2), AvgPool1d(4, 2, padding=2)])
Exemplo n.º 4
0
 def __init__(self):
     super(MultiScaleDiscriminator, self).__init__()
     self.discriminators = nn.ModuleList([
         Discriminator(),
         Discriminator(),
         Discriminator(),
     ])
     self.meanpools = nn.ModuleList(
         [AvgPool1d(4, 2, padding=2),
          AvgPool1d(4, 4, padding=2)])
Exemplo n.º 5
0
 def __init__(self, out_channels, kernel_size, stride, dilation):
     super(cnnseq, self).__init__()
     self.Conv=Conv1d(in_channels=1,out_channels=out_channels,\
     kernel_size=kernel_size,\
     stride=stride,padding=0,dilation=dilation)
     self.pool = AvgPool1d(kernel_size=5, stride=3)
     self.Conv2=Conv1d(in_channels=out_channels,\
 out_channels=out_channels,kernel_size=3,stride=stride,dilation=dilation)
     self.act = ReLU()
     self.pool2 = AvgPool1d(kernel_size=3, stride=2)
     self.linear = Linear(out_channels * 4, 1)
     self.sigm = Sigmoid()
Exemplo n.º 6
0
 def __init__(self, use_cond=False, c_in=1):
     super(MultiScaleDiscriminator, self).__init__()
     from utils.commons.hparams import hparams
     self.discriminators = nn.ModuleList([
         DiscriminatorS(use_spectral_norm=True,
                        use_cond=use_cond,
                        upsample_rates=[4, 4, hparams['hop_size'] // 16],
                        c_in=c_in),
         DiscriminatorS(use_cond=use_cond,
                        upsample_rates=[4, 4, hparams['hop_size'] // 32],
                        c_in=c_in),
         DiscriminatorS(use_cond=use_cond,
                        upsample_rates=[4, 4, hparams['hop_size'] // 64],
                        c_in=c_in),
     ])
     self.meanpools = nn.ModuleList(
         [AvgPool1d(4, 2, padding=1),
          AvgPool1d(4, 2, padding=1)])
Exemplo n.º 7
0
 def __init__(self, num_labels: int, hidden_size: int, max_seq_length: int, filter_size: int = 9,
              out_channels: int = 16, padding=True, hidden_dropout_prob: float = 0.1) -> Tensor:
     super().__init__()
     self.dropout = Dropout(hidden_dropout_prob)
     padding_size = int((filter_size - 1) / 2) if padding else 0
     self.conv1 = Conv1d(in_channels=hidden_size, out_channels=out_channels, kernel_size=filter_size,
                            padding=padding_size)
     self.max_pool = AvgPool1d(kernel_size=2)
     self.classifier_in_size = int(out_channels * max_seq_length / 2) if padding else \
         int((out_channels * (max_seq_length - filter_size + 1)) / 2)
     self.classifier = Linear(self.classifier_in_size, num_labels)
Exemplo n.º 8
0
 def __init__(self, in_channels: int):
     """Initialize the layers."""
     super().__init__()
     self.layers = Sequential(
         self._get_conv_block(in_channels, 64, kernel_size=5),  # 512 => 256
         self._get_conv_block(64, 64, kernel_size=5),  # 256 => 128
         self._get_conv_block(64, 128),  # 128 => 64
         self._get_conv_block(128, 128, stride=1),  # 64 => 64
         self._get_conv_block(128, 256, stride=1),  # 64 => 64
         AvgPool1d(kernel_size=2),  # 64 => 32
     )
Exemplo n.º 9
0
def avg_pool1d():
    """
    一维均值池化器
    @:param kernel_size – the size of the window
    @:param stride – the stride of the window. Default value is kernel_size
    @:param padding – implicit zero padding to be added on both sides
    @:param ceil_mode – when True, will use ceil instead of floor to compute the output shape
    @:param count_include_pad – when True, will include the zero-padding in the averaging calculation
    """
    pool = AvgPool1d(kernel_size=3, stride=1)
    input = tensor([[[0, 1, 2, 2, 3, 3, 3, 4, 4, 4]]], dtype=float32)
    output = pool(input)
    print(output)
Exemplo n.º 10
0
 def __init__(self, in_channel,out_channel,cnn_ker,cnn_stride,\
         pool_stride,pool_ker ,bn):
     super(CnnModule, self).__init__()
     mylist=ModuleList()
     mylist.append(Conv1d(in_channel,out_channel,
                          kernel_size=cnn_ker, \
                            stride=cnn_stride, padding=0,
                            bias=False))
     if bn==1:
         mylist.append(BatchNorm1d(out_channel))
     if pool_ker>0:
         mylist.append(
         AvgPool1d(kernel_size=pool_ker,stride=pool_stride))
     self.mylist=Sequential(*mylist)
Exemplo n.º 11
0
    def __init__(self, input_shape=(1280, 64), n_classes=3):
        super(LexNet, self).__init__()
        self.c1 = Conv1d(64, 256, kernel_size=2)
        l = input_shape[0]
        l -= 1

        self.c2 = Conv1d(256, 128, kernel_size=2)
        l -= 1

        self.pool = AvgPool1d(kernel_size=2)
        l = int(np.floor(((l - 2) / 2.) + 1))

        self.d1 = Dropout(0.25)

        self.c3 = Conv1d(128, 128, kernel_size=2)
        l -= 1
        self.d2 = Dropout(0.25)

        l = int(np.floor(((l - 2) / 2.) + 1))
        self.mlp = MLP([l * 128, 128, 128])
        self.final = Lin(128, 3)
        self.soft = nn.Softmax()
Exemplo n.º 12
0
    def forward(self, words_embed, chr_rep):
        attention_coefficients = self._calc_attention_coefficients()
        # dynamic average and max pool according to batch sentence length
        activate_avg_pool = AvgPool1d(words_embed.shape[1], 1)
        activate_max_pool = MaxPool1d(words_embed.shape[1], 1)

        # embed_word concat with embed chr level -> Bi-LSTM layer
        x = self._embeddings(words_embed)
        x = torch.cat([x, chr_rep], dim=2)

        # 3 layers Bi-LSTM + skip connections + dropout layers in between
        output_seq, _ = self._lstm_layer_0(x)
        output_seq = self._dropout_0(output_seq)
        output_seq, _ = self._lstm_layer_1(torch.cat([x, output_seq], dim=2))
        output_seq = self._dropout_1(output_seq)
        output_seq, _ = self._lstm_layer_2(torch.cat([x, output_seq], dim=2))
        output_seq = self._dropout_2(output_seq)

        # final vec + attention
        avg_pool = activate_avg_pool(output_seq.transpose(1, 2)).squeeze(dim=2)
        max_pool = activate_max_pool(output_seq.transpose(1, 2)).squeeze(dim=2)
        gate_attention = torch.sum(output_seq * attention_coefficients, dim=1)
        x = torch.cat([gate_attention, avg_pool, max_pool], dim=1)
        return x
Exemplo n.º 13
0
    def __init__(self, n_filters_time, filter_time_length, n_filters_spat,
                 pool_time_length, pool_time_stride, conv_nonlin, pool_mode,
                 pool_nonlin, batch_norm, batch_norm_alpha, drop_prob,
                 final_conv_length, **kwargs):
        super().__init__(**kwargs)

        self.sequential = Sequential()
        split_cnn = SplitConv(in_size=self.input_size,
                              middle_size=n_filters_time,
                              out_size=n_filters_spat,
                              time_kernel_size=filter_time_length,
                              input_in_rnn_format=False)
        self.sequential.add_module('split_cnn', split_cnn)

        if batch_norm:
            bn = BatchNorm1d(n_filters_spat)
            self.sequential.add_module('batch_norm', bn)

        non_lin = Expression(square)
        self.sequential.add_module('non_lin_0', non_lin)

        pool = AvgPool1d(kernel_size=pool_time_length, stride=pool_time_stride)
        self.sequential.add_module('pool_1', pool)
        #
        non_lin = Expression(safe_log)
        self.sequential.add_module('non_lin_1', non_lin)

        dropout = Dropout(p=drop_prob)
        self.sequential.add_module('dropout', dropout)

        conv = nn.Conv1d(in_channels=n_filters_spat,
                         out_channels=self.output_size,
                         kernel_size=final_conv_length,
                         bias=True)

        self.sequential.add_module('conv', conv)
Exemplo n.º 14
0
 def __init__(self, kernel_size, stride, padding):
     super(ChannelPool3d, self).__init__(kernel_size, stride, padding)
     self.pool_1d = AvgPool1d(self.kernel_size, self.stride, self.padding,
                              self.ceil_mode)
Exemplo n.º 15
0
def dim_reduce(t):
    m = AvgPool1d(20)   
    t = m(t.unsqueeze(1))
    return t.squeeze()
Exemplo n.º 16
0
    def forward(self, words_embed, pos_embed, ner_embed, chr_rep,
                semantic_tree, per_mask, org_mask):
        attention_coefficients_i, attention_coefficients_o = self._calc_attention_coefficients(
        )
        # dynamic average and max pool according to batch sentence length
        activate_avg_pool = AvgPool1d(words_embed.shape[1], 1)
        activate_max_pool = MaxPool1d(words_embed.shape[1], 1)

        # concat following [ semantic_parent | chr_embed | word_embed | POS_embed | NER_embed ]
        semantic_tree = semantic_tree.unsqueeze(dim=2)
        embed_word = self._embeddings_words(words_embed)
        x = torch.cat([
            semantic_tree, chr_rep, embed_word,
            self._embeddings_pos(pos_embed),
            self._embeddings_ner(ner_embed)
        ],
                      dim=2)

        # ========================================= ENCODER =========================================
        # 3 layers Bi-LSTM + skip connections + dropout layers in between
        output_seq, _ = self._encoder_layer_0(x)
        output_seq = self._dropout(output_seq)
        output_seq, _ = self._encoder_layer_1(
            torch.cat([semantic_tree, chr_rep, embed_word, output_seq], dim=2))
        output_seq = self._dropout(output_seq)
        output_seq, _ = self._encoder_layer_2(
            torch.cat([semantic_tree, chr_rep, embed_word, output_seq], dim=2))

        # attention
        avg_pool = activate_avg_pool(output_seq.transpose(1, 2)).squeeze(dim=2)
        max_pool = activate_max_pool(output_seq.transpose(1, 2)).squeeze(dim=2)
        gate_attention_i = torch.sum(output_seq * attention_coefficients_i,
                                     dim=1)
        gate_attention_o = torch.sum(output_seq * attention_coefficients_o,
                                     dim=1)
        x = torch.cat([gate_attention_i, gate_attention_o, avg_pool, max_pool],
                      dim=1).unsqueeze(dim=1)
        x = torch.stack([x.squeeze(dim=1)
                         for _ in range(embed_word.shape[1])]).transpose(0, 1)

        # ========================================= DECODER =========================================
        output_seq, _ = self._decoder_layer_0(
            torch.cat([chr_rep, embed_word, x], dim=2))
        output_seq = self._dropout(output_seq)
        output_seq, _ = self._decoder_layer_1(
            torch.cat([chr_rep, embed_word, output_seq], dim=2))
        output_seq = self._dropout(output_seq)
        output_seq, _ = self._decoder_layer_2(
            torch.cat([chr_rep, embed_word, output_seq], dim=2))

        per_mask = torch.stack([per_mask for _ in range(output_seq.shape[2])],
                               dim=2).float()
        org_mask = torch.stack([org_mask for _ in range(output_seq.shape[2])],
                               dim=2).float()

        max_pool = MaxPool1d(output_seq.shape[1])
        per_org_rep = torch.cat([
            max_pool((per_mask * output_seq).transpose(1, 2)).squeeze(dim=2),
            max_pool((org_mask * output_seq).transpose(1, 2)).squeeze(dim=2)
        ],
                                dim=1)
        # per_org_rep = torch.cat([torch.sum((org_mask*output_seq), dim=1),
        #                          torch.sum((per_mask*output_seq), dim=1)], dim=1)
        return per_org_rep