text_model/example/simple_text_clf.py
def _softmax_log_loss(self, clf_out, label):
        """
        Softmax log-likelihood loss.

        Args:
            clf_out: output of the classifier.
            label: one-hot label of sentences, tensor => [batch_size, class_num].

        Returns:
            A dict of all outputs.
        """
        label = tf.cast(label, dtype=TF_DTYPE)
        max_x = tf.reduce_max(clf_out["fc_out"], axis=1, keep_dims=True)
        log_likelihood = tf.reduce_sum(label * (clf_out["fc_out"] - max_x), axis=1) - \
                         tf.log(tf.reduce_sum(tf.exp(clf_out["fc_out"] - max_x), axis=1) + 1e-9)
        ## get regular term
        reg_loss = tf.add_n(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES))
        loss = -1 * tf.reduce_mean(log_likelihood) + reg_loss

        acc, acc_op = tf.metrics.accuracy(labels=tf.argmax(label, axis=1),
                                          predictions=tf.argmax(clf_out["pred"], axis=1))
        prec, prec_op = tf.metrics.precision(labels=tf.argmax(label, axis=1),
                                             predictions=tf.argmax(clf_out["pred"], axis=1))
        return { "loss" : loss, "acc" : acc_op, "prec" : prec_op }

        Softmax log-likelihood loss.

        Args:
            clf_out: output of the classifier.
            label: one-hot label of sentences, tensor => [batch_size, class_num].

        Returns:
            A dict of all outputs.
        
Softmax log-likelihood loss.
Softmax log - likelihood loss .
def _softmax_log_loss(self, clf_out, label):
        label = tf.cast(label, dtype=TF_DTYPE)
        max_x = tf.reduce_max(clf_out["fc_out"], axis=1, keep_dims=True)
        log_likelihood = tf.reduce_sum(label * (clf_out["fc_out"] - max_x), axis=1) - \
                         tf.log(tf.reduce_sum(tf.exp(clf_out["fc_out"] - max_x), axis=1) + 1e-9)
        reg_loss = tf.add_n(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES))
        loss = -1 * tf.reduce_mean(log_likelihood) + reg_loss
        acc, acc_op = tf.metrics.accuracy(labels=tf.argmax(label, axis=1),
                                          predictions=tf.argmax(clf_out["pred"], axis=1))
        prec, prec_op = tf.metrics.precision(labels=tf.argmax(label, axis=1),
                                             predictions=tf.argmax(clf_out["pred"], axis=1))
        return { "loss" : loss, "acc" : acc_op, "prec" : prec_op }
def _softmax_log_loss ( self , clf_out , label ) : label = tf . cast ( label , dtype = TF_DTYPE ) max_x = tf . reduce_max ( clf_out [ "fc_out" ] , axis = 1 , keep_dims = True ) log_likelihood = tf . reduce_sum ( label * ( clf_out [ "fc_out" ] - max_x ) , axis = 1 ) - tf . log ( tf . reduce_sum ( tf . exp ( clf_out [ "fc_out" ] - max_x ) , axis = 1 ) + 1e-9 ) reg_loss = tf . add_n ( tf . get_collection ( tf . GraphKeys . REGULARIZATION_LOSSES ) ) loss = - 1 * tf . reduce_mean ( log_likelihood ) + reg_loss acc , acc_op = tf . metrics . accuracy ( labels = tf . argmax ( label , axis = 1 ) , predictions = tf . argmax ( clf_out [ "pred" ] , axis = 1 ) ) prec , prec_op = tf . metrics . precision ( labels = tf . argmax ( label , axis = 1 ) , predictions = tf . argmax ( clf_out [ "pred" ] , axis = 1 ) ) return { "loss" : loss , "acc" : acc_op , "prec" : prec_op }
Softmax log-likelihood loss.
Softmax log - likelihood loss .
"""
        Softmax log-likelihood loss.

        Args:
            clf_out: output of the classifier.
            label: one-hot label of sentences, tensor => [batch_size, class_num].

        Returns:
            A dict of all outputs.
        """ ## get regular term
A dict of all outputs.
A dict of all outputs .
self
clf_out
output of the classifier.
output of the classifier .
label
one-hot label of sentences, tensor => [batch_size, class_num].
one - hot label of sentences tensor = > [ batch_size class_num ] .