def cross_entropy_loss(logits, one_hot_labels, label_smoothing=0, weight=1.0, scope=None):
 logits.get_shape().assert_is_compatible_with(one_hot_labels.get_shape())
 with tf.name_scope(scope, 'CrossEntropyLoss', [logits, one_hot_labels]):
  num_classes = one_hot_labels.get_shape()[(-1)].value
  one_hot_labels = tf.cast(one_hot_labels, logits.dtype)
  if (label_smoothing > 0):
   smooth_positives = (1.0 - label_smoothing)
   smooth_negatives = (label_smoothing / num_classes)
   one_hot_labels = ((one_hot_labels * smooth_positives) + smooth_negatives)
  cross_entropy = tf.contrib.nn.deprecated_flipped_softmax_cross_entropy_with_logits(logits, one_hot_labels, name='xentropy')
  weight = tf.convert_to_tensor(weight, dtype=logits.dtype.base_dtype, name='loss_weight')
  loss = tf.multiply(weight, tf.reduce_mean(cross_entropy), name='value')
  tf.add_to_collection(LOSSES_COLLECTION, loss)
  return loss

def cross_entropy_loss ( logits , one_hot_labels , label_smoothing = 0 , weight = 1.0 , scope = None ) : logits . get_shape ( ) . assert_is_compatible_with ( one_hot_labels . get_shape ( ) ) with tf . name_scope ( scope , 'CrossEntropyLoss' , [ logits , one_hot_labels ] ) : num_classes = one_hot_labels . get_shape ( ) [ ( - 1 ) ] . value one_hot_labels = tf . cast ( one_hot_labels , logits . dtype ) if ( label_smoothing > 0 ) : smooth_positives = ( 1.0 - label_smoothing ) smooth_negatives = ( label_smoothing / num_classes ) one_hot_labels = ( ( one_hot_labels * smooth_positives ) + smooth_negatives ) cross_entropy = tf . contrib . nn . deprecated_flipped_softmax_cross_entropy_with_logits ( logits , one_hot_labels , name = 'xentropy' ) weight = tf . convert_to_tensor ( weight , dtype = logits . dtype . base_dtype , name = 'loss_weight' ) loss = tf . multiply ( weight , tf . reduce_mean ( cross_entropy ) , name = 'value' ) tf . add_to_collection ( LOSSES_COLLECTION , loss ) return loss
define a cross entropy loss using softmax_cross_entropy_with_logits .