Caffe2 - C++ API
A deep learning, cross platform ML framework
clip_op.h
1 
17 #ifndef CAFFE2_OPERATORS_CLIP_OP_H_
18 #define CAFFE2_OPERATORS_CLIP_OP_H_
19 
20 #include <limits>
21 
22 #include "caffe2/core/context.h"
23 #include "caffe2/core/logging.h"
24 #include "caffe2/core/operator.h"
25 #include "caffe2/utils/math.h"
26 
27 namespace caffe2 {
28 
29 template <typename T, class Context>
30 class ClipOp final : public Operator<Context> {
31  public:
32  USE_OPERATOR_CONTEXT_FUNCTIONS;
33  ClipOp(const OperatorDef& operator_def, Workspace* ws)
34  : Operator<Context>(operator_def, ws),
35  min_(std::numeric_limits<T>::lowest()),
36  max_(std::numeric_limits<T>::max()) {
37  if (HasArgument("min")) {
38  min_ = static_cast<T>(OperatorBase::GetSingleArgument<float>("min", 0));
39  }
40  if (HasArgument("max")) {
41  max_ = static_cast<T>(OperatorBase::GetSingleArgument<float>("max", 0));
42  }
43  }
44 
45  bool RunOnDevice() override;
46 
47  protected:
48  T min_;
49  T max_;
50 };
51 
52 template <typename T, class Context>
53 class ClipGradientOp final : public Operator<Context> {
54  public:
55  USE_OPERATOR_CONTEXT_FUNCTIONS;
56  ClipGradientOp(const OperatorDef& operator_def, Workspace* ws)
57  : Operator<Context>(operator_def, ws),
58  min_(std::numeric_limits<T>::lowest()),
59  max_(std::numeric_limits<T>::max()) {
60  if (HasArgument("min")) {
61  min_ = static_cast<T>(OperatorBase::GetSingleArgument<float>("min", 0));
62  }
63  if (HasArgument("max")) {
64  max_ = static_cast<T>(OperatorBase::GetSingleArgument<float>("max", 0));
65  }
66  }
67 
68  bool RunOnDevice() override;
69 
70  protected:
71  T min_;
72  T max_;
73  // Input: Y, dY; Output: dX
74 };
75 
76 } // namespace caffe2
77 
78 #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:63
Copyright (c) 2016-present, Facebook, Inc.
bool HasArgument(const string &name) const
Checks if the operator has an argument of the given name.
Definition: operator.h:52