File: summarize_op.cc

package info (click to toggle)
pytorch 1.7.1-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 80,340 kB
  • sloc: cpp: 670,830; python: 343,991; ansic: 67,845; asm: 5,503; sh: 2,924; java: 2,888; xml: 266; makefile: 244; ruby: 148; yacc: 144; objc: 51; lex: 44
file content (69 lines) | stat: -rw-r--r-- 2,285 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "caffe2/operators/summarize_op.h"

namespace caffe2 {

template <>
bool SummarizeOp<float, CPUContext>::RunOnDevice() {
  auto& X = Input(0);
  const auto N = X.numel();
  CAFFE_ENFORCE_GT(N, 0);

  const float* Xdata = X.data<float>();
  double mean = 0;
  float max = Xdata[0];
  float min = Xdata[0];
  for (auto i = 0; i < N; ++i) {
    mean += static_cast<double>(Xdata[i]) / N;
    max = std::max(max, Xdata[i]);
    min = std::min(min, Xdata[i]);
  }
  // We will simply do a two-pass. More efficient solutions can be written but
  // I'll keep code simple for now.
  double standard_deviation = 0;
  for (auto i = 0; i < N; ++i) {
    double diff = Xdata[i] - mean;
    standard_deviation += diff * diff;
  }
  // Unbiased or biased? Let's do unbiased now.
  standard_deviation = N == 1 ? 0 : std::sqrt(standard_deviation / (N - 1));
  if (to_file_) {
    (*log_file_) << min << " " << max << " " << mean << " "
                 << standard_deviation << std::endl;
  }
  if (OutputSize()) {
    auto* Y = Output(0, {NUM_STATS}, at::dtype<float>());
    float* Ydata = Y->template mutable_data<float>();
    Ydata[MIN_IDX] = min;
    Ydata[MAX_IDX] = max;
    Ydata[MEAN_IDX] = static_cast<float>(mean);
    Ydata[STD_IDX] = static_cast<float>(standard_deviation);
  }
  return true;
}

REGISTER_CPU_OPERATOR(Summarize, SummarizeOp<float, CPUContext>);

// Input: X; output: if set, a summarized Tensor of shape 4, with the values
// being min, max, mean and std respectively.
OPERATOR_SCHEMA(Summarize)
    .NumInputs(1)
    .NumOutputs(0, 1)
    .SetDoc(R"DOC(
Summarize computes four statistics of the input tensor (Tensor)- min,
max, mean and standard deviation. The output will be written to a 1-D tensor of
size 4 if an output tensor is provided. Else, if the argument 'to_file' is
greater than 0, the values are written to a log file in the root folder.
)DOC")
    .Arg(
        "to_file",
        "(int, default 0) flag to indicate if the summarized "
        "statistics have to be written to a log file.")
    .Input(0, "data", "The input data as Tensor.")
    .Output(
        0,
        "output",
        "1-D tensor (Tensor) of size 4 containing min, "
        "max, mean and standard deviation");

SHOULD_NOT_DO_GRADIENT(Summarize);
} // namespace caffe2