Caffe2 - C++ API
A deep learning, cross platform ML framework
softmax_with_loss_op.h
1 #ifndef SOFTMAX_WITH_LOSS_OP_H_
2 #define SOFTMAX_WITH_LOSS_OP_H_
3 
4 #include "caffe2/core/context.h"
5 #include "caffe2/core/logging.h"
6 #include "caffe2/core/operator.h"
7 #include "caffe2/utils/math.h"
8 
9 namespace caffe2 {
10 
11 template <typename T, class Context>
12 class SoftmaxWithLossOp final : public Operator<Context> {
13  public:
14  SoftmaxWithLossOp(const OperatorDef& operator_def, Workspace* ws)
15  : Operator<Context>(operator_def, ws),
16  scale_(OperatorBase::GetSingleArgument<float>("scale", 1.)),
17  label_prob_mode_(OperatorBase::GetSingleArgument<int>("label_prob", 0)),
18  order_(StringToStorageOrder(
19  OperatorBase::GetSingleArgument<string>("order", "NCHW"))),
20  axis_(OperatorBase::GetSingleArgument<int>("axis", 1)) {
21  CAFFE_ENFORCE(scale_ >= 0);
22  CAFFE_ENFORCE_EQ(
23  order_, StorageOrder::NCHW, "Only NCHW order is supported right now.");
24  }
25  USE_OPERATOR_CONTEXT_FUNCTIONS;
26 
27  bool RunOnDevice() override;
28 
29  protected:
30  float scale_;
31  int label_prob_mode_;
32  StorageOrder order_;
33  int axis_;
34 
35  Tensor<Context> losses_; // Per example loss
36  Tensor<Context> rowmax_; // per example row max
37  Tensor<Context> weights_; // unignored weights
38  Tensor<Context> sum_multiplier_; // Vector of ones for summing via dot prod
39  Tensor<Context> total_weight_ptr_;
40  Tensor<Context> scratch_;
41 };
42 
43 template <typename T, class Context>
44 class SoftmaxWithLossGradientOp final : public Operator<Context> {
45  public:
46  SoftmaxWithLossGradientOp(const OperatorDef& def, Workspace* ws)
47  : Operator<Context>(def, ws),
48  scale_(OperatorBase::GetSingleArgument<float>("scale", 1.)),
49  label_prob_mode_(OperatorBase::GetSingleArgument<int>("label_prob", 0)),
50  order_(StringToStorageOrder(
51  OperatorBase::GetSingleArgument<string>("order", "NCHW"))),
52  only_loss_(OperatorBase::GetSingleArgument<bool>("only_loss", false)),
53  axis_(OperatorBase::GetSingleArgument<int>("axis", 1)) {
54  CAFFE_ENFORCE(scale_ >= 0);
55  CAFFE_ENFORCE_EQ(
56  order_, StorageOrder::NCHW, "Only NCHW order is supported right now.");
57  }
58  USE_OPERATOR_CONTEXT_FUNCTIONS;
59 
60  bool RunOnDevice() override;
61 
62  protected:
63  float scale_;
64  int label_prob_mode_;
65  Tensor<Context> sum_multiplier_;
66  Tensor<Context> weights_; // unignored weights
67  Tensor<Context> total_weight_ptr_;
68  StorageOrder order_;
69  bool only_loss_;
70  int axis_;
71  Tensor<Context> scratch_;
72 };
73 
74 } // namespace caffe2
75 
76 #endif // SOFTMAX_WITH_LOSS_OP_H_
Tensor is the basic class in Caffe2 that stores a contiguous memory with its shape information...
Definition: tensor.h:93
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 ...