center_loss

paddle.fluid.layers.loss. center_loss ( input, label, num_classes, alpha, param_attr, update_center=True ) [source]
Api_attr

Static Graph

Center loss Cost layer

This OP accepts input (deep features,the output of the last hidden layer) and target label and return the center loss cost. The average of the distances of each sample in the mini-batch from the center of the corresponding category is calculated as the center loss.

For deep features, \(X\), and target labels, \(Y\), the equation is:

\[\begin{split}Out = \\frac{1}{2}(X - Y)^2\end{split}\]
Parameters
  • input (Variable) – a 2-D tensor with shape[N x M]. Its dtype should be float32 or float64.

  • label (Variable) – the groud truth which is a 2-D tensor with shape[N x 1],where N is the batch size. Its dtype should be int32.

  • num_classes (int) – the number of classification categories.

  • alpha (float|Variable) – learning rate of centers.

  • param_attr (ParamAttr) – Attribute initializer of centers.

  • update_center (bool) – whether to update value of center.

Returns

2-D tensor with shape [N * 1]

Return type

Variable

Examples

import paddle.fluid as fluid
import paddle
paddle.enable_static()

input = fluid.data(name='x',shape=[20,30],dtype='float32')
label = fluid.data(name='y',shape=[20,1],dtype='int64')
num_classes = 1000
alpha = 0.01
param_attr = fluid.initializer.Xavier(uniform=False)
center_loss=fluid.layers.center_loss(input=input,
       label=label,
       num_classes=1000,
       alpha=alpha,
       param_attr=fluid.initializer.Xavier(uniform=False),
       update_center=True)