Caffe2 - C++ API
A deep learning, cross platform ML framework
normalize_l1_op.cc
1 
17 #include "caffe2/operators/normalize_l1_op.h"
18 
19 #include "caffe2/core/tensor.h"
20 
21 namespace caffe2 {
22 
23 template <typename T, class Context>
24 void NormalizeL1Op<T, Context>::DoNormalize(
25  const T* xData,
26  T* yData,
27  const int m,
28  const int n,
29  const int sf) {
30  using InnerStride = Eigen::InnerStride<Eigen::Dynamic>;
31  using StridedVec =
32  Eigen::Map<Eigen::Matrix<T, 1, Eigen::Dynamic>, 0, InnerStride>;
33  using ConstStridedVec =
34  Eigen::Map<const Eigen::Matrix<T, 1, Eigen::Dynamic>, 0, InnerStride>;
35 
36  for (int i = 0; i < n; ++i) {
37  auto base = (i / sf) * sf * m + (i % sf);
38  ConstStridedVec xVec(xData + base, 1, m, InnerStride(sf));
39  auto norm = xVec.template lpNorm<1>();
40  if (norm != 0) {
41  StridedVec yVec(yData + base, 1, m, InnerStride(sf));
42  yVec = xVec / norm;
43  }
44  }
45 };
46 
47 REGISTER_CPU_OPERATOR(NormalizeL1, NormalizeL1Op<float, CPUContext>);
48 OPERATOR_SCHEMA(NormalizeL1)
49  .NumInputs(1)
50  .NumOutputs(1)
51  .Arg("axis", "axis to normalize")
52  .SetDoc(R"DOC(
53 Given a matrix, apply L1-normalization along the specified axis.
54 )DOC");
55 
56 } // namespace caffe2
Copyright (c) 2016-present, Facebook, Inc.