Skip to content

Commit

Permalink
adding minmaxScaler to normalize data vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravenwater committed Apr 30, 2023
1 parent ce455dd commit 516c474
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion benchmark/error/blas/dot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,30 @@ void SampleError(unsigned N = 10000, double mean = 0.0, double stddev = 2.0) {
DotProductError< integer<8> >(x, minx, maxx, y, miny, maxy);
}

namespace sw {
namespace universal {
// data normalization

// MinMaxScaler rescales the elements of a vector from their original
// range [min, max] to a new range [lb, ub]
template<typename Scalar>
blas::vector<Scalar> minmaxScaler(const blas::vector<Scalar>& v, Scalar lb = 0, Scalar ub = 1) {
if (lb < ub) {
std::cerr << "target range is inconsistent\n";
}
auto min = abs(v[blas::amin(v.size(), v)]);
auto max = abs(v[blas::amax(v.size(), v)]);
auto mapto = (ub - lb) / (max - min);
blas::vector<Scalar> t;
for (auto e : v) {
t.push_back( (e - min) * mapto );
}
return t;
}

}
}

/*
* When we want to take arbitrary vectors and want to faithfully calculate a
* dot product using lower precision types, we need to 'squeeze' the values
Expand Down Expand Up @@ -206,11 +230,12 @@ int main()
try {
using namespace sw::universal;

unsigned N{ 1000 };
unsigned N{ 14 };
double mean{ 0.0 }, stddev{ 1.0 };

auto dv = sw::universal::blas::gaussian_random_vector<double>(N, mean, stddev);
auto dminmax = minmax(dv);
auto dminmaxScaled = minmaxScaler(dv);

auto sv = squeeze<float>(dv);
auto sminmax = minmax(sv);
Expand All @@ -223,6 +248,7 @@ try {

if (N < 15) {
std::cout << dv << '\n';
std::cout << dminmaxScaled << '\n';
std::cout << sv << '\n';
std::cout << hv << '\n';
std::cout << qv << '\n';
Expand Down

0 comments on commit 516c474

Please sign in to comment.