Пример #1
0
    def init_context_embedding(self, embedded_terms):
        assert (isinstance(self.Config, RNNConfig))

        with tf.name_scope("rnn"):

            # Length Calculation
            x_length = sequence.calculate_sequence_length(
                self.get_input_parameter(InputSample.I_X_INDS))
            s_length = tf.cast(x=tf.maximum(x_length, 1), dtype=tf.int32)

            # Forward cell
            cell = sequence.get_cell(
                hidden_size=self.Config.HiddenSize,
                cell_type=self.Config.CellType,
                dropout_rnn_keep_prob=self.__dropout_rnn_keep_prob)

            # Output
            all_outputs, _ = sequence.rnn(cell=cell,
                                          inputs=embedded_terms,
                                          sequence_length=s_length,
                                          dtype=tf.float32)

            h_outputs = sequence.select_last_relevant_in_sequence(
                all_outputs, s_length)

        return h_outputs
Пример #2
0
    def calculate_keys_length_func(self, keys):
        scalar_length = super(InteractiveMLPAttention,
                              self).calculate_keys_length_func(keys)

        self.__dynamic_lens = sequence.calculate_sequence_length(
            sequence=keys, is_neg_placeholder=InputSample.FRAMES_PAD_VALUE < 0)

        return scalar_length
Пример #3
0
    def init_context_embedding(self, embedded_terms):
        assert (isinstance(self.Config, RCNNConfig))
        text_length = sequence.calculate_sequence_length(
            self.get_input_parameter(InputSample.I_X_INDS))

        with tf.name_scope("bi-rnn"):

            fw_cell = sequence.get_cell(
                hidden_size=self.Config.SurroundingOneSideContextEmbeddingSize,
                cell_type=self.Config.CellType,
                dropout_rnn_keep_prob=self.__dropout_rnn_keep_prob)

            bw_cell = sequence.get_cell(
                hidden_size=self.Config.SurroundingOneSideContextEmbeddingSize,
                cell_type=self.Config.CellType,
                dropout_rnn_keep_prob=self.__dropout_rnn_keep_prob)

            (output_fw, output_bw), states = sequence.bidirectional_rnn(
                cell_fw=fw_cell,
                cell_bw=bw_cell,
                inputs=embedded_terms,
                sequence_length=text_length,
                dtype=tf.float32)

            output_fw, output_bw = self.modify_rnn_outputs_optional(
                output_fw, output_bw)

        with tf.name_scope("ctx"):
            shape = [tf.shape(output_fw)[0], 1, tf.shape(output_fw)[2]]
            c_left = tf.concat([tf.zeros(shape), output_fw[:, :-1]],
                               axis=1,
                               name="context_left")
            c_right = tf.concat(
                [output_bw[:, 1:], tf.zeros(shape)],
                axis=1,
                name="context_right")

        with tf.name_scope("word-representation"):
            merged = tf.concat([c_left, embedded_terms, c_right],
                               axis=2,
                               name="merged")

        with tf.name_scope("text-representation"):
            y2 = tf.tanh(
                tf.einsum('aij,jk->aik', merged, self.__hidden[self.H_W_text])
                + self.__hidden[self.H_b_text])

        with tf.name_scope("max-pooling"):
            y3 = tf.reduce_max(y2, axis=1)

        return y3
Пример #4
0
    def init_context_embedding(self, embedded_terms):
        assert (isinstance(self.Config, SelfAttentionBiLSTMConfig))

        # Bidirectional(Left&Right) Recurrent Structure
        with tf.name_scope("bi-lstm"):
            x_length = sequence.calculate_sequence_length(
                self.get_input_parameter(InputSample.I_X_INDS))
            s_length = tf.cast(x=tf.maximum(x_length, 1), dtype=tf.int32)

            fw_cell = sequence.get_cell(
                hidden_size=self.Config.HiddenSize,
                cell_type=self.Config.CellType,
                dropout_rnn_keep_prob=self.__dropout_rnn_keep_prob)

            bw_cell = sequence.get_cell(
                hidden_size=self.Config.HiddenSize,
                cell_type=self.Config.CellType,
                dropout_rnn_keep_prob=self.__dropout_rnn_keep_prob)

            (self.output_fw,
             self.output_bw), states = sequence.bidirectional_rnn(
                 cell_fw=fw_cell,
                 cell_bw=bw_cell,
                 inputs=embedded_terms,
                 sequence_length=s_length,
                 dtype=tf.float32)
            H = tf.concat([self.output_fw, self.output_bw], axis=2)
            H_reshape = tf.reshape(H, [-1, 2 * self.Config.HiddenSize])

        with tf.name_scope("self-attention"):
            _H_s1 = tf.nn.tanh(tf.matmul(H_reshape, self.__W_s1))
            _H_s2 = tf.matmul(_H_s1, self.__W_s2)
            _H_s2_reshape = tf.transpose(tf.reshape(
                _H_s2, [-1, self.Config.TermsPerContext, self.Config.RSize]),
                                         perm=[0, 2, 1])

            self.__A = tf.nn.softmax(_H_s2_reshape, name="attention")
            self.__avg_by_r_A = tf.reduce_mean(self.__A, axis=-2)

        with tf.name_scope("sentence-embedding"):
            # M shape (r, 2u)
            M = tf.matmul(self.__A, H)

        # M_flat (batch_size, r * 2u)
        return tf.reshape(M, shape=[-1, self.ContextEmbeddingSize])
Пример #5
0
    def init_context_embedding(self, embedded_terms):
        assert(isinstance(embedded_terms, list))

        context_embedded, aspects_embedded = embedded_terms

        with tf.name_scope('dynamic_rnn'):

            # Prepare cells
            aspect_cell = get_cell(hidden_size=self.Config.HiddenSize,
                                   cell_type=self.Config.CellType,
                                   dropout_rnn_keep_prob=self.__dropout_rnn_keep_prob)

            context_cell = get_cell(hidden_size=self.Config.HiddenSize,
                                    cell_type=self.Config.CellType,
                                    dropout_rnn_keep_prob=self.__dropout_rnn_keep_prob)

            # Calculate input lengths
            aspect_lens = sequence.calculate_sequence_length(
                sequence=self.get_aspect_input(),
                is_neg_placeholder=InputSample.FRAMES_PAD_VALUE < 0)

            aspect_lens_casted = tf.cast(x=tf.maximum(aspect_lens, 1), dtype=tf.int32)

            context_lens = sequence.calculate_sequence_length(
                sequence=self.get_input_parameter(InputSample.I_X_INDS))

            context_lens_casted = tf.cast(x=tf.maximum(context_lens, 1), dtype=tf.int32)

            # Receive aspect output
            aspect_outputs, _ = sequence.rnn(cell=aspect_cell,
                                             inputs=aspects_embedded,
                                             sequence_length=aspect_lens_casted,
                                             dtype=tf.float32,
                                             scope='aspect_outputs')
            aspect_avg = self.__aggreagate(self.Config,
                                           outputs=aspect_outputs,
                                           length=aspect_lens_casted)

            # Receive context output
            context_outputs, _ = sequence.rnn(cell=context_cell,
                                              inputs=context_embedded,
                                              sequence_length=context_lens_casted,
                                              dtype=tf.float32,
                                              scope='context_outputs')
            context_avg = self.__aggreagate(self.Config,
                                            outputs=context_outputs,
                                            length=context_lens_casted)

            # Attention for aspects
            self.__aspect_att = tf.nn.softmax(
                tf.nn.tanh(tf.einsum('ijk,kl,ilm->ijm', aspect_outputs, self.__w_a,
                                     tf.expand_dims(context_avg, -1)) + self.__b_a),
                axis=1)
            aspect_rep = tf.reduce_sum(self.__aspect_att * aspect_outputs, axis=1)

            # Attention for context
            self.__context_att = tf.nn.softmax(
                tf.nn.tanh(tf.einsum('ijk,kl,ilm->ijm', context_outputs, self.__w_c,
                                     tf.expand_dims(aspect_avg, -1)) + self.__b_c),
                axis=1)
            context_rep = tf.reduce_sum(self.__context_att * context_outputs, axis=1)

            return tf.concat([context_rep, aspect_rep], 1)