Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added metrics (#8)
Browse files Browse the repository at this point in the history
* Added accuracy calculation
l-bat authored Mar 13, 2021
1 parent e3b28db commit e89fe7b
Showing 6 changed files with 124 additions and 0 deletions.
13 changes: 13 additions & 0 deletions include/deepworks/metrics.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <deepworks/tensor.hpp>

namespace deepworks {
namespace metric {

float accuracy(const Tensor& y_pred, const Tensor& y_true);

float sparse_accuracy(const Tensor& y_pred, const Tensor& y_true);

} // namespace metric
} // namespace deepworks
1 change: 1 addition & 0 deletions include/deepworks/tensor.hpp
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@ class Tensor {

void copyTo(Tensor tensor);
Type *data();
const Type *data() const;
size_t total() const;
void allocate(const Shape& shape);
bool empty() const;
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -13,6 +13,8 @@ set(SRC_FILES
# Runtime entities
${CMAKE_CURRENT_LIST_DIR}/runtime/tensor.cpp
${CMAKE_CURRENT_LIST_DIR}/runtime/cpu/kernels/kernels.cpp

${CMAKE_CURRENT_LIST_DIR}/metrics.cpp
)

set(DeepWorks_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/include/")
53 changes: 53 additions & 0 deletions src/metrics.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <numeric>
#include <algorithm>

#include <Eigen/Core>
#include <deepworks/metrics.hpp>
#include "util/assert.hpp"


using ConstMatrix = Eigen::Map<const Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>;
using ConstVector = Eigen::Map<const Eigen::Matrix<float, 1, Eigen::Dynamic, Eigen::RowMajor>>;

float deepworks::metric::accuracy(const deepworks::Tensor& y_pred,
const deepworks::Tensor& y_true) {
const auto& shape = y_pred.shape();
DeepWorks_Assert(shape.size() == 2);
int rows = shape[0];
int cols = shape[1];
DeepWorks_Assert(y_true.total() == rows);

ConstMatrix y_pred_mat(y_pred.data(), rows, cols);
ConstVector y_true_vec(y_true.data(), rows);

float acc = 0;
ConstMatrix::Index pred_col;
// FIXME: Calculate without loop
for (int i = 0; i < rows; i++) {
y_pred_mat.row(i).maxCoeff(&pred_col);
auto label = y_true_vec[i];
acc += pred_col == label;
}
return acc / rows;
}

float deepworks::metric::sparse_accuracy(const deepworks::Tensor& y_pred,
const deepworks::Tensor& y_true) {
const auto& shape = y_pred.shape();
DeepWorks_Assert(y_true.shape() == shape);
DeepWorks_Assert(shape.size() == 2);
int rows = shape[0];
int cols = shape[1];

ConstMatrix y_pred_mat(y_pred.data(), rows, cols);
ConstMatrix y_true_mat(y_true.data(), rows, cols);
float acc = 0;
ConstMatrix::Index pred_col, label_col;
// FIXME: Calculate without loop
for (int i = 0; i < rows; i++) {
y_pred_mat.row(i).maxCoeff(&pred_col);
y_true_mat.row(i).maxCoeff(&label_col);
acc += pred_col == label_col;
}
return acc / rows;
}
4 changes: 4 additions & 0 deletions src/runtime/tensor.cpp
Original file line number Diff line number Diff line change
@@ -76,6 +76,10 @@ Tensor::Type *Tensor::data() {
return m_descriptor->m_data;
}

const Tensor::Type *Tensor::data() const {
return m_descriptor->m_data;
}

size_t Tensor::total() const {
return m_descriptor->m_total;
}
51 changes: 51 additions & 0 deletions tests/unit/test_metrics.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <gtest/gtest.h>

#include <deepworks/tensor.hpp>
#include <deepworks/metrics.hpp>

namespace dw = deepworks;

TEST(TestMetrics, accuracy) {
std::vector<float> labels = {0, 1, 0, 0};
std::vector<float> predict = {
0.56, 0.44,
0.03, 0.97,
0.57, 0.43,
0.18, 0.82,
};

dw::Tensor y_pred(dw::Shape{4, 2});
dw::Tensor y_true(dw::Shape{4});

std::copy(labels.begin(), labels.end(), y_true.data());
std::copy(predict.begin(), predict.end(), y_pred.data());

float expected = 0.75;
float acc = dw::metric::accuracy(y_pred, y_true);
EXPECT_FLOAT_EQ(expected, acc);
}

TEST(TestMetrics, sparse_accuracy) {
std::vector<float> labels = {
1, 0,
0, 1,
0, 1,
1, 0,
};
std::vector<float> predict = {
0.56, 0.44,
0.03, 0.97,
0.57, 0.43,
0.18, 0.82,
};

dw::Tensor y_pred(dw::Shape{4, 2});
dw::Tensor y_true(dw::Shape{4, 2});

std::copy(labels.begin(), labels.end(), y_true.data());
std::copy(predict.begin(), predict.end(), y_pred.data());

float expected = 0.5;
float acc = dw::metric::sparse_accuracy(y_pred, y_true);
EXPECT_FLOAT_EQ(expected, acc);
}

0 comments on commit e89fe7b

Please sign in to comment.