Caffe2 - C++ API
A deep learning, cross platform ML framework
sigmoid_cross_entropy_loss_op.cc
1 
17 #include "sigmoid_cross_entropy_loss_op.h"
18 
19 namespace caffe2 {
20 
21 REGISTER_CPU_OPERATOR(
22  SigmoidCrossEntropyLoss,
23  SigmoidCrossEntropyLossOp<float, CPUContext>);
24 REGISTER_CPU_OPERATOR(
25  SigmoidCrossEntropyLossGradient,
26  SigmoidCrossEntropyLossGradientOp<float, CPUContext>);
27 
28 OPERATOR_SCHEMA(SigmoidCrossEntropyLoss)
29  .NumInputs(2)
30  .NumOutputs(1)
31  .SetDoc(R"DOC(
32 Compute sigmoid activations followed by averaged binary cross entropy loss. The
33 target values may be in {-1, 0, 1}, where -1 indicates that the corresponding
34 sample should be ignored and {0, 1} correspond to the binary classes 0 and 1. By
35 default the loss is divided by the number of targets > -1 and then multiplied by
36 the `scale` op argument. The divisive normalization may be disable by setting
37 the op argument `normalize` to 0 (the multiplication by `scale` still takes
38 effect).
39 
40 This op fuses sigmoid and cross entropy for numerical stability in both forward
41 and gradient computation.
42 )DOC")
43  .Arg(
44  "scale",
45  "(float) default 1.0; multiply the loss by this scale factor.")
46  .Arg(
47  "normalize",
48  "(int) default 1; if true, divide the loss by the number of targets > "
49  "-1.")
50  .Input(
51  0,
52  "X",
53  "Tensor of predicted logits (shape must be at least 1D).")
54  .Input(
55  1,
56  "targets",
57  "Tensor of targets of type int and same shape as logits X.")
58  .Output(
59  0,
60  "loss",
61  "Scalar loss.");
62 
63 OPERATOR_SCHEMA(SigmoidCrossEntropyLossGradient)
64  .NumInputs(3)
65  .NumOutputs(1)
66  .Input(
67  0,
68  "X",
69  "See SigmoidCrossEntropyLoss.")
70  .Input(
71  1,
72  "targets",
73  "See SigmoidCrossEntropyLoss.")
74  .Input(
75  2,
76  "d_loss",
77  "Gradient of forward output 0 (loss).")
78  .Output(
79  0,
80  "dX",
81  "Gradient of forward input 0 (X).");
82 
84  using GradientMakerBase::GradientMakerBase;
85  vector<OperatorDef> GetGradientDefs() override {
86  return SingleGradientDef(
87  "SigmoidCrossEntropyLossGradient",
88  "",
89  vector<string>{I(0), I(1), GO(0)},
90  vector<string>{GI(0)});
91  }
92 };
93 
94 REGISTER_GRADIENT(SigmoidCrossEntropyLoss, GetSigmoidCrossEntropyLossGradient);
95 
96 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13
static vector< OperatorDef > SingleGradientDef(const Args &...args)
a helper function to allow one to create one single operator def, which is usually the case for many ...