def __init__(self,
                 rnn_type,
                 bidirectional,
                 num_layers,
                 hidden_size,
                 dropout=0.0,
                 embeddings=None,
                 use_bridge=False):
        super(RNNEncoder, self).__init__()
        assert embeddings is not None

        num_directions = 2 if bidirectional else 1
        assert hidden_size % num_directions == 0
        hidden_size = hidden_size // num_directions
        self.embeddings = embeddings

        self.rnn, self.no_pack_padded_seq = \
            rnn_factory(rnn_type,
                        input_size=embeddings.embedding_size,
                        hidden_size=hidden_size,
                        num_layers=num_layers,
                        dropout=dropout,
                        bidirectional=bidirectional)
        self.auxi_lstm, self.no_pack_padded_seq = \
            rnn_factory(rnn_type,
                        input_size=embeddings.embedding_size,
                        hidden_size=hidden_size,
                        num_layers=num_layers,
                        dropout=dropout,
                        bidirectional=bidirectional)

        # Initialize the bridge layer
        self.use_bridge = use_bridge
        if self.use_bridge:
            self._initialize_bridge(rnn_type, hidden_size, num_layers)
Exemplo n.º 2
0
    def __init__(self, cnn_kernel_width, rnn_type, enc_layers, dec_layers,
                 enc_rnn_size, dec_rnn_size, enc_pooling, dropout,
                 sample_rate, window_size):
        super(CRNNEncoder, self).__init__()

        self.cnn = ResNet(
            BasicBlock, cnn_kernel_width, num_classes=enc_rnn_size
        )
        self.enc_layers = enc_layers
        self.rnn_type = rnn_type
        self.dec_layers = dec_layers
        num_directions = 2
        self.num_directions = num_directions
        assert enc_rnn_size % num_directions == 0
        enc_rnn_size_real = enc_rnn_size // num_directions
        assert dec_rnn_size % num_directions == 0
        self.dec_rnn_size = dec_rnn_size
        dec_rnn_size_real = dec_rnn_size // num_directions
        self.dec_rnn_size_real = dec_rnn_size_real
        self.dec_rnn_size = dec_rnn_size
        # input_size = int(math.floor((sample_rate * window_size) / 2) + 1)
        # input_size = 1
        enc_pooling = enc_pooling.split(',')
        assert len(enc_pooling) == enc_layers or len(enc_pooling) == 1
        if len(enc_pooling) == 1:
            enc_pooling = enc_pooling * enc_layers
        enc_pooling = [int(p) for p in enc_pooling]
        self.enc_pooling = enc_pooling

        if dropout > 0:
            self.dropout = nn.Dropout(dropout)
        else:
            self.dropout = None
        self.W = nn.Linear(enc_rnn_size, dec_rnn_size, bias=False)
        self.batchnorm_0 = nn.BatchNorm1d(enc_rnn_size, affine=True)
        self.rnn_0, self.no_pack_padded_seq = \
            rnn_factory(rnn_type,
                        input_size=enc_rnn_size,
                        hidden_size=enc_rnn_size_real,
                        num_layers=1,
                        dropout=dropout,
                        bidirectional=True)
        self.pool_0 = nn.MaxPool1d(enc_pooling[0])
        for l in range(enc_layers - 1):
            batchnorm = nn.BatchNorm1d(enc_rnn_size, affine=True)
            rnn, _ = \
                rnn_factory(rnn_type,
                            input_size=enc_rnn_size,
                            hidden_size=enc_rnn_size_real,
                            num_layers=1,
                            dropout=dropout,
                            bidirectional=True)
            setattr(self, 'rnn_%d' % (l + 1), rnn)
            setattr(self, 'pool_%d' % (l + 1),
                    nn.MaxPool1d(enc_pooling[l + 1]))
            setattr(self, 'batchnorm_%d' % (l + 1), batchnorm)
Exemplo n.º 3
0
    def __init__(self, rnn_type, enc_layers, dec_layers, brnn, enc_rnn_size,
                 dec_rnn_size, enc_pooling, dropout, sample_rate, window_size,
                 enc_input_size):
        super(AudioEncoder, self).__init__()
        self.enc_layers = enc_layers
        self.rnn_type = rnn_type
        self.dec_layers = dec_layers
        num_directions = 2 if brnn else 1
        self.num_directions = num_directions
        assert enc_rnn_size % num_directions == 0
        enc_rnn_size_real = enc_rnn_size // num_directions
        assert dec_rnn_size % num_directions == 0
        self.dec_rnn_size = dec_rnn_size
        dec_rnn_size_real = dec_rnn_size // num_directions
        self.dec_rnn_size_real = dec_rnn_size_real
        self.dec_rnn_size = dec_rnn_size
        input_size = enc_input_size
        enc_pooling = enc_pooling.split(',')
        assert len(enc_pooling) == enc_layers or len(enc_pooling) == 1
        if len(enc_pooling) == 1:
            enc_pooling = enc_pooling * enc_layers
        enc_pooling = [int(p) for p in enc_pooling]
        self.enc_pooling = enc_pooling

        if type(dropout) is not list:
            dropout = [dropout]
        if max(dropout) > 0:
            self.dropout = nn.Dropout(dropout[0])
        else:
            self.dropout = None
        self.W = nn.Linear(enc_rnn_size, dec_rnn_size, bias=False)
        self.batchnorm_0 = nn.BatchNorm1d(enc_rnn_size, affine=True)
        self.rnn_0, self.no_pack_padded_seq = \
            rnn_factory(rnn_type,
                        input_size=input_size,
                        hidden_size=enc_rnn_size_real,
                        num_layers=1,
                        dropout=dropout[0],
                        bidirectional=brnn)
        self.pool_0 = nn.MaxPool1d(enc_pooling[0])
        for l in range(enc_layers - 1):
            batchnorm = nn.BatchNorm1d(enc_rnn_size, affine=True)
            rnn, _ = \
                rnn_factory(rnn_type,
                            input_size=enc_rnn_size,
                            hidden_size=enc_rnn_size_real,
                            num_layers=1,
                            dropout=dropout[0],
                            bidirectional=brnn)
            setattr(self, 'rnn_%d' % (l + 1), rnn)
            setattr(self, 'pool_%d' % (l + 1),
                    nn.MaxPool1d(enc_pooling[l + 1]))
            setattr(self, 'batchnorm_%d' % (l + 1), batchnorm)
Exemplo n.º 4
0
    def __init__(self, rnn_type, enc_layers, dec_layers, brnn,
                 enc_rnn_size, dec_rnn_size, enc_pooling, dropout,
                 sample_rate, window_size):
        super(AudioEncoder, self).__init__()
        self.enc_layers = enc_layers
        self.rnn_type = rnn_type
        self.dec_layers = dec_layers
        num_directions = 2 if brnn else 1
        self.num_directions = num_directions
        assert enc_rnn_size % num_directions == 0
        enc_rnn_size_real = enc_rnn_size // num_directions
        assert dec_rnn_size % num_directions == 0
        self.dec_rnn_size = dec_rnn_size
        dec_rnn_size_real = dec_rnn_size // num_directions
        self.dec_rnn_size_real = dec_rnn_size_real
        self.dec_rnn_size = dec_rnn_size
        input_size = int(math.floor((sample_rate * window_size) / 2) + 1)
        enc_pooling = enc_pooling.split(',')
        assert len(enc_pooling) == enc_layers or len(enc_pooling) == 1
        if len(enc_pooling) == 1:
            enc_pooling = enc_pooling * enc_layers
        enc_pooling = [int(p) for p in enc_pooling]
        self.enc_pooling = enc_pooling

        if dropout > 0:
            self.dropout = nn.Dropout(dropout)
        else:
            self.dropout = None
        self.W = nn.Linear(enc_rnn_size, dec_rnn_size, bias=False)
        self.batchnorm_0 = nn.BatchNorm1d(enc_rnn_size, affine=True)
        self.rnn_0, self.no_pack_padded_seq = \
            rnn_factory(rnn_type,
                        input_size=input_size,
                        hidden_size=enc_rnn_size_real,
                        num_layers=1,
                        dropout=dropout,
                        bidirectional=brnn)
        self.pool_0 = nn.MaxPool1d(enc_pooling[0])
        for l in range(enc_layers - 1):
            batchnorm = nn.BatchNorm1d(enc_rnn_size, affine=True)
            rnn, _ = \
                rnn_factory(rnn_type,
                            input_size=enc_rnn_size,
                            hidden_size=enc_rnn_size_real,
                            num_layers=1,
                            dropout=dropout,
                            bidirectional=brnn)
            setattr(self, 'rnn_%d' % (l + 1), rnn)
            setattr(self, 'pool_%d' % (l + 1),
                    nn.MaxPool1d(enc_pooling[l + 1]))
            setattr(self, 'batchnorm_%d' % (l + 1), batchnorm)
Exemplo n.º 5
0
    def __init__(self,
                 rnn_type,
                 num_layers,
                 hidden_size,
                 dropout=0.0,
                 gnmt=False,
                 embeddings=None):
        super(ResidualEncoder, self).__init__()
        assert embeddings is not None
        assert num_layers > 1
        self.embeddings = embeddings
        self.gnmt = gnmt
        self.num_layers = num_layers
        self.enc_reshape = True
        self.layers = nn.ModuleList()
        self.dropout = nn.Dropout(dropout)
        bottom_layers = 1
        if gnmt:
            bi_rnn, self.no_pack_padded_seq = \
                rnn_factory(rnn_type,
                            input_size=embeddings.embedding_size,
                            hidden_size=hidden_size,
                            num_layers=1,
                            bidirectional=True)
            rnn, _ = \
                rnn_factory(rnn_type,
                            input_size=hidden_size*2,
                            hidden_size=hidden_size,
                            num_layers=1,
                            bidirectional=False)
            self.layers.append(bi_rnn)
            self.layers.append(rnn)
            bottom_layers = 2
        else:
            rnn, self.no_pack_padded_seq=\
                rnn_factory(rnn_type,
                            input_size=embeddings.embedding_size,
                            hidden_size=hidden_size,
                            num_layers=1,
                            bidirectional=False)
            self.layers.append(rnn)

        for i in range(num_layers - bottom_layers):
            rnn, _= \
                rnn_factory(rnn_type,
                            input_size=hidden_size,
                            hidden_size=hidden_size,
                            num_layers=1,
                            bidirectional=False)
            self.layers.append(rnn)
    def __init__(self,
                 rnn_type,
                 bidirectional,
                 num_layers,
                 hidden_size,
                 dropout=0.0,
                 embeddings=None,
                 use_bridge=False,
                 device="cpu"):
        super(ConfnetEncoder, self).__init__()
        assert embeddings is not None

        self.embeddings = embeddings
        self.thetav = nn.Linear(in_features=self.embeddings.embedding_size,
                                out_features=self.embeddings.embedding_size)
        self.v_bar = nn.Linear(in_features=self.embeddings.embedding_size,
                               out_features=1)

        num_directions = 2 if bidirectional else 1
        assert hidden_size % num_directions == 0
        hidden_size = hidden_size // num_directions
        self.device = device
        self.rnn, self.no_pack_padded_seq = \
            rnn_factory(rnn_type,
                        input_size=embeddings.embedding_size,
                        hidden_size=hidden_size,
                        num_layers=num_layers,
                        dropout=dropout,
                        bidirectional=bidirectional)

        # Initialize the bridge layer
        self.use_bridge = use_bridge
        if self.use_bridge:
            self._initialize_bridge(rnn_type, hidden_size, num_layers)
Exemplo n.º 7
0
    def __init__(self,
                 rnn_type,
                 bidirectional,
                 num_layers,
                 hidden_size,
                 dropout=0.0,
                 embeddings=None,
                 use_bridge=False):
        super(RNNEncoder, self).__init__()
        assert embeddings is not None

        num_directions = 2 if bidirectional else 1
        assert hidden_size % num_directions == 0
        hidden_size = hidden_size // num_directions
        self.embeddings = embeddings

        self.rnn, self.no_pack_padded_seq = \
            rnn_factory(rnn_type,
                        input_size=embeddings.embedding_size,
                        hidden_size=hidden_size,
                        num_layers=num_layers,
                        dropout=dropout,
                        bidirectional=bidirectional)

        self.linear = nn.Linear(hidden_size * 4 * num_directions, 2)

        self.layer_norm = nn.LayerNorm(hidden_size * 4 * num_directions)
        self.batch_norm = nn.BatchNorm1d(hidden_size * 4 * num_directions)

        # Initialize the bridge layer
        self.use_bridge = use_bridge
        if self.use_bridge:
            self._initialize_bridge(rnn_type, hidden_size, num_layers)
Exemplo n.º 8
0
    def __init__(self,
                 rnn_type,
                 bidirectional,
                 num_layers,
                 hidden_size,
                 dropout=0.0,
                 embeddings=None,
                 use_bridge=False):
        super(TreeEncoder, self).__init__()
        assert embeddings is not None
        num_directions = 2 if bidirectional else 1
        assert hidden_size % num_directions == 0
        self.hidden_size = hidden_size // num_directions
        self.input_size = embeddings.embedding_size

        self.embeddings = embeddings
        self.rnn, self.no_pack_padded_seq = rnn_factory(
            rnn_type,
            input_size=self.input_size,
            hidden_size=self.hidden_size,
            num_layers=num_layers,
            dropout=dropout,
            bidirectional=bidirectional)
        self.treelstm = ChildSumTreeLSTM(rnn_type,
                                         self.input_size,
                                         self.hidden_size,
                                         bias=True)

        # Initialize the bridge layer
        self.use_bridge = use_bridge
        if self.use_bridge:
            self._initialize_bridge(rnn_type, self.hidden_size, num_layers)
Exemplo n.º 9
0
    def __init__(self,
                 rnn_type,
                 bidirectional,
                 num_layers,
                 hidden_size,
                 dropout=0.0,
                 embeddings=None,
                 use_bridge=False,
                 output_word_final=False):
        super(SeqHREncoder, self).__init__()
        assert embeddings is not None

        num_directions = 2 if bidirectional else 1
        assert hidden_size % num_directions == 0
        hidden_size = hidden_size // num_directions
        self.embeddings = embeddings

        self.word_rnn, self.word_no_pack_padded_seq = \
            rnn_factory(rnn_type,
                        input_size=embeddings.embedding_size,
                        hidden_size=hidden_size,
                        num_layers=num_layers,
                        dropout=dropout,
                        bidirectional=bidirectional)
        assert not self.word_no_pack_padded_seq

        self.sent_rnn, self.sent_no_pack_padded_seq = \
            rnn_factory(rnn_type,
                        input_size=hidden_size * num_directions,
                        hidden_size=hidden_size,
                        num_layers=num_layers,
                        dropout=dropout,
                        bidirectional=bidirectional)
        assert not self.sent_no_pack_padded_seq

        # add by wchen
        self.hidden_size = hidden_size
        self.num_layers = num_layers
        self.num_directions = num_directions
        self.output_word_final = output_word_final
        self.dropout = nn.Dropout(dropout)

        # Initialize the bridge layer
        self.use_bridge = use_bridge
        if self.use_bridge:
            self._initialize_bridge(rnn_type, hidden_size, num_layers)
Exemplo n.º 10
0
    def __init__(self,
                 wals_model,
                 rnn_type,
                 bidirectional,
                 num_layers,
                 hidden_size,
                 wals_size,
                 dropout=0.0,
                 embeddings=None,
                 use_bridge=False):
        super(RNNEncoder, self).__init__()
        assert embeddings is not None

        num_directions = 2 if bidirectional else 1
        assert hidden_size % num_directions == 0
        hidden_size = hidden_size // num_directions

        self.embeddings = embeddings
        self.wals_model = wals_model
        self.num_layers = num_layers
        self.wals_size = wals_size

        self.rnn, self.no_pack_padded_seq = \
            rnn_factory(rnn_type,
                        input_size=embeddings.embedding_size,
                        hidden_size=hidden_size,
                        num_layers=num_layers,
                        dropout=dropout,
                        bidirectional=bidirectional)

        if self.wals_model == 'WalstoSource_Target' or self.wals_model == 'WalstoSource_Both':

            self.rnn_wals, self.no_pack_padded_seq_wals = \
                rnn_factory(rnn_type,
                            input_size=hidden_size+2*self.wals_size,
                            hidden_size=hidden_size,
                            num_layers=num_layers,
                            dropout=dropout,
                            bidirectional=bidirectional)

        # Initialize the bridge layer
        self.use_bridge = use_bridge
        if self.use_bridge:
            self._initialize_bridge(rnn_type, hidden_size, num_layers)
Exemplo n.º 11
0
    def __init__(self,
                 rnn_type,
                 bidirectional,
                 num_layers,
                 hidden_size,
                 dropout=0.0,
                 embeddings=None,
                 use_bridge=False,
                 separate_layers=False):
        super(RNNEncoder, self).__init__()
        assert embeddings is not None

        num_directions = 2 if bidirectional else 1
        assert hidden_size % num_directions == 0
        hidden_size = hidden_size // num_directions
        self.embeddings = embeddings
        self.separate_layers = separate_layers

        if separate_layers:
            layers = []
            for n in range(num_layers):
                layer_n, self.no_pack_padded_seq = rnn_factory(rnn_type,
                        input_size=(embeddings.embedding_size if n == 0 else \
                                hidden_size * (2 if bidirectional else 1)),
                        hidden_size=hidden_size,
                        num_layers=1,
                        dropout=dropout,
                        bidirectional=bidirectional)
                layers.append(layer_n)
            self.layers = nn.ModuleList(layers)
            self.dropout = nn.Dropout(dropout)
        else:
            self.rnn, self.no_pack_padded_seq = \
                rnn_factory(rnn_type,
                            input_size=embeddings.embedding_size,
                            hidden_size=hidden_size,
                            num_layers=num_layers,
                            dropout=dropout,
                            bidirectional=bidirectional)

        # Initialize the bridge layer
        self.use_bridge = use_bridge
        if self.use_bridge:
            self._initialize_bridge(rnn_type, hidden_size, num_layers)
Exemplo n.º 12
0
 def __init__(self, rnn_type, hidden_size, dropout=0.0, filed=None):
     super(SpeakerGenerator, self).__init__()
     num_directions = 2
     assert hidden_size % num_directions == 0
     hidden_size = hidden_size // num_directions
     self.rnn, self.no_pack_padded_seq = \
         rnn_factory(rnn_type,
                     input_size=hidden_size*num_directions,
                     hidden_size=hidden_size,
                     num_layers=1,
                     dropout=dropout,
                     bidirectional=True)
     self.out = nn.Bilinear(hidden_size, hidden_size, len(filed))
     self.gen = nn.Sequential(Cast(torch.float32), nn.LogSoftmax(dim=-1))
Exemplo n.º 13
0
    def __init__(self, rnn_type, bidirectional, num_layers,
                 hidden_size, dropout=0.0, emb_size=300):
        super(RNNEncoder, self).__init__()

        num_directions = 2 if bidirectional else 1
        assert hidden_size % num_directions == 0
        hidden_size = hidden_size // num_directions

        self.rnn, self.no_pack_padded_seq = \
            rnn_factory(rnn_type,
                        input_size=emb_size,
                        hidden_size=hidden_size,
                        num_layers=num_layers,
                        dropout=dropout,
                        bidirectional=bidirectional)
Exemplo n.º 14
0
    def __init__(self, rnn_type, bidirectional, num_layers,
                 hidden_size, single_enc_double_dec_layers, dropout=0.0, embeddings=None):
        super(RNNEncoder, self).__init__()
        assert embeddings is not None

        num_directions = 2 if bidirectional else 1
        if single_enc_double_dec_layers == False:
            assert hidden_size % num_directions == 0
            hidden_size = hidden_size // num_directions
        self.enc_reshape = not single_enc_double_dec_layers
        self.embeddings = embeddings
        self.hidden_size=hidden_size
        self.rnn, self.no_pack_padded_seq = \
            rnn_factory(rnn_type,
                        input_size=embeddings.embedding_size,
                        hidden_size=hidden_size,
                        num_layers=num_layers,
                        dropout=dropout,
                        bidirectional=bidirectional)
    def __init__(self, rnn_type, bidirectional, num_layers,
                 hidden_size, dropout=0.0, embeddings=None,
                 use_bridge=False, bidir_treelstm=False):
        super(TreeLSTMEncoder, self).__init__()
        assert embeddings is not None

        num_directions = 2 if bidirectional else 1
        assert hidden_size % num_directions == 0
        hidden_size = hidden_size // num_directions
        self.embeddings = embeddings

        use_rnn = bidirectional
        self.rnn = None
        if use_rnn:        
            self.rnn, self.no_pack_padded_seq = \
                rnn_factory(rnn_type,
                            input_size=embeddings.embedding_size,
                            hidden_size=hidden_size,
                            num_layers=num_layers,
                            dropout=dropout,
                            bidirectional=bidirectional)
        
        self.bidir_treelstm = bidir_treelstm
        if self.bidir_treelstm:
            self.childsumtreelstm = ChildSumTreeLSTM(
                embeddings.embedding_size,
                embeddings.embedding_size//2)

            self.topdown = TopDownTreeLSTM(
                embeddings.embedding_size//2,
                embeddings.embedding_size//2)
        else:
            self.childsumtreelstm = ChildSumTreeLSTM(
                embeddings.embedding_size,
                embeddings.embedding_size)                

        # Initialize the bridge layer
        self.use_bridge = use_bridge
        if self.use_bridge:
            self._initialize_bridge(rnn_type,
                                    hidden_size,
                                    num_layers)
Exemplo n.º 16
0
    def __init__(self, rnn_type, bidirectional, num_layers,
                 hidden_size, dropout=0.0, embeddings=None, sel_classifier='simple_fc', detach_sel_probs=False):
        super(RNNSelector, self).__init__()
        assert embeddings is not None
        assert bidirectional

        num_directions = 2 if bidirectional else 1
        assert hidden_size % num_directions == 0
        hidden_size = hidden_size // num_directions
        self.hidden_size = hidden_size
        self.embeddings = embeddings

        self.detach_sel_probs = detach_sel_probs

        self.rnn, self.no_pack_padded_seq = \
            rnn_factory(rnn_type,
                        input_size=embeddings.embedding_size,
                        hidden_size=hidden_size,
                        num_layers=num_layers,
                        dropout=dropout if num_layers > 1 else 0.0,
                        bidirectional=bidirectional)

        # add a simplest classifier first to build the whole framework
        # activation function: sigmoid
        self.sel_classifier = sel_classifier
        if sel_classifier == 'simple_fc':
            self.word_feats_linear = nn.Linear(2 * hidden_size, hidden_size)
            self.simple_classifier = nn.Linear(hidden_size, 1)
        else:
            # for complex_Nallapati classifier, refer to https://arxiv.org/pdf/1611.04230.pdf
            self.word_feats_linear = nn.Linear(2 * hidden_size, hidden_size)
            self.art_feats_linear = nn.Linear(2 * hidden_size, hidden_size)

            self.w_content = nn.Linear(hidden_size, 1)
            self.w_salience = nn.Linear(hidden_size, hidden_size, bias=False)
            self.w_novelty = nn.Linear(hidden_size, hidden_size, bias=False)

        self.dropout_m = nn.Dropout(p=dropout)
        self.tanh = nn.Tanh()
        self.sigmoid = nn.Sigmoid()
Exemplo n.º 17
0
    def __init__(self,
                 rnn_type,
                 bidirectional,
                 num_layers,
                 hidden_size,
                 input_size,
                 dropout=0.0):
        super(FusionEncoder, self).__init__()

        self.sort_data = True

        num_directions = 2 if bidirectional else 1
        assert hidden_size % num_directions == 0
        hidden_size = hidden_size // num_directions

        self.rnn, self.no_pack_padded_seq = \
            rnn_factory(rnn_type,
                        input_size=input_size,
                        hidden_size=hidden_size,
                        num_layers=num_layers,
                        dropout=dropout,
                        bidirectional=bidirectional)
Exemplo n.º 18
0
    def __init__(self, rnn_type, bidirectional, num_layers,
                 hidden_size, dropout=0.0, embeddings=None,
                 use_bridge=False, gcn_dropout=0.0,
                 gcn_edge_dropout=0.0, n_gcn_layers=1, activation='',
                 highway=''):
                    
        super(GCNEncoder, self).__init__()
        assert embeddings is not None

        num_directions = 2 if bidirectional else 1
        assert hidden_size % num_directions == 0
        hidden_size = hidden_size // num_directions
        self.embeddings = embeddings
        
        use_rnn = bidirectional
        self.rnn = None        
        if use_rnn:
            self.rnn, self.no_pack_padded_seq = \
                rnn_factory(rnn_type,
                            input_size=embeddings.embedding_size,
                            hidden_size=hidden_size,
                            num_layers=num_layers,
                            dropout=dropout,
                            bidirectional=bidirectional)
            
        self.gcn = GraphConvolution(embeddings.embedding_size,
            embeddings.embedding_size,
            gcn_dropout,
            gcn_edge_dropout,
            n_gcn_layers,
            activation,
            highway)

        # Initialize the bridge layer
        self.use_bridge = use_bridge
        if self.use_bridge:
            self._initialize_bridge(rnn_type,
                                    hidden_size,
                                    num_layers)
Exemplo n.º 19
0
    def __init__(self,
                 rnn_type,
                 bidirectional,
                 num_layers,
                 hidden_size,
                 dropout=0.0,
                 embeddings=None,
                 use_bridge=False):
        super(RNNEncoder, self).__init__()
        assert embeddings is not None

        num_directions = 2 if bidirectional else 1
        assert hidden_size % num_directions == 0
        hidden_size = hidden_size // num_directions
        self.embeddings = embeddings

        self.rnn, self.no_pack_padded_seq = \
            rnn_factory(rnn_type,
                        input_size=embeddings.embedding_size,
                        hidden_size=hidden_size,
                        num_layers=num_layers, # this is 1.
                        dropout=dropout,
                        bidirectional=bidirectional)
        # self.rnn is a LSTM(128, 256, bidirectional=True)  # input dim; output dim;

        # init another sentence-level layer: this is shared by all the sentences, why 4?
        self.sent_rnn = nn.LSTM(4 * embeddings.embedding_size,
                                hidden_size,
                                num_layers=1,
                                dropout=dropout,
                                bidirectional=True)
        # (512,256)
        # import pdb;pdb.set_trace()

        # Initialize the bridge layer
        self.use_bridge = use_bridge
        if self.use_bridge:
            self._initialize_bridge(rnn_type, hidden_size, num_layers)
Exemplo n.º 20
0
 def _build_rnn(self, rnn_type, **kwargs):
     rnn, _ = rnn_factory(rnn_type, **kwargs)
     return rnn
Exemplo n.º 21
0
    def __init__(self, rnn_type, enc_layers, dec_layers, brnn, input_size,
                 enc_rnn_size, dec_rnn_size, enc_pooling, dropout, ctc_ratio,
                 tgt_vocab_size, sample_rate, window_size, ctc_out_layer):
        super(AudioEncoder, self).__init__()
        self.enc_layers = enc_layers
        self.rnn_type = rnn_type
        self.dec_layers = dec_layers
        if ctc_out_layer == -1:
            ctc_out_layer = enc_layers - 1
        self.ctc_out_layer = ctc_out_layer
        num_directions = 2 if brnn else 1
        self.num_directions = num_directions
        assert enc_rnn_size % num_directions == 0
        enc_rnn_size_real = enc_rnn_size // num_directions
        assert dec_rnn_size % num_directions == 0
        self.dec_rnn_size = dec_rnn_size
        dec_rnn_size_real = dec_rnn_size // num_directions
        self.dec_rnn_size_real = dec_rnn_size_real
        self.dec_rnn_size = dec_rnn_size
        enc_pooling = enc_pooling.split(',')
        assert len(enc_pooling) == enc_layers or len(enc_pooling) == 1
        if len(enc_pooling) == 1:
            enc_pooling = enc_pooling * enc_layers
        enc_pooling = [int(p) for p in enc_pooling]
        self.enc_pooling = enc_pooling

        if dropout > 0:
            self.dropout = nn.Dropout(dropout)
        else:
            self.dropout = None
        self.W = nn.Linear(enc_rnn_size, dec_rnn_size, bias=False)
        self.batchnorm_0 = nn.BatchNorm1d(enc_rnn_size, affine=True)
        self.pyramid = False

        enc_rnn_size_real_0 = enc_rnn_size_real
        if self.pyramid:
            enc_rnn_size_real_0 = enc_rnn_size_real / enc_pooling[0]
        self.rnn_0, self.no_pack_padded_seq = \
            rnn_factory(rnn_type,
                        input_size=input_size,
                        hidden_size=enc_rnn_size_real_0,
                        num_layers=1,
                        dropout=dropout,
                        bidirectional=brnn)
        self.pool_0 = nn.MaxPool1d(enc_pooling[0])
        for l in range(enc_layers - 1):
            enc_rnn_size_real_l = enc_rnn_size_real
            if self.pyramid:
                enc_rnn_size_real_l = enc_rnn_size_real / enc_pooling[l]
            batchnorm = nn.BatchNorm1d(enc_rnn_size, affine=True)
            rnn, _ = \
                rnn_factory(rnn_type,
                            input_size=enc_rnn_size,
                            hidden_size=enc_rnn_size_real_l,
                            num_layers=1,
                            dropout=dropout,
                            bidirectional=brnn)
            setattr(self, 'rnn_%d' % (l + 1), rnn)
            setattr(self, 'pool_%d' % (l + 1),
                    nn.MaxPool1d(enc_pooling[l + 1]))
            setattr(self, 'batchnorm_%d' % (l + 1), batchnorm)

        if ctc_ratio > 0:
            gen_func = nn.LogSoftmax(dim=-1)
            self.ctc_gen = nn.Sequential(
                nn.Linear(enc_rnn_size, tgt_vocab_size), gen_func)
Exemplo n.º 22
0
 def _build_rnn(self, rnn_type, **kwargs):
     rnn, _ = rnn_factory(rnn_type, **kwargs)
     return rnn
Exemplo n.º 23
0
    def __init__(self, rnn_type, bidirectional, num_layers, hidden_size,
                 dropout, embeddings):
        super(TGEncoder, self).__init__()

        assert bidirectional
        # In current version, only one BiGRU layer for context encoding, one BiGRU layer for title encoding
        # one BiGRU layer for merging.
        # We regard num_layers as 2 since context encoding and title encoding are at the same level.
        assert num_layers == 2
        self.rnn_type = rnn_type
        hidden_size = hidden_size // 2
        self.real_hidden_size = hidden_size
        # self.no_pack_padded_seq = False
        self.bidirectional = bidirectional
        # TODO: set res_ratio as an argument
        self.res_ratio = 0.5

        self.embeddings = embeddings

        # One BiGRU layer for context encoding
        self.src_rnn, self.src_no_pack_padded_seq = \
            rnn_factory(rnn_type,
                        input_size=embeddings.embedding_size,
                        hidden_size=hidden_size,
                        num_layers=1,
                        bidirectional=bidirectional)
        # self.src_rnn = getattr(nn, rnn_type)(
        #     input_size=embeddings.embedding_size,
        #     hidden_size=hidden_size,
        #     num_layers=1,
        #     bidirectional=self.bidirectional)

        # One BiGRU layer for title encoding
        self.query_rnn, self.query_no_pack_padded_seq = \
            rnn_factory(rnn_type,
                        input_size=embeddings.embedding_size,
                        hidden_size=hidden_size,
                        num_layers=1,
                        bidirectional=bidirectional)
        # self.query_rnn = getattr(nn, rnn_type)(
        #     input_size=embeddings.embedding_size,
        #     hidden_size=hidden_size,
        #     num_layers=1,
        #     bidirectional=self.bidirectional)

        # One BiGRU layer for merging
        self.merge_rnn, self.merge_no_pack_padded_seq = \
            rnn_factory(rnn_type,
                        input_size=4 * hidden_size,
                        hidden_size=hidden_size,
                        num_layers=1,
                        bidirectional=bidirectional)
        # self.merge_rnn = getattr(nn, rnn_type)(
        #     input_size=4 * hidden_size,
        #     hidden_size=hidden_size,
        #     num_layers=1,
        #     bidirectional=self.bidirectional)

        self.match_fc = nn.Linear(2 * hidden_size, 2 * hidden_size, bias=False)

        self.dropout = nn.Dropout(p=dropout)