Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kohonen distance #211

Merged
merged 29 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
38c6ad9
[kohonen_distance] if direct distance is less than distance through s…
Stepka Jun 3, 2020
1683a72
[kohonen_distance] bugfix
Stepka Jun 3, 2020
a5d228c
[kohonen_distance] added sparcification to the graph
Stepka Jun 4, 2020
f1cdc92
[kohonen_distance] Added distortion estimate for measuring of the non…
Stepka Jun 5, 2020
79bf1d4
[kohonen_distance] Updated self-documentation
Stepka Jun 7, 2020
055a76d
[Redif] Reverse diffusion class: bug fixed, inteface updated to Metri…
Stepka Jun 9, 2020
df0e760
[kohonen_distance] integrated redif to the Kohonen distance
Stepka Jun 10, 2020
3869191
[kohonen_distance] Updated self-documentation
Stepka Jun 10, 2020
a2cfe3b
[Redif] Updated self documantation
Stepka Jun 11, 2020
171d506
[kohonen_distance] bugfix
Stepka Jun 11, 2020
58f973b
[kohonen_distance] bugfix
Stepka Jun 11, 2020
e81d9af
[kohonen_distance] bugfix
Stepka Jun 11, 2020
a017ecd
[kohonen_distance] bugfix
Stepka Jun 11, 2020
f288cca
[auto_detect_metric] bugfix
Stepka Jun 11, 2020
357c04d
[kohonen_distance] bugfix
Stepka Jun 11, 2020
5f2db18
bugfix
Stepka Jun 15, 2020
f234a10
[redif] bugfix
Stepka Jun 15, 2020
fa05db0
[redif] bugfix
Stepka Jun 15, 2020
49b0fa2
[redif, python] bugfix
Stepka Jun 15, 2020
d056812
[redif, python] bugfix
Stepka Jun 15, 2020
4537b1c
[redif, python] bugfix
Stepka Jun 15, 2020
c106ca4
[kohonen_distance] updated distortion
Stepka Jun 15, 2020
3c73041
[kohonen_distance] examples updates
Stepka Jun 15, 2020
1c1726c
[redif] updated interface
Stepka Jun 18, 2020
be78b65
Merge branch 'master' into kohonen_distance
Stepka Jun 18, 2020
f9780fd
[redif] updated example
Stepka Jun 18, 2020
4f704dd
[redif] lilfix
Stepka Jun 18, 2020
6223348
[redif] python fix
Stepka Jun 18, 2020
2c83a6a
[kohonen_distance] bugfix
Stepka Jun 18, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions examples/distance_examples/kohonen_distance_example_2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ int main()

auto result = distance_1(min_point, max_point);
std::cout << "result: " << result << std::endl;
result = distance_1.distortion_estimate(filtered_data);
std::cout << "distortion estimate: " << result << std::endl;
std::cout << "" << std::endl;

//

Expand Down Expand Up @@ -228,6 +231,9 @@ int main()

result = distance_2(min_point, max_point);
std::cout << "result: " << result << std::endl;
result = distance_2.distortion_estimate(filtered_data);
std::cout << "distortion estimate: " << result << std::endl;
std::cout << "" << std::endl;

//

Expand Down Expand Up @@ -270,6 +276,9 @@ int main()

result = distance_3(min_point, max_point);
std::cout << "result: " << result << std::endl;
result = distance_3.distortion_estimate(filtered_data);
std::cout << "distortion estimate: " << result << std::endl;
std::cout << "" << std::endl;

//

Expand Down Expand Up @@ -312,6 +321,9 @@ int main()

result = distance_4(min_point, max_point);
std::cout << "result: " << result << std::endl;
result = distance_4.distortion_estimate(filtered_data);
std::cout << "distortion estimate: " << result << std::endl;
std::cout << "" << std::endl;

//

Expand Down
23 changes: 7 additions & 16 deletions examples/mapping_examples/reverse_diffusion_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,20 @@ int main(int argc, char *argv[])
std::cout << std::endl;

using Record = std::vector<double>;
using Metric = metric::Euclidean<double>;
using Metric = metric::Euclidean<double>;

std::vector<Record> dataset = readCsvData("./assets/testdataset/compound.csv", ',');
std::vector<Record> test_dataset;
std::vector<Record> test_dataset = dataset;

for (int i = 0; i < 4; ++i)
{
test_dataset.push_back(dataset[i]);
}

metric::Redif redif(dataset, 7, 15, Metric());
metric::Redif redif(dataset, 4, 10, Metric());

auto [encoded_data, indicies] = redif.encode(test_dataset);
auto decoded_data = redif.decode(test_dataset, indicies);
auto encoded_data = redif.encode(test_dataset);
auto decoded_data = redif.decode(encoded_data);

auto is_equal = std::equal(
test_dataset.begin(),
Expand All @@ -134,16 +134,7 @@ int main(int argc, char *argv[])
);
}
);


std::cout << "original dataset: " << std::endl;
matrix_print(test_dataset);
std::cout << std::endl;
std::cout << "encoded and decoded back dataset: " << std::endl;
matrix_print(decoded_data);
std::cout << std::endl;
std::cout << std::endl;


std::cout << "is encoded and decoded back dataset is equal with original: " << (is_equal ? "true" : "false") << std::endl;

return 0;
Expand Down
16 changes: 7 additions & 9 deletions modules/distance/k-structured/Kohonen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ template <typename D, typename Sample, typename Graph, typename Metric, typename
double Kohonen<D, Sample, Graph, Metric, Distribution>::distortion_estimate(const std::vector<Sample>& samples)
{
metric::Euclidean<D> euclidean_distance;
std::vector<D> factor_histogram;
double sum = 0;
int count = 0;
for (size_t i = 0; i < samples.size(); ++i)
{
for (size_t j = 0; j < samples.size(); ++j)
Expand All @@ -219,21 +220,18 @@ double Kohonen<D, Sample, Graph, Metric, Distribution>::distortion_estimate(cons
{
auto euclidean = euclidean_distance(samples[i], samples[j]);
auto kohonen = operator()(samples[i], samples[j]);
if (euclidean != 0 && kohonen != 0)
if (euclidean != 0 && !std::isinf(kohonen))
{
factor_histogram.push_back(kohonen / euclidean);
}
else
{
factor_histogram.push_back(0);
sum += kohonen / euclidean;
count++;
}
}
}
}

metric::PMQ<Discrete<D>, double> pmq(factor_histogram);
double mean = sum / count;

return pmq.variance();
return mean - 1;
}


Expand Down
61 changes: 43 additions & 18 deletions modules/mapping/Redif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ blaze::DynamicMatrix<Tv> Redif<Tv, Metric>::calcWeightedGraphLaplacian(const bla
}

template <typename Tv, class Metric>
std::tuple<std::vector<std::vector<Tv>>, std::vector<size_t>> Redif<Tv, Metric>::encode(
std::vector<std::vector<Tv>> Redif<Tv, Metric>::encode(
const std::vector<std::vector<Tv>>& x
)
{
Expand All @@ -165,22 +165,18 @@ std::tuple<std::vector<std::vector<Tv>>, std::vector<size_t>> Redif<Tv, Metric>:
for (size_t j = 0; j < x_as_matrix.columns(); j++)
x_as_matrix(i, j) = x[i][j];

auto [encoded_data, l_idx] = encode(x_as_matrix);
auto encoded_data = encode(x_as_matrix);

std::vector<std::vector<Tv>> encoded_data_as_vectors(encoded_data.rows(), std::vector<Tv>(encoded_data.columns()));
for (size_t i = 0; i < encoded_data.rows(); i++)
for (size_t j = 0; j < encoded_data.columns(); j++)
encoded_data_as_vectors[i][j] = encoded_data(i, j);

std::vector<size_t> l_idx_as_vector(l_idx.size(), 0);
for (size_t i = 0; i < l_idx.size(); i++)
l_idx_as_vector[i] = l_idx[i];

return { encoded_data_as_vectors, l_idx_as_vector };
return encoded_data_as_vectors;
}

template <typename Tv, class Metric>
std::tuple<blaze::DynamicMatrix<Tv>, blaze::DynamicVector<size_t>> Redif<Tv, Metric>::encode(
blaze::DynamicMatrix<Tv> Redif<Tv, Metric>::encode(
const blaze::DynamicMatrix<Tv>& x
){
size_t nTrain = xTrain.rows();
Expand Down Expand Up @@ -278,25 +274,20 @@ std::tuple<blaze::DynamicMatrix<Tv>, blaze::DynamicVector<size_t>> Redif<Tv, Met
}
}

return { xEncodedRes, l_idx };
return xEncodedRes;
}


template <typename Tv, class Metric>
std::vector<std::vector<Tv>> Redif<Tv, Metric>::decode(
const std::vector<std::vector<Tv>>& xEncoded,
const std::vector<size_t>& l_idx)
const std::vector<std::vector<Tv>>& xEncoded)
{
blaze::DynamicMatrix<Tv> x_as_matrix(xEncoded.size(), xEncoded[0].size(), 0);
for (size_t i = 0; i < x_as_matrix.rows(); i++)
for (size_t j = 0; j < x_as_matrix.columns(); j++)
x_as_matrix(i, j) = xEncoded[i][j];

blaze::DynamicVector<Tv> l_idx_as_vector(l_idx.size(), 0);
for (size_t i = 0; i < l_idx.size(); i++)
l_idx_as_vector[i] = l_idx[i];

auto decoded_data = decode(x_as_matrix, l_idx_as_vector);
auto decoded_data = decode(x_as_matrix);

std::vector<std::vector<Tv>> decoded_data_as_vectors(decoded_data.rows(), std::vector<Tv>(decoded_data.columns()));
for (size_t i = 0; i < decoded_data.rows(); i++)
Expand All @@ -309,10 +300,44 @@ std::vector<std::vector<Tv>> Redif<Tv, Metric>::decode(

template <typename Tv, class Metric>
blaze::DynamicMatrix<Tv> Redif<Tv, Metric>::decode(
const blaze::DynamicMatrix<Tv>& xEncoded,
const blaze::DynamicVector<size_t>& l_idx)
const blaze::DynamicMatrix<Tv>& xEncoded)

{
size_t nX = xEncoded.rows();
size_t nTrain = xTrain.rows();
blaze::DynamicVector<size_t> l_idx = blaze::DynamicVector<size_t>(nX);

/// Compute distances
for (size_t i = 0; i < nX; i++)
{
blaze::DynamicVector<Tv, blaze::rowVector> rowi;
rowi = row(xEncoded, i);
std::vector<Tv> veci;
for (size_t s = 0; s < rowi.size(); ++s)
{
veci.push_back(rowi[s]);
}

Tv closest_dist = -1;
for (size_t k = 0; k < nTrain; k++)
{
blaze::DynamicVector<Tv, blaze::rowVector> rowi, rowk;
rowk = row(xTrainEncoded, k);
std::vector<Tv> veck;
for (size_t s = 0; s < rowk.size(); ++s)
{
veck.push_back(rowk[s]);
}

Tv dist = metric(veci, veck);
if (closest_dist < 0 || closest_dist > dist)
{
l_idx[i] = k;
closest_dist = dist;
}
}
}

size_t n = xTrainEncoded.rows();
size_t nIter = LArray.size();
for (size_t i = 0; i < l_idx.size(); i++)
Expand Down
14 changes: 6 additions & 8 deletions modules/mapping/Redif.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ class Redif
*
* @param x - 'Noisy' Records.
*
* @return The tuple of denoised (encoded) records and indices of nearest samples in training set to records.
* @return denoised (encoded) records.
*/
std::tuple<std::vector<std::vector<Tv>>, std::vector<size_t>> encode(const std::vector<std::vector<Tv>>& x);
std::vector<std::vector<Tv>> encode(const std::vector<std::vector<Tv>>& x);

/**
* @brief
Expand All @@ -64,11 +64,10 @@ class Redif
* iterations of the reverse diffusion process, the process is reveresed back.
*
* @param xEncoded - Denoised (encoded) records.
* @param l_idx - Indices of nearest Sample in training set to records.
*
* @return Decoded records.
*/
std::vector<std::vector<Tv>> decode(const std::vector<std::vector<Tv>>& xEncoded, const std::vector<size_t>& l_idx);
std::vector<std::vector<Tv>> decode(const std::vector<std::vector<Tv>>& xEncoded);

/**
* @brief
Expand Down Expand Up @@ -112,9 +111,9 @@ class Redif
*
* @param x - 'Noisy' Records
*
* @return The tuple of denoised (encoded) records and indices of nearest samples in training set to records.
* @return denoised (encoded) records.
*/
std::tuple<blaze::DynamicMatrix<Tv>, blaze::DynamicVector<size_t>> encode(const blaze::DynamicMatrix<Tv>& x);
blaze::DynamicMatrix<Tv> encode(const blaze::DynamicMatrix<Tv>& x);

/**
* @brief
Expand All @@ -123,11 +122,10 @@ class Redif
* iterations of the reverse diffusion process, the process is reveresed back.
*
* @param xEncoded - Denoised (encoded) records.
* @param l_idx - Indices of nearest Sample in training set to records.
*
* @return Decoded records.
*/
blaze::DynamicMatrix<Tv> decode(const blaze::DynamicMatrix<Tv>& xEncoded, const blaze::DynamicVector<size_t>& l_idx);
blaze::DynamicMatrix<Tv> decode(const blaze::DynamicMatrix<Tv>& xEncoded);

/**
* @brief
Expand Down
13 changes: 4 additions & 9 deletions python/src/mapping/subs/redif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,10 @@ void register_wrapper_Redif(py::module& m) {
py::arg("n_iter") = 15,
py::arg("metric") = Metric()
);
std::tuple<Container, std::vector<size_t>> (Class::*encode_vector)(const Container&) = &Class::encode;
cls.def("encode", encode_vector,
py::arg("x")
);
Container (Class::*decode_vector)(const Container&, const std::vector<size_t>&) = &Class::decode;
cls.def("decode", decode_vector,
py::arg("xEncoded"),
py::arg("l_idx")
);
Container (Class::*encode_vector)(const Container&) = &Class::encode;
cls.def("encode", encode_vector, py::arg("x"));
Container (Class::*decode_vector)(const Container&) = &Class::decode;
cls.def("decode", decode_vector, py::arg("xEncoded"));
}

void export_metric_Redif(py::module& m) {
Expand Down