#include <algorithm>
#include <cmath>
#include <cfloat>
+#include <functional>
#include <utility>
#include <vector>
(*top)[0]->Reshape(1, 1, 1, 1);
}
-template<typename Dtype>
-static bool int_Dtype_pair_greater(std::pair<int, Dtype> a,
- std::pair<int, Dtype> b) {
- return a.second > b.second;
-}
-
template <typename Dtype>
Dtype AccuracyLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
vector<int> max_id(top_k_+1);
for (int i = 0; i < num; ++i) {
// Top-k accuracy
- std::vector<std::pair<int, Dtype> > bottom_data_vector;
+ std::vector<std::pair<Dtype, int> > bottom_data_vector;
for (int j = 0; j < dim; ++j) {
bottom_data_vector.push_back(
- std::make_pair(j, bottom_data[i * dim + j]));
+ std::make_pair(bottom_data[i * dim + j], j));
}
std::partial_sort(
bottom_data_vector.begin(), bottom_data_vector.begin() + top_k_,
- bottom_data_vector.end(), int_Dtype_pair_greater<Dtype>);
+ bottom_data_vector.end(), std::greater<std::pair<Dtype, int> >());
// check if true label is in top k predictions
- for (int k = 0; k < top_k_; k++)
- if (bottom_data_vector[k].first == static_cast<int>(bottom_label[i])) {
+ for (int k = 0; k < top_k_; k++) {
+ if (bottom_data_vector[k].second == static_cast<int>(bottom_label[i])) {
++accuracy;
break;
}
+ }
}
// LOG(INFO) << "Accuracy: " << accuracy;
#include <algorithm>
#include <cfloat>
+#include <functional>
#include <queue>
#include <utility>
#include <vector>
}
}
-template<typename Dtype>
-static bool int_Dtype_pair_greater(std::pair<int, Dtype> a,
- std::pair<int, Dtype> b) {
- return a.second > b.second;
-}
-
template <typename Dtype>
Dtype ArgMaxLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
int num = bottom[0]->num();
int dim = bottom[0]->count() / bottom[0]->num();
for (int i = 0; i < num; ++i) {
- std::vector<std::pair<int, Dtype> > bottom_data_vector;
+ std::vector<std::pair<Dtype, int> > bottom_data_vector;
for (int j = 0; j < dim; ++j) {
bottom_data_vector.push_back(
- std::make_pair(j, bottom_data[i * dim + j]));
+ std::make_pair(bottom_data[i * dim + j], j));
}
std::partial_sort(
bottom_data_vector.begin(), bottom_data_vector.begin() + top_k_,
- bottom_data_vector.end(), int_Dtype_pair_greater<Dtype>);
+ bottom_data_vector.end(), std::greater<std::pair<Dtype, int> >());
+ for (int j = 0; j < top_k_; ++j) {
+ top_data[(*top)[0]->offset(i, 0, j)] = bottom_data_vector[j].second;
+ }
if (out_max_val_) {
for (int j = 0; j < top_k_; ++j) {
- top_data[(*top)[0]->offset(i, 0, j)] = bottom_data_vector[j].first;
- top_data[(*top)[0]->offset(i, 1, j)] = bottom_data_vector[j].second;
- }
- } else {
- for (int j = 0; j < top_k_; ++j) {
- top_data[(*top)[0]->offset(i, 0, j)] = bottom_data_vector[j].first;
+ top_data[(*top)[0]->offset(i, 1, j)] = bottom_data_vector[j].first;
}
}
}