Caffe2 - C++ API
A deep learning, cross platform ML framework
clip_op.h
1 #ifndef CAFFE2_OPERATORS_CLIP_OP_H_
2 #define CAFFE2_OPERATORS_CLIP_OP_H_
3 
4 #include <limits>
5 
6 #include "caffe2/core/context.h"
7 #include "caffe2/core/logging.h"
8 #include "caffe2/core/operator.h"
9 #include "caffe2/utils/math.h"
10 
11 namespace caffe2 {
12 
13 template <typename T, class Context>
14 class ClipOp final : public Operator<Context> {
15  public:
16  USE_OPERATOR_CONTEXT_FUNCTIONS;
17  ClipOp(const OperatorDef& operator_def, Workspace* ws)
18  : Operator<Context>(operator_def, ws),
19  min_(std::numeric_limits<T>::lowest()),
20  max_(std::numeric_limits<T>::max()) {
21  if (HasArgument("min")) {
22  min_ = static_cast<T>(OperatorBase::GetSingleArgument<float>("min", 0));
23  }
24  if (HasArgument("max")) {
25  max_ = static_cast<T>(OperatorBase::GetSingleArgument<float>("max", 0));
26  }
27  }
28 
29  bool RunOnDevice() override;
30 
31  protected:
32  T min_;
33  T max_;
34 };
35 
36 template <typename T, class Context>
37 class ClipGradientOp final : public Operator<Context> {
38  public:
39  USE_OPERATOR_CONTEXT_FUNCTIONS;
40  ClipGradientOp(const OperatorDef& operator_def, Workspace* ws)
41  : Operator<Context>(operator_def, ws),
42  min_(std::numeric_limits<T>::lowest()),
43  max_(std::numeric_limits<T>::max()) {
44  if (HasArgument("min")) {
45  min_ = static_cast<T>(OperatorBase::GetSingleArgument<float>("min", 0));
46  }
47  if (HasArgument("max")) {
48  max_ = static_cast<T>(OperatorBase::GetSingleArgument<float>("max", 0));
49  }
50  }
51 
52  bool RunOnDevice() override;
53 
54  protected:
55  T min_;
56  T max_;
57  // Input: Y, dY; Output: dX
58 };
59 
60 } // namespace caffe2
61 
62 #endif // CAFFE2_OPERATORS_CLIP_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 ...
bool HasArgument(const string &name) const
Checks if the operator has an argument of the given name.
Definition: operator.h:37