Adding a clusterer on top of the network

This commit is contained in:
emeric
2018-12-10 13:02:44 +01:00
parent 63abdc782c
commit de712936eb
2 changed files with 165 additions and 53 deletions
+149
View File
@@ -0,0 +1,149 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "SOM.hpp"
#include "DataNormalizer.hpp"
/*
* For each InputVector, associate vector<T> values
*/
template<typename T>
class Clusterer
{
public:
using SampleType = std::pair<SOM::InputVector /* key */, T /* value*/ >;
Clusterer(const std::vector<SampleType>& samples, std::size_t inputDimCount, std::size_t iterationCount);
const std::vector<T>& getClusterValues(const SOM::InputVector& data) const;
void dump(std::ostream& os) const;
private:
void train(const std::vector<std::pair<SOM::InputVector, T>>& samples, std::size_t iterationCount);
std::vector<T>& getValues(SOM::Coords coords);
const std::vector<T>& getValues(SOM::Coords coords) const;
std::size_t _width;
std::size_t _height;
std::vector<std::vector<T>> _values; // Map of T vectors
SOM::DataNormalizer _dataNormalizer;
SOM::Network _network;
};
template<typename T>
Clusterer<T>::Clusterer(const std::vector<SampleType>& samples, std::size_t inputDimCount, std::size_t iterationCount)
:
_width(3),
_height(3),
_dataNormalizer(inputDimCount),
_network(_width, _height, inputDimCount)
{
_values.resize(_width * _height);
train(samples, iterationCount);
}
template<typename T>
std::vector<T>&
Clusterer<T>::getValues(SOM::Coords coords)
{
return _values[ coords.x + coords.y*_width ];
}
template<typename T>
const std::vector<T>&
Clusterer<T>::getValues(SOM::Coords coords) const
{
return _values[ coords.x + coords.y*_width ];
}
template<typename T>
void
Clusterer<T>::train(const std::vector<std::pair<SOM::InputVector, T>>& samples, std::size_t iterationCount)
{
// Train
{
std::vector<SOM::InputVector> inputVectors;
inputVectors.reserve(samples.size());
for (const auto& sample : samples)
{
inputVectors.push_back(sample.first);
}
_dataNormalizer.computeNormalizationFactors(inputVectors);
for (auto& inputVector : inputVectors)
_dataNormalizer.normalizeData(inputVector);
_network.train(inputVectors, iterationCount);
}
// Classify data
for (const auto& sample : samples)
{
auto inputVector = sample.first;
const auto& value = sample.second;
_dataNormalizer.normalizeData(inputVector);
auto coords = _network.classify(inputVector);
auto& values = getValues(coords);
values.push_back(value);
}
}
template<typename T>
const std::vector<T>&
Clusterer<T>::getClusterValues(const SOM::InputVector& inputVector) const
{
auto inputVectorNormalized = inputVector;
_dataNormalizer.normalizeData(inputVectorNormalized);
return getValues(_network.classify(inputVectorNormalized));
}
template<typename T>
void
Clusterer<T>::dump(std::ostream& os) const
{
os << "Internal network:" << std::endl;
_network.dump(os);
os << "Values: " << std::endl;
for (std::size_t x = 0; x < _width; ++x)
{
for (std::size_t y = 0; y < _height; ++y)
{
os << "[";
for (const auto& value : getValues({x, y}))
os << value << " ";
os << "] ";
}
os << std::endl;
}
}
+16 -53
View File
@@ -6,9 +6,11 @@
#include "classifier/SOM.hpp"
#include "classifier/DataNormalizer.hpp"
#include "classifier/Clusterer.hpp"
int main(int argc, char *argv[])
{
if (argc != 2)
{
std::cerr << "Usage: <file>" << std::endl;
@@ -17,64 +19,25 @@ int main(int argc, char *argv[])
auto iterationCount = std::stoul(argv[1]);
using namespace SOM;
SOM::Network network(5, 5, 2);
network.dump(std::cout);
std::vector< std::vector<double> > inputValues =
std::vector< std::pair<std::vector<SOM::InputVector::value_type>, std::string> > inputValues =
{
{ 160, 1 },
{ 80, -1 },
{ 80, -0.75 },
{ 240, 0.5 },
{ 240, -0.5 },
{ 120, -0.5 },
{ 140, -0.5 },
{{ 160, 1 }, { "banane" }},
{{ 80, -1 }, { "poire" }},
{{ 80, -0.75 }, {"pocolat"}},
{{ 240, 0.5 }, {"abricot"}},
{{ 240, -0.5 }, {"peche"}},
{{ 120, -0.5 }, {"fraise"}},
{{ 140, -0.5 }, {"myrtille"}},
};
std::cout << "Before normalization:" << std::endl;
for (const auto& inputValue : inputValues)
{
std::cout << inputValue << std::endl;
}
std::cout << std::endl;
Clusterer<std::string> classifier(inputValues, 2, iterationCount);
std::cout << "After normalization:" << std::endl;
std::cout << "Clusterer :" << std::endl;
classifier.dump(std::cout);
SOM::DataNormalizer normalizer(2);
normalizer.computeNormalizationFactors(inputValues);
for (auto& inputValue : inputValues)
normalizer.normalizeData(inputValue);
for (const auto& inputValue : inputValues)
{
std::cout << inputValue << std::endl;
}
std::cout << std::endl;
for (const auto& inputValue : inputValues)
{
auto res = network.classify(inputValue);
std::cout << "Found at " << res.x << ", " << res.y << std::endl;
}
std::cout << "Training network for " << iterationCount << " iterations" << std::endl;
network.train(inputValues, iterationCount);
network.dump(std::cout);
std::cout << "OK" << std::endl;
for (const auto& inputValue : inputValues)
{
auto res = network.classify(inputValue);
std::cout << "Found at " << res.x << ", " << res.y << std::endl;
}
std::cout << "Classify 195, 0.35 = " << std::endl;
for (const auto& val : classifier.getClusterValues({195, 0.35}))
std::cout << val << " " << std::endl;
return EXIT_SUCCESS;
}