Exemple #1
0
def calc_join_and_meet(t1_box, t2_box):
    """
    # two boxes are t1_box, t2_box
    # the corresponding embeddings are t1_min_embed, t1_max_embed, t2_min_embed, t2_max_embed
    Returns:
        join box, min box, and disjoint condition:
    """
    # join is min value of (a, c), max value of (b, d)
    t1_min_embed = t1_box.min_embed
    t1_max_embed = t1_box.max_embed
    t2_min_embed = t2_box.min_embed
    t2_max_embed = t2_box.max_embed
    join_min = tf.minimum(t1_min_embed, t2_min_embed)
    join_max = tf.maximum(t1_max_embed, t2_max_embed)
    # find meet is calculate the max value of (a,c), min value of (b,d)
    meet_min = tf.maximum(t1_min_embed, t2_min_embed)  # batchsize * embed_size
    meet_max = tf.minimum(t1_max_embed, t2_max_embed)  # batchsize * embed_size
    # The overlap cube's max value have to be bigger than min value in every dimension to form a valid cube
    # if it's not, then two concepts are disjoint, return none
    cond = tf.cast(tf.less_equal(meet_max, meet_min),
                   tf.float32)  # batchsize * embed_size
    cond = tf.cast(tf.reduce_sum(cond, axis=1),
                   tf.bool)  # batchsize. If disjoint, cond > 0; else, cond = 0
    meet_box = MyBox(meet_min, meet_max)
    join_box = MyBox(join_min, join_max)
    return join_box, meet_box, cond
Exemple #2
0
 def softbox_marg(self, features, params, mode):
     """marg log probability"""
     max_embed = self.min_embed + tf.exp(self.delta_embed)
     universe_min = tf.reduce_min(self.min_embed, axis=0, keepdims=True)
     universe_max = tf.reduce_max(max_embed, axis=0, keepdims=True)
     universe_volume = volume_calculation(MyBox(universe_min, universe_max),
                                          self.temperature)
     box_volume = volume_calculation(MyBox(self.min_embed, max_embed),
                                     self.temperature)
     predicted_marginal_logits = tf.log(box_volume) - tf.log(
         universe_volume)
     return predicted_marginal_logits
Exemple #3
0
def get_word_embedding(idx, min_embed, delta_embed):
    """read word embedding from embedding table, get unit cube embeddings"""
    min_embed = tf.nn.embedding_lookup(min_embed, idx)
    delta_embed = tf.nn.embedding_lookup(delta_embed,
                                         idx)  # [batch_size, embed_size]
    max_embed = min_embed + tf.exp(delta_embed)
    t1_box = MyBox(min_embed, max_embed)
    return t1_box
Exemple #4
0
    def __init__(self, placeholder, FLAGS):
        self.label_size = FLAGS.label_size
        self.embed_dim = FLAGS.embed_dim
        self.cond_weight = FLAGS.cond_weight
        self.marg_weight = FLAGS.marg_weight
        self.reg_weight = FLAGS.reg_weight
        self.regularization_method = FLAGS.regularization_method
        self.temperature = 1.0

        self.t1x = placeholder['t1_idx_placeholder']  #[batch_size]
        self.t2x = placeholder['t2_idx_placeholder']  #[batch_size]
        self.label = placeholder['label_placeholder']  #[batch_size]
        self.marginal_label = placeholder[
            'marginal_label_placeholder']  # [label_size]

        # self.t1x = tf.Print(self.t1x, [tf.shape(self.t1x), tf.shape(self.t2x), tf.shape(self.label), tf.shape(self.marginal_label)], 'shape')
        """Initiate box embeddings"""
        self.min_embed, self.delta_embed = self.init_word_embedding()
        """For training"""
        self.t1_box = self.get_word_embedding(self.t1x)
        self.t2_box = self.get_word_embedding(self.t2x)
        conditional_logits, self.meet_box, self.disjoint,\
        self.nested, self.overlap_volume, self.rhs_volume = self.get_conditional_probability(self.t1_box, self.t2_box)
        evaluation_logits, _, _, _, _, _ = self.get_conditional_probability(
            self.t1_box, self.t2_box)
        self.neg_log_prob = -evaluation_logits
        """get conditional probability loss"""
        cond_pos_loss = tf.multiply(conditional_logits, self.label)
        cond_neg_loss = tf.multiply(
            tf.log(1 - tf.exp(conditional_logits) + 1e-10), 1 - self.label)
        cond_loss = -tf.reduce_mean(cond_pos_loss + cond_neg_loss)
        self.cond_loss = self.cond_weight * cond_loss
        """model marg prob loss"""
        if self.marg_weight > 0.0:
            # prediction
            self.max_embed = self.min_embed + tf.exp(self.delta_embed)
            self.universe_min = tf.reduce_min(self.min_embed,
                                              axis=0,
                                              keepdims=True)
            self.universe_max = tf.reduce_max(self.max_embed,
                                              axis=0,
                                              keepdims=True)
            self.universe_volume = self.volume_calculation(
                MyBox(self.universe_min, self.universe_max))
            self.box_volume = self.volume_calculation(
                MyBox(self.min_embed, self.max_embed))
            self.predicted_marginal_logits = tf.log(self.box_volume) - tf.log(
                self.universe_volume)
            # marginal loss
            marg_pos_loss = tf.multiply(self.predicted_marginal_logits,
                                        self.marginal_label)
            marg_neg_loss = tf.multiply(
                tf.log(1 - tf.exp(self.predicted_marginal_logits) + 1e-10),
                1 - self.marginal_label)
            self.marg_loss = -tf.reduce_mean(marg_pos_loss + marg_neg_loss)
            self.marg_loss *= self.marg_weight
        else:
            self.marg_loss = tf.constant(0.0)
        """model regurlization"""
        if self.regularization_method == 'universe_edge' and self.reg_weight > 0.0:
            self.regularization = self.reg_weight * tf.reduce_mean(
                tf.nn.softplus(self.universe_max - self.universe_min))
        elif self.regularization_method == 'delta' and self.reg_weight > 0.0:
            self.regularization = self.reg_weight * tf.reduce_mean(
                tf.square(tf.exp(self.delta_embed)))
        else:
            self.regularization = tf.constant(0.0)
        """model final loss"""
        self.loss = self.cond_loss + self.marg_loss + self.regularization