Skip to content

Commit

Permalink
Added initializers (#15)
Browse files Browse the repository at this point in the history
* Added initializers
  • Loading branch information
l-bat authored Mar 15, 2021
1 parent 9f0c38b commit 4bc988b
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 6 deletions.
2 changes: 2 additions & 0 deletions include/deepworks/deepworks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
#include <deepworks/layer.hpp>
#include <deepworks/layer_info.hpp>
#include <deepworks/tensor.hpp>
#include <deepworks/metrics.hpp>
#include <deepworks/initializers.hpp>
17 changes: 17 additions & 0 deletions include/deepworks/initializers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <deepworks/tensor.hpp>

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
2 changes: 1 addition & 1 deletion include/deepworks/metrics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
Expand Down
35 changes: 35 additions & 0 deletions src/initializers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <random>
#include <algorithm>

#include <deepworks/initializers.hpp>
#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<float> 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<float> dist(lower, upper);
std::generate_n(tensor.data(), tensor.total(), [&dist, &gen]() { return dist(gen); });
}
4 changes: 2 additions & 2 deletions src/metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace dw = deepworks;

TEST(TestMetrics, accuracy) {
TEST(TestMetrics, Accuracy) {
std::vector<float> labels = {0, 1, 0, 0};
std::vector<float> predict = {
0.56, 0.44,
Expand All @@ -25,7 +25,7 @@ TEST(TestMetrics, accuracy) {
EXPECT_FLOAT_EQ(expected, acc);
}

TEST(TestMetrics, sparse_accuracy) {
TEST(TestMetrics, AccuracyOneHot) {
std::vector<float> labels = {
1, 0,
0, 1,
Expand All @@ -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);
}

0 comments on commit 4bc988b

Please sign in to comment.