(logits, labels, alpha, gamma=2, name='focal_loss')
return loss
Focal loss for multi classification
    :param logits: A float32 tensor of shape [batch_size num_class].
    :param labels: A int32 tensor of shape [batch_size, num_class] or [batch_size].
    :param alpha: A 1D float32 tensor for focal loss alpha hyper-parameter
    :param gamma: A scalar for focal loss gamma hyper-parameter.
    Returns: A tensor of the same shape as `lables`
Focal loss for multi classification
    :param logits: A float32 tensor of shape [batch_size num_class].
    :param labels: A int32 tensor of shape [batch_size, num_class] or [batch_size].
    :param alpha: A 1D float32 tensor for focal loss alpha hyper-parameter
    :param gamma: A scalar for focal loss gamma hyper-parameter.
    Returns: A tensor of the same shape as `lables`
Focal loss for multi classification : param logits : A float32 tensor of shape [ batch_size num_class ] . : param labels : A int32 tensor of shape [ batch_size num_class ] or [ batch_size ] . : param alpha : A 1D float32 tensor for focal loss alpha hyper - parameter : param gamma : A scalar for focal loss gamma hyper - parameter . Returns : A tensor of the same shape as lables
def focal_loss(logits, labels, alpha, gamma=2, name='focal_loss'):
  """
    Focal loss for multi classification
    :param logits: A float32 tensor of shape [batch_size num_class].
    :param labels: A int32 tensor of shape [batch_size, num_class] or [batch_size].
    :param alpha: A 1D float32 tensor for focal loss alpha hyper-parameter
    :param gamma: A scalar for focal loss gamma hyper-parameter.
    Returns: A tensor of the same shape as `lables`
    """
  if len(labels.shape) == 1:
    labels = tf.one_hot(labels, logits.shape[-1])
  else:
    labels = labels
  labels = tf.to_float(labels)

  y_pred = tf.nn.softmax(logits, dim=-1)
  L = -labels * tf.log(y_pred)
  L *= alpha * ((1 - y_pred)**gamma)
  loss = tf.reduce_sum(L)

  tf.summary.scalar(name, loss)
  return loss
def focal_loss ( logits , labels , alpha , gamma = 2 , name = 'focal_loss' ) : if len ( labels . shape ) == 1 : labels = tf . one_hot ( labels , logits . shape [ - 1 ] ) else : labels = labels labels = tf . to_float ( labels ) y_pred = tf . nn . softmax ( logits , dim = - 1 ) L = - labels * tf . log ( y_pred ) L *= alpha * ( ( 1 - y_pred ) ** gamma ) loss = tf . reduce_sum ( L ) tf . summary . scalar ( name , loss ) return loss