Caffe2 - C++ API
A deep learning, cross platform ML framework
loss_op.cc
1 #include "caffe2/operators/loss_op.h"
2 
3 namespace caffe2 {
4 
5 REGISTER_CPU_OPERATOR(AveragedLoss, AveragedLoss<float, CPUContext>);
6 REGISTER_CPU_OPERATOR(AveragedLossGradient,
7  AveragedLossGradient<float, CPUContext>);
8 
9 OPERATOR_SCHEMA(AveragedLoss)
10  .NumInputs(1)
11  .NumOutputs(1)
12  .ScalarType(TensorProto::FLOAT)
13  .SetDoc(R"DOC(
14 The *AveragedLoss* op takes a single 1-D input tensor *input* and returns a single output float value *output*. The output represents the average of the values in *input*. This op is commonly used for averaging losses, hence the name, however it does not exclusively operate on losses.
15 
16 Github Links:
17 
18 - https://github.com/caffe2/caffe2/blob/master/caffe2/operators/loss_op.h
19 - https://github.com/caffe2/caffe2/blob/master/caffe2/operators/loss_op.cc
20 
21 
22 <details>
23 
24 <summary> <b>Example</b> </summary>
25 
26 **Code**
27 
28 ```
29 
30 workspace.ResetWorkspace()
31 
32 op = core.CreateOperator(
33  "AveragedLoss",
34  ["input"],
35  ["output"],
36 )
37 
38 workspace.FeedBlob("input", np.array([8, 10, 12]).astype(np.float32))
39 print("input:\n", workspace.FetchBlob("input"))
40 
41 workspace.RunOperatorOnce(op)
42 print("output: \n", workspace.FetchBlob("output"))
43 
44 ```
45 
46 **Result**
47 
48 ```
49 
50 input:
51  [ 8. 10. 12.]
52 output:
53  10.0
54 
55 ```
56 
57 </details>
58 
59 
60 )DOC")
61  .Input(0, "input", "The input data as Tensor")
62  .Output(0, "output", "The output tensor of size 1 containing the averaged value.");
63 
64 OPERATOR_SCHEMA(AveragedLossGradient).NumInputs(2).NumOutputs(1);
65 
67  using GradientMakerBase::GradientMakerBase;
68  vector<OperatorDef> GetGradientDefs() override {
69  return SingleGradientDef(
70  "AveragedLossGradient", "",
71  vector<string>{I(0), GO(0)},
72  vector<string>{GI(0)});
73  }
74 };
75 REGISTER_GRADIENT(AveragedLoss, GetAveragedLossGradient);
76 
77 } // 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 ...