在注意力和CRF层上使用Bi-LSTM

时间:2019-10-07 18:35:33

标签: python-3.x tensorflow deep-learning

我正在执行命名实体识别任务。到目前为止,我已经编写了直到关注层的代码,但是之后我不知道如何继续。我希望我的模型以这样一种方式进行:将其输入到关注层之后的CRF层中。 下面是我的代码段

with tf.variable_scope("bi-lstm"):
        cell_fw = tf.contrib.rnn.LSTMCell(self.config.hidden_size)
        cell_bw = tf.contrib.rnn.LSTMCell(self.config.hidden_size)
        (output_fw, output_bw), _ = tf.nn.bidirectional_dynamic_rnn(cell_fw, 
            cell_bw, self.word_embeddings, sequence_length=self.sequence_lengths, 
            dtype=tf.float32)

        W = tf.Variable(tf.random_normal([self.config.hidden_size], stddev=0.1))
        H = output_fw + output_bw  # (batch_size, seq_len, HIDDEN_SIZE)
        M = tf.tanh(H)  # M = tanh(H)  (batch_size, seq_len, HIDDEN_SIZE)

        self.alpha = tf.nn.softmax(tf.reshape(tf.matmul(tf.reshape(M, [-1, self.config.hidden_size]),
                                                    tf.reshape(W, [-1, 1])), (-1, self.max_len)))  # batch_size x seq_len

        r = tf.matmul(tf.transpose(H, [0, 2, 1]),tf.reshape(self.alpha, [-1, self.max_len, 1]))
        r = tf.squeeze(r)
        h_star = tf.tanh(r)  # (batch , HIDDEN_SIZE)

        h_drop = tf.nn.dropout(h_star, self.dropout)

我已经看到了一些CRF层实现,并且所有这些实现都需要某种Logit张量来使用(例如example)。所以我想知道如何生成这样的logit张量。

0 个答案:

没有答案