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_(OperatorBase::GetSingleArgument<float>("scale", 1.)),
33  normalize_(OperatorBase::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<Context> losses_;
48  Tensor<Context> counts_;
49  Tensor<Context> 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_(OperatorBase::GetSingleArgument<float>("scale", 1.)),
58  normalize_(OperatorBase::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<Context> counts_;
73  Tensor<Context> normalizer_;
74 };
75 
76 } // namespace caffe2
77 
78 #endif // SIGMOID_CROSS_ENTROPY_LOSS_OP_H_
Tensor is the basic class in Caffe2 that stores a contiguous memory with its shape information...
Definition: tensor.h:109
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
Definition: workspace.h:63
Copyright (c) 2016-present, Facebook, Inc.