def nsc_document_layer(x, max_doc_len, doc_len, identities, hidden_size, doc_hop_cnt=1, bidirectional_lstm=True, lstm_cells=None, auged=False, attention_type='additive'): outputs = [] with tf.variable_scope('document_layer'): # lstm_outputs, _state = lstm(outputs, doc_len, hidden_size, 'lstm') lstm_bkg, _state = lstm(x, doc_len, hidden_size, 'lstm_bkg', bidirectional=bidirectional_lstm, lstm_cells=lstm_cells) lstm_outputs = lstm_bkg doc_bkg = [i for i in identities] for ihop in range(doc_hop_cnt): if doc_bkg: doc_bkg[0] = hop('hop', lstm_outputs, lstm_bkg, doc_bkg[0], doc_bkg[1:], doc_len, max_doc_len, '', auged=auged, attention_type=attention_type) output = doc_bkg[0] else: output = hop('hop', lstm_outputs, lstm_bkg, None, None, doc_len, max_doc_len, '', auged=auged, attention_type=attention_type) outputs.append(output) outputs = tf.concat(outputs, axis=-1) return outputs
def dnsc(self, x, max_sen_len, max_doc_len, sen_len, doc_len, identities): x = tf.reshape(x, [-1, max_sen_len, self.emb_dim]) sen_len = tf.reshape(sen_len, [-1]) def lstm(inputs, sequence_length, hidden_size, scope): cell_fw = tf.nn.rnn_cell.LSTMCell(hidden_size // 2, forget_bias=0., initializer=xavier()) cell_bw = tf.nn.rnn_cell.LSTMCell(hidden_size // 2, forget_bias=0., initializer=xavier()) outputs, state = tf.nn.bidirectional_dynamic_rnn( cell_fw=cell_fw, cell_bw=cell_bw, inputs=inputs, sequence_length=sequence_length, dtype=tf.float32, scope=scope) outputs = tf.concat(outputs, axis=2) return outputs, state with tf.variable_scope('sentence_layer'): # lstm_outputs, _state = lstm(x, sen_len, self.hidden_size, 'lstm') # lstm_outputs = tf.reshape(lstm_outputs, [-1, max_sen_len, self.hidden_size]) lstm_bkg, _state = lstm(x, sen_len, self.hidden_size, 'lstm_bkg') lstm_bkg = tf.reshape(lstm_bkg, [-1, max_sen_len, self.hidden_size]) lstm_outputs = lstm_bkg sen_bkg = [ tf.reshape(tf.tile(bkg[:, None, :], (1, max_doc_len, 1)), (-1, self.hidden_size)) for bkg in identities ] sen_bkg = hop('attention', lstm_outputs, lstm_bkg, sen_bkg[0], sen_bkg[1:], sen_len, max_sen_len, '') outputs = tf.reshape(sen_bkg, [-1, max_doc_len, self.hidden_size]) with tf.variable_scope('document_layer'): # lstm_outputs, _state = lstm(outputs, doc_len, self.hidden_size, 'lstm') lstm_bkg, _state = lstm(outputs, doc_len, self.hidden_size, 'lstm_bkg') lstm_outputs = lstm_bkg doc_bkg = [i for i in identities] doc_bkg = hop('attention', lstm_outputs, lstm_bkg, doc_bkg[0], doc_bkg[1:], doc_len, max_doc_len, '') outputs = doc_bkg return outputs
def nsc_sentence_layer(x, max_sen_len, max_doc_len, sen_len, identities, hidden_size, emb_dim, sen_hop_cnt=1, bidirectional_lstm=True, lstm_cells=None, auged=False, attention_type='additive'): x = tf.reshape(x, [-1, max_sen_len, x.shape[-1]]) sen_len = tf.reshape(sen_len, [-1]) outputs = [] with tf.variable_scope('sentence_layer'): # lstm_outputs, _state = lstm(x, sen_len, hidden_size, 'lstm') # lstm_outputs = tf.reshape(lstm_outputs, [-1, max_sen_len, hidden_size]) lstm_bkg, _state = lstm(x, sen_len, hidden_size, 'lstm_bkg', bidirectional=bidirectional_lstm, lstm_cells=lstm_cells) lstm_bkg = tf.reshape(lstm_bkg, [-1, max_sen_len, hidden_size]) lstm_outputs = lstm_bkg sen_bkg = [ tf.reshape(tf.tile(bkg[:, None, :], (1, max_doc_len, 1)), (-1, emb_dim)) for bkg in identities ] for ihop in range(sen_hop_cnt): if sen_bkg: sen_bkg[0] = hop('hop', lstm_outputs, lstm_bkg, sen_bkg[0], sen_bkg[1:], sen_len, max_sen_len, '', auged=auged, attention_type=attention_type) output = sen_bkg[0] else: output = hop('hop', lstm_outputs, lstm_bkg, None, None, sen_len, max_sen_len, '', auged=auged, attention_type=attention_type) outputs.append(output) # outputs = tf.concat(outputs, axis=-1) # outputs = tf.layers.dense(outputs, hidden_size, use_bias=False) outputs = tf.reshape(output, [-1, max_doc_len, hidden_size]) return outputs