1 #include "caffe2/operators/normalize_op.h" 3 #include "caffe2/core/tensor.h" 7 template <
typename T,
class Context>
8 void NormalizeGradientOp<T, Context>::DoNormalize(
15 using InnerStride = Eigen::InnerStride<Eigen::Dynamic>;
17 Eigen::Map<Eigen::Matrix<T, 1, Eigen::Dynamic>, 0, InnerStride>;
18 using ConstStridedVec =
19 Eigen::Map<const Eigen::Matrix<T, 1, Eigen::Dynamic>, 0, InnerStride>;
21 for (
int i = 0; i < n; ++i) {
22 auto base = (i / sf) * sf * m + (i % sf);
23 ConstStridedVec xVec(xData + base, 1, m, InnerStride(sf));
24 ConstStridedVec gOutVec(gOutData + base, 1, m, InnerStride(sf));
26 auto row_sum = xVec.dot(gOutVec);
27 auto row_norm = xVec.template lpNorm<2>();
28 row_norm = std::max(row_norm, kEps_);
29 auto row_norm_3 = pow(row_norm, 3);
30 StridedVec gInVec(gInData + base, 1, m, InnerStride(sf));
31 gInVec = (gOutVec / row_norm) - ((xVec / row_norm_3) * row_sum);
35 REGISTER_CPU_OPERATOR(Normalize, NormalizeOp<float, CPUContext>);
36 OPERATOR_SCHEMA(Normalize)
39 .Arg(
"axis",
"axis to normalize")
41 Given a matrix, apply L2-normalization along the specified dimension. 43 .IdenticalTypeAndShape(); 45 REGISTER_CPU_GRADIENT_OPERATOR( 47 NormalizeGradientOp<float, CPUContext>); 48 GRADIENT_OPERATOR_SCHEMA(NormalizeGradient) 51 .Arg("axis",
"axis to normalize");
54 using GradientMakerBase::GradientMakerBase;
55 vector<OperatorDef> GetGradientDefs()
override {
56 CAFFE_ENFORCE_EQ(def_.input_size(), 1);
60 vector<string>{I(0), GO(0)},
61 vector<string>{GI(0)});
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
static vector< OperatorDef > SingleGradientDef(const Args &...args)
a helper function to allow one to create one single operator def, which is usually the case for many ...