1 #include "caffe2/operators/summarize_op.h" 6 bool SummarizeOp<float, CPUContext>::RunOnDevice() {
8 const auto N = X.numel();
9 CAFFE_ENFORCE_GT(N, 0);
11 const float* Xdata = X.data<
float>();
15 for (
auto i = 0; i < N; ++i) {
16 mean +=
static_cast<double>(Xdata[i]) / N;
17 max = std::max(max, Xdata[i]);
18 min = std::min(min, Xdata[i]);
22 double standard_deviation = 0;
23 for (
auto i = 0; i < N; ++i) {
24 double diff = Xdata[i] - mean;
25 standard_deviation += diff * diff;
28 standard_deviation = N == 1 ? 0 : std::sqrt(standard_deviation / (N - 1));
30 (*log_file_) << min <<
" " << max <<
" " << mean <<
" " 31 << standard_deviation << std::endl;
34 auto* Y = Output(0, {NUM_STATS}, at::dtype<float>());
35 float* Ydata = Y->template mutable_data<float>();
38 Ydata[MEAN_IDX] =
static_cast<float>(mean);
39 Ydata[STD_IDX] =
static_cast<float>(standard_deviation);
44 REGISTER_CPU_OPERATOR(Summarize, SummarizeOp<float, CPUContext>);
48 OPERATOR_SCHEMA(Summarize)
52 Summarize computes four statistics of the input tensor (Tensor)- min, 53 max, mean and standard deviation. The output will be written to a 1-D tensor of 54 size 4 if an output tensor is provided. Else, if the argument 'to_file' is 55 greater than 0, the values are written to a log file in the root folder. 59 "(int, default 0) flag to indicate if the summarized " 60 "statistics have to be written to a log file.")
61 .Input(0,
"data",
"The input data as Tensor.")
65 "1-D tensor (Tensor) of size 4 containing min, " 66 "max, mean and standard deviation");
68 SHOULD_NOT_DO_GRADIENT(Summarize);
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...