From 4bc988b1be1baad9b3e15ac36daa91bf4a4cc7ae Mon Sep 17 00:00:00 2001 From: Liubov Batanina Date: Mon, 15 Mar 2021 19:33:16 +0300 Subject: [PATCH] Added initializers (#15) * Added initializers --- include/deepworks/deepworks.hpp | 2 ++ include/deepworks/initializers.hpp | 17 +++++++++++++++ include/deepworks/metrics.hpp | 2 +- src/CMakeLists.txt | 1 + src/initializers.cpp | 35 ++++++++++++++++++++++++++++++ src/metrics.cpp | 4 ++-- tests/unit/test_metrics.cpp | 6 ++--- 7 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 include/deepworks/initializers.hpp create mode 100644 src/initializers.cpp diff --git a/include/deepworks/deepworks.hpp b/include/deepworks/deepworks.hpp index 03d523aa6a2ede..f15c50bd004bac 100644 --- a/include/deepworks/deepworks.hpp +++ b/include/deepworks/deepworks.hpp @@ -7,3 +7,5 @@ #include #include #include +#include +#include diff --git a/include/deepworks/initializers.hpp b/include/deepworks/initializers.hpp new file mode 100644 index 00000000000000..1e56c5fbc79519 --- /dev/null +++ b/include/deepworks/initializers.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include + +namespace deepworks { +namespace initializer { + +void zeros(Tensor& tensor); + +void constant(Tensor& tensor, float value); + +void xavierUniform(Tensor& tensor); + +void uniform(Tensor& tensor, float lower = 0.f, float upper = 1.f); + +} // namespace initializer +} // namespace deepworks diff --git a/include/deepworks/metrics.hpp b/include/deepworks/metrics.hpp index 7abe50cacfbd3a..82cbd4a50cf8f0 100644 --- a/include/deepworks/metrics.hpp +++ b/include/deepworks/metrics.hpp @@ -7,7 +7,7 @@ namespace metric { float accuracy(const Tensor& y_pred, const Tensor& y_true); -float sparse_accuracy(const Tensor& y_pred, const Tensor& y_true); +float accuracyOneHot(const Tensor& y_pred, const Tensor& y_true); } // namespace metric } // namespace deepworks diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d04629cf914d19..986506968bb5c0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -15,6 +15,7 @@ set(SRC_FILES ${CMAKE_CURRENT_LIST_DIR}/runtime/cpu/kernels/kernels.cpp ${CMAKE_CURRENT_LIST_DIR}/metrics.cpp + ${CMAKE_CURRENT_LIST_DIR}/initializers.cpp ) set(DeepWorks_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/include/") diff --git a/src/initializers.cpp b/src/initializers.cpp new file mode 100644 index 00000000000000..61591d5c62400b --- /dev/null +++ b/src/initializers.cpp @@ -0,0 +1,35 @@ +#include +#include + +#include +#include "util/assert.hpp" + + +void deepworks::initializer::zeros(deepworks::Tensor& tensor) { + std::fill_n(tensor.data(), tensor.total(), 0); +} + +void deepworks::initializer::constant(deepworks::Tensor& tensor, float value) { + std::fill_n(tensor.data(), tensor.total(), value); +} + +void deepworks::initializer::xavierUniform(deepworks::Tensor& tensor) { + std::random_device rd; + // FIXME: create a generator once + std::mt19937 gen(rd()); + const auto& shape = tensor.shape(); + DeepWorks_Assert(shape.size() >= 1); + int inp_features = shape.size() == 2 ? shape[0] : tensor.total() / shape[0]; + int out_features = shape.size() == 2 ? shape[1] : shape[0]; + auto a = std::sqrt(6.0) / (inp_features + out_features); + std::uniform_real_distribution dist(-a, a); + std::generate_n(tensor.data(), tensor.total(), [&dist, &gen]() { return dist(gen); }); +} + +void deepworks::initializer::uniform(deepworks::Tensor& tensor, float lower, float upper) { + std::random_device rd; + // FIXME: create a generator once + std::mt19937 gen(rd()); + std::uniform_real_distribution dist(lower, upper); + std::generate_n(tensor.data(), tensor.total(), [&dist, &gen]() { return dist(gen); }); +} diff --git a/src/metrics.cpp b/src/metrics.cpp index a23381fee3e880..04408aba8d9b87 100644 --- a/src/metrics.cpp +++ b/src/metrics.cpp @@ -31,8 +31,8 @@ float deepworks::metric::accuracy(const deepworks::Tensor& y_pred, return acc / rows; } -float deepworks::metric::sparse_accuracy(const deepworks::Tensor& y_pred, - const deepworks::Tensor& y_true) { +float deepworks::metric::accuracyOneHot(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); diff --git a/tests/unit/test_metrics.cpp b/tests/unit/test_metrics.cpp index 9a79210fde8aa6..6030d22bf35518 100644 --- a/tests/unit/test_metrics.cpp +++ b/tests/unit/test_metrics.cpp @@ -5,7 +5,7 @@ namespace dw = deepworks; -TEST(TestMetrics, accuracy) { +TEST(TestMetrics, Accuracy) { std::vector labels = {0, 1, 0, 0}; std::vector predict = { 0.56, 0.44, @@ -25,7 +25,7 @@ TEST(TestMetrics, accuracy) { EXPECT_FLOAT_EQ(expected, acc); } -TEST(TestMetrics, sparse_accuracy) { +TEST(TestMetrics, AccuracyOneHot) { std::vector labels = { 1, 0, 0, 1, @@ -46,6 +46,6 @@ TEST(TestMetrics, sparse_accuracy) { std::copy(predict.begin(), predict.end(), y_pred.data()); float expected = 0.5; - float acc = dw::metric::sparse_accuracy(y_pred, y_true); + float acc = dw::metric::accuracyOneHot(y_pred, y_true); EXPECT_FLOAT_EQ(expected, acc); }