1 #include "caffe2/operators/accuracy_op.h" 6 bool AccuracyOp<float, CPUContext>::RunOnDevice() {
7 auto& X = Input(PREDICTION);
8 auto& label = Input(LABEL);
10 CAFFE_ENFORCE_EQ(X.dim(), 2);
13 CAFFE_ENFORCE_EQ(label.dim(), 1);
14 CAFFE_ENFORCE_EQ(label.dim32(0), N);
15 auto* Y = Output(0, vector<int64_t>(), at::dtype<float>());
16 const auto* Xdata = X.data<
float>();
17 const auto* labelData = label.data<
int>();
18 const int top_k = top_k_;
24 for (
int i = 0; i < N; ++i) {
25 auto label_i = labelData[i];
26 auto label_pred = Xdata[i * D + label_i];
28 for (
int j = 0; j < D; ++j) {
29 auto pred = Xdata[i * D + j];
30 if ((pred > label_pred) || (pred == label_pred && j < label_i)) {
40 CAFFE_ENFORCE_LE(correct, N);
41 *(Y->template mutable_data<float>()) =
static_cast<float>(correct) / N;
46 REGISTER_CPU_OPERATOR(Accuracy, AccuracyOp<float, CPUContext>);
48 OPERATOR_SCHEMA(Accuracy)
51 .ScalarType(TensorProto::FLOAT)
53 Accuracy takes two inputs- predictions and labels, and returns a float 54 accuracy value for the batch. Predictions are expected in the form of 2-D tensor 55 containing a batch of scores for various classes, and labels are expected in the 56 form of 1-D tensor containing true label indices of samples in the batch. If 57 the score for the label index in the predictions is the highest among all 58 classes, it is considered a correct prediction. 62 "Count as correct by comparing the true label to the top k scoring " 63 "classes (default 1: only compare to the top scoring class i.e. argmax)")
67 "2-D tensor (Tensor<float>) of size " 68 "(num_batches x num_classes) containing scores")
72 "1-D tensor (Tensor<float>) of size (num_batches) having " 73 "the indices of true labels")
77 "1-D tensor (Tensor<float>) of size 1 containing " 80 SHOULD_NOT_DO_GRADIENT(Accuracy);
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...