Caffe2 - C++ API
A deep learning, cross platform ML framework
sigmoid_cross_entropy_loss_op.h
1 
17 #ifndef SIGMOID_CROSS_ENTROPY_LOSS_OP_H_
18 #define SIGMOID_CROSS_ENTROPY_LOSS_OP_H_
19 
20 #include "caffe2/core/context.h"
21 #include "caffe2/core/logging.h"
22 #include "caffe2/core/operator.h"
23 #include "caffe2/utils/math.h"
24 
25 namespace caffe2 {
26 
27 template <typename T, class Context>
28 class SigmoidCrossEntropyLossOp final : public Operator<Context> {
29  public:
30  SigmoidCrossEntropyLossOp(const OperatorDef& operator_def, Workspace* ws)
31  : Operator<Context>(operator_def, ws),
32  scale_(this->template GetSingleArgument<float>("scale", 1.)),
33  normalize_(this->template GetSingleArgument<int>("normalize", 1)) {
34  CAFFE_ENFORCE(scale_ >= 0);
35  CAFFE_ENFORCE(normalize_ == 0 || normalize_ == 1);
36  }
37  USE_OPERATOR_CONTEXT_FUNCTIONS;
38 
39  bool RunOnDevice() override {
40  // No CPU implementation for now
41  CAFFE_NOT_IMPLEMENTED;
42  }
43 
44  protected:
45  float scale_;
46  int normalize_;
47  Tensor losses_{Context::GetDeviceType()};
48  Tensor counts_{Context::GetDeviceType()};
49  Tensor normalizer_;
50 };
51 
52 template <typename T, class Context>
53 class SigmoidCrossEntropyLossGradientOp final : public Operator<Context> {
54  public:
55  SigmoidCrossEntropyLossGradientOp(const OperatorDef& def, Workspace* ws)
56  : Operator<Context>(def, ws),
57  scale_(this->template GetSingleArgument<float>("scale", 1.)),
58  normalize_(this->template GetSingleArgument<int>("normalize", 1)) {
59  CAFFE_ENFORCE(scale_ >= 0);
60  CAFFE_ENFORCE(normalize_ == 0 || normalize_ == 1);
61  }
62  USE_OPERATOR_CONTEXT_FUNCTIONS;
63 
64  bool RunOnDevice() override {
65  // No CPU implementation for now
66  CAFFE_NOT_IMPLEMENTED;
67  }
68 
69  protected:
70  float scale_;
71  int normalize_;
72  Tensor counts_{Context::GetDeviceType()};
73  Tensor normalizer_;
74 };
75 
76 } // namespace caffe2
77 
78 #endif // SIGMOID_CROSS_ENTROPY_LOSS_OP_H_
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
Definition: workspace.h:47
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13