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  template <class... Args>
18  explicit ClipOp(Args&&... args)
19  : Operator<Context>(std::forward<Args>(args)...),
20  min_(std::numeric_limits<T>::lowest()),
21  max_(std::numeric_limits<T>::max()) {
22  if (HasArgument("min")) {
23  min_ = static_cast<T>(this->template GetSingleArgument<float>("min", 0));
24  }
25  if (HasArgument("max")) {
26  max_ = static_cast<T>(this->template GetSingleArgument<float>("max", 0));
27  }
28  }
29 
30  bool RunOnDevice() override;
31 
32  protected:
33  T min_;
34  T max_;
35 };
36 
37 template <typename T, class Context>
38 class ClipGradientOp final : public Operator<Context> {
39  public:
40  USE_OPERATOR_CONTEXT_FUNCTIONS;
41  template <class... Args>
42  explicit ClipGradientOp(Args&&... args)
43  : Operator<Context>(std::forward<Args>(args)...),
44  min_(std::numeric_limits<T>::lowest()),
45  max_(std::numeric_limits<T>::max()) {
46  if (HasArgument("min")) {
47  min_ = static_cast<T>(this->template GetSingleArgument<float>("min", 0));
48  }
49  if (HasArgument("max")) {
50  max_ = static_cast<T>(this->template GetSingleArgument<float>("max", 0));
51  }
52  }
53 
54  bool RunOnDevice() override;
55 
56  protected:
57  T min_;
58  T max_;
59  // Input: Y, dY; Output: dX
60 };
61 
62 } // namespace caffe2
63 
64 #endif // CAFFE2_OPERATORS_CLIP_OP_H_
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13
bool HasArgument(const string &name) const
Checks if the operator has an argument of the given name.
Definition: operator.h:70