forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added initializers
- Loading branch information
Showing
7 changed files
with
61 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters