コード例 #1
0
        def cal_loss(pred, labels, batch_size, conf):
            if self.sim_mode == 'represent':
                pos_scores, neg_scores = batch_hard_triplet_scores(labels, pred) # pos/neg scores
                pos_scores = tf.squeeze(pos_scores, -1)
                neg_scores = tf.squeeze(neg_scores, -1)
                #for represent, 
                #     pred is a batch of tensors which size >1
                #     we can use triplet loss(hinge loss) or contrastive loss
                #if use hinge loss, we don't need labels
                #if use other loss(contrastive loss), we need define pos/neg target before
                if self.loss_type == 'hinge_loss':
                    #pairwise
                    loss = get_loss(type = self.loss_type, 
                                    pos_logits = pos_scores,
                                    neg_logits = neg_scores,
                                    is_distance = True,
                                    **conf)
                else:
                    #pointwise
                    pos_target = tf.ones(shape = [int(self.batch_size)/2], dtype = tf.float32)
                    neg_target = tf.zeros(shape = [int(self.batch_size)/2], dtype = tf.float32)

                    pos_loss = get_loss(type = self.loss_type, logits = pos_scores, labels =
                                    pos_target, **conf)
                    neg_loss = get_loss(type = self.loss_type, logits = neg_scores, labels =
                                    neg_target, **conf)
                    loss = pos_loss + neg_loss

            elif self.sim_mode == 'cross':
                #for cross:
                #   pred is a batch of tensors which size == 1
                #pdb.set_trace()
                if self.loss_type == 'hinge_loss':
                    #pairwise
                    if self.num_output == 1:
                        pred = tf.nn.sigmoid(pred)
                    elif self.num_output == 2:
                        pred = tf.nn.softmax(pred)[:,0]
                        pred = tf.expand_dims(pred,-1)
                    else:
                        raise ValueError('unsupported num_output, 1(sigmoid) or 2(softmax)?')
                    pos_scores = tf.strided_slice(pred, [0], [batch_size], [2])
                    neg_scores = tf.strided_slice(pred, [1], [batch_size], [2])
                    loss = get_loss(type = self.loss_type, 
                                    pos_logits = pos_scores,
                                    neg_logits = neg_scores,
                                    is_distance = False,
                                    **conf)
                elif self.loss_type in ['sigmoid_loss']:
                    #pointwise
                    #labels = tf.stack([labels, 1-labels], axis = -1)
                    loss = get_loss(type = self.loss_type, logits = pred, labels =
                                    labels, **conf)
                else:
                    raise ValueError('unsupported loss for cross match')
            else:
                raise ValueError('unknown sim mode, cross or represent?')
            return loss
コード例 #2
0
        def cal_loss(pred, labels, batch_size, conf):
            if self.tfrecords_mode == 'class':
                pos_scores, neg_scores = batch_hard_triplet_scores(labels, pred, is_distance = self.is_distance) # pos/neg scores
                pos_scores = tf.squeeze(pos_scores, -1)
                neg_scores = tf.squeeze(neg_scores, -1)
                #for represent, 
                #     pred is a batch of tensors which size >1
                #     we can use triplet loss(hinge loss) or contrastive loss
                #if use hinge loss, we don't need labels
                #if use other loss(contrastive loss), we need define pos/neg target before
                if self.loss_type in ['hinge_loss','improved_triplet_loss']:
                    #pairwise
                    loss = get_loss(type = self.loss_type, 
                                    pos_logits = pos_scores,
                                    neg_logits = neg_scores,
                                    **conf)
                else:
                    #pointwise
                    pos_target = tf.ones(shape = [int(self.batch_size)], dtype = tf.float32)
                    neg_target = tf.zeros(shape = [int(self.batch_size)], dtype = tf.float32)

                    pos_loss = get_loss(type = self.loss_type, logits = pos_scores, labels =
                                    pos_target, **conf)
                    neg_loss = get_loss(type = self.loss_type, logits = neg_scores, labels =
                                    neg_target, **conf)
                    loss = pos_loss + neg_loss

            elif self.tfrecords_mode in ['pair','point']:
                if self.loss_type in ['hinge_loss','improved_triplet_loss']:
                    assert self.tfrecords_mode == 'pair', "only pair mode can provide <query, pos, neg> format data"
                    #pairwise
                    if self.num_output == 1:
                        pred = tf.nn.sigmoid(pred)
                    elif self.num_output == 2:
                        pred = tf.nn.softmax(pred)[:,0]
                        pred = tf.expand_dims(pred,-1)
                    else:
                        raise ValueError('unsupported num_output, 1(sigmoid) or 2(softmax)?')
                    pos_scores = tf.strided_slice(pred, [0], [batch_size], [2])
                    neg_scores = tf.strided_slice(pred, [1], [batch_size], [2])
                    loss = get_loss(type = self.loss_type, 
                                    pos_logits = pos_scores,
                                    neg_logits = neg_scores,
                                    **conf)
                elif self.loss_type in ['sigmoid_loss']:
                    #pointwise
                    labels = tf.expand_dims(labels,axis=-1)
                    loss = get_loss(type = self.loss_type, logits = pred, labels =
                                    labels, **conf)
                else:
                    raise ValueError('unsupported loss for pair/point match')
            else:
                raise ValueError('unknown tfrecords_mode?')
            return loss
コード例 #3
0
 def cal_loss(self, pred, labels, batch_size, conf):
     loss = get_loss(type=self.loss_type,
                     logits=pred,
                     labels=labels,
                     labels_sparse=True,
                     **conf)
     return loss
コード例 #4
0
 def cal_loss(self, pred, labels, pos_target, neg_target, batch_size, conf):
     if self.loss_type == 'hinge_loss':
         if self.sub_loss_type == 'all':
             loss = batch_all_triplet_loss(labels, pred, conf['margin'])
         else:
             loss = batch_hard_triplet_loss(labels, pred, conf['margin'])
     else:
         loss = get_loss(type = self.loss_type, logits = pred, labels =
                             labels, **conf)
     return loss
コード例 #5
0
    def loss(self, out):
        with tf.name_scope("output"):
            self.scores = tf.nn.softmax(out, axis=1, name="scores")
            self.predictions = tf.argmax(self.scores, -1, output_type=tf.int32,
                                         name = 'predictions')
        with tf.name_scope("loss"):
            #self.loss = tf.reduce_mean(
            #    tf.nn.sparse_softmax_cross_entropy_with_logits(logits=out, labels=self.y))
            self.loss = get_loss(type = self.loss_type, logits = out, labels =
                                 self.y, labels_sparse = True, **self.conf)
            self.optimizer = tf.train.AdamOptimizer(self.learning_rate).minimize(self.loss, global_step=self.global_step)

        with tf.name_scope("accuracy"):
            correct_predictions = tf.equal(self.predictions, self.y)
            self.accuracy = tf.reduce_mean(tf.cast(correct_predictions, "float"), name="accuracy")