Caffe2 - C++ API
A deep learning, cross platform ML framework
softmax_shared.cc
1 #include "caffe2/core/context.h"
2 #include "caffe2/core/operator.h"
3 #include "caffe2/utils/math.h"
4 
5 namespace caffe2 {
6 
7 void SoftmaxCPU(
8  CPUContext& context,
9  const int N,
10  const int D,
11  const float* Xdata,
12  float* Ydata,
13  float* scale,
14  const float* sum_multiplier,
15  bool logarithmic,
16  float* rowmax) {
17  math::RowwiseMax<float, CPUContext>(N, D, Xdata, rowmax, &context);
18  // Put the intermediate result X - max(X) into Y
19  context.template CopyFromCPU<float>(N * D, Xdata, Ydata);
20  // Subtract the max (for numerical reasons)
21  math::Gemm<float, CPUContext>(
22  CblasNoTrans,
23  CblasNoTrans,
24  N,
25  D,
26  1,
27  -1,
28  rowmax,
29  sum_multiplier,
30  1,
31  Ydata,
32  &context);
33  // Exponentiation
34  math::Exp<float, CPUContext>(N * D, Ydata, Ydata, &context);
35  math::Gemv<float, CPUContext>(
36  CblasNoTrans, N, D, 1, Ydata, sum_multiplier, 0, scale, &context);
37  // Do division
38  // TODO(Yangqing): maybe implement it more beautifully?
39  if (!logarithmic) {
40  for (int i = 0; i < N; ++i) {
41  for (int j = 0; j < D; ++j) {
42  Ydata[i * D + j] /= scale[i];
43  }
44  }
45  } else {
46  for (int i = 0; i < N; ++i) {
47  for (int j = 0; j < D; ++j) {
48  Ydata[i * D + j] =
49  Xdata[i * D + j] - rowmax[i] - log(fmaxf(scale[i], 1e-20f));
50  }
51  }
52  }
53 }
54 
55 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13
Definition: static.cpp:70