Added google benchmark support + small test
This commit is contained in:
@@ -24,6 +24,7 @@ pkg_check_modules(LIBAV IMPORTED_TARGET libavcodec libavutil libavformat)
|
|||||||
pkg_check_modules(Archive REQUIRED IMPORTED_TARGET libarchive)
|
pkg_check_modules(Archive REQUIRED IMPORTED_TARGET libarchive)
|
||||||
find_package(PAM)
|
find_package(PAM)
|
||||||
find_package(STB)
|
find_package(STB)
|
||||||
|
find_package(benchmark)
|
||||||
|
|
||||||
# WT
|
# WT
|
||||||
if (NOT Wt_FOUND)
|
if (NOT Wt_FOUND)
|
||||||
@@ -70,6 +71,15 @@ elseif (IMAGE_LIBRARY STREQUAL STB AND NOT STB_FOUND)
|
|||||||
endif ()
|
endif ()
|
||||||
message(STATUS "IMAGE_LIBRARY set to ${IMAGE_LIBRARY}")
|
message(STATUS "IMAGE_LIBRARY set to ${IMAGE_LIBRARY}")
|
||||||
|
|
||||||
|
# Benchmark
|
||||||
|
if (benchmark_FOUND)
|
||||||
|
set(BUILD_BENCHMARKS ON)
|
||||||
|
message(STATUS "Google Benchmark found. Building benchmarks")
|
||||||
|
else ()
|
||||||
|
set(BUILD_BENCHMARKS OFF)
|
||||||
|
message(STATUS "Google Benchmark not found. Not building benchmarks")
|
||||||
|
endif()
|
||||||
|
|
||||||
add_subdirectory(src)
|
add_subdirectory(src)
|
||||||
|
|
||||||
install(DIRECTORY approot DESTINATION share/lms)
|
install(DIRECTORY approot DESTINATION share/lms)
|
||||||
|
|||||||
@@ -22,3 +22,7 @@ install(TARGETS lmssom DESTINATION lib)
|
|||||||
if(BUILD_TESTING)
|
if(BUILD_TESTING)
|
||||||
add_subdirectory(test)
|
add_subdirectory(test)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
if (BUILD_BENCHMARKS)
|
||||||
|
add_subdirectory(bench)
|
||||||
|
endif()
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
|
||||||
|
add_executable(bench-som
|
||||||
|
SomBench.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(bench-som PRIVATE
|
||||||
|
lmssom
|
||||||
|
benchmark
|
||||||
|
)
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2024 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <random>
|
||||||
|
#include <benchmark/benchmark.h>
|
||||||
|
|
||||||
|
#include "som/Network.hpp"
|
||||||
|
|
||||||
|
using namespace SOM;
|
||||||
|
|
||||||
|
// Benchmark function
|
||||||
|
static void BM_Matrix(benchmark::State& state)
|
||||||
|
{
|
||||||
|
std::minstd_rand randomEngine{ 42 };
|
||||||
|
std::uniform_int_distribution distrib{ 0, 1000 };
|
||||||
|
|
||||||
|
Matrix<int> matrix{ static_cast<Coordinate>(state.range(0)), static_cast<Coordinate>(state.range(0)) };
|
||||||
|
|
||||||
|
for (Coordinate x {}; x < matrix.getWidth(); ++x )
|
||||||
|
{
|
||||||
|
for (Coordinate y {}; y < matrix.getHeight(); ++y )
|
||||||
|
matrix.get({ x, y }) = distrib(randomEngine);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto _ : state)
|
||||||
|
{
|
||||||
|
// Code inside this loop is measured repeatedly
|
||||||
|
const Position pos{ matrix.getPositionMinElement([](int a, int b) { return a < b; }) };
|
||||||
|
benchmark::DoNotOptimize(pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Perform cleanup here if needed
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register the benchmark with custom range
|
||||||
|
BENCHMARK(BM_Matrix)->Arg(3)->Arg(6)->Arg(12)->Arg(24);
|
||||||
|
|
||||||
|
BENCHMARK_MAIN();
|
||||||
@@ -26,108 +26,104 @@
|
|||||||
|
|
||||||
namespace SOM
|
namespace SOM
|
||||||
{
|
{
|
||||||
|
using Coordinate = unsigned;
|
||||||
|
|
||||||
using Coordinate = unsigned;
|
struct Position
|
||||||
using Norm = InputVector::value_type;
|
{
|
||||||
|
Coordinate x;
|
||||||
|
Coordinate y;
|
||||||
|
|
||||||
struct Position
|
bool operator<(const Position& other) const
|
||||||
{
|
{
|
||||||
Coordinate x;
|
if (x == other.x)
|
||||||
Coordinate y;
|
return y < other.y;
|
||||||
|
else
|
||||||
|
return x < other.x;
|
||||||
|
}
|
||||||
|
|
||||||
bool operator<(const Position& other) const
|
bool operator==(const Position& other) const
|
||||||
{
|
{
|
||||||
if (x == other.x)
|
return x == other.x && y == other.y;
|
||||||
return y < other.y;
|
}
|
||||||
else
|
};
|
||||||
return x < other.x;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator==(const Position& other) const
|
template <typename T>
|
||||||
{
|
class Matrix
|
||||||
return x == other.x && y == other.y;
|
{
|
||||||
}
|
public:
|
||||||
};
|
Matrix() = default;
|
||||||
|
|
||||||
template <typename T>
|
Matrix(Coordinate width, Coordinate height)
|
||||||
class Matrix
|
: _width{ width }
|
||||||
{
|
, _height{ height }
|
||||||
public:
|
{
|
||||||
Matrix() = default;
|
_values.resize(static_cast<std::size_t>(_width) * static_cast<std::size_t>(_height));
|
||||||
|
}
|
||||||
|
|
||||||
Matrix(Coordinate width, Coordinate height)
|
template<typename... CtrArgs>
|
||||||
: _width {width}
|
Matrix(Coordinate width, Coordinate height, CtrArgs&& ... args)
|
||||||
, _height {height}
|
: _width{ width }
|
||||||
{
|
, _height{ height }
|
||||||
_values.resize(static_cast<std::size_t>(_width) * static_cast<std::size_t>(_height));
|
{
|
||||||
}
|
_values.resize(static_cast<std::size_t>(_width) * static_cast<std::size_t>(_height), T{ std::forward<CtrArgs>(args)... });
|
||||||
|
}
|
||||||
|
|
||||||
template<typename... CtArgs>
|
void clear()
|
||||||
Matrix(Coordinate width, Coordinate height, CtArgs... args)
|
{
|
||||||
: _width {width}
|
_values.clear();
|
||||||
, _height {height}
|
}
|
||||||
{
|
|
||||||
_values.resize(static_cast<std::size_t>(_width) * static_cast<std::size_t>(_height), T{args...});
|
|
||||||
}
|
|
||||||
|
|
||||||
void clear()
|
Coordinate getHeight() const { return _height; }
|
||||||
{
|
Coordinate getWidth() const { return _width; }
|
||||||
std::vector<T> values(static_cast<std::size_t>(_width) * static_cast<std::size_t>(_height));
|
|
||||||
_values.swap(values);
|
|
||||||
}
|
|
||||||
|
|
||||||
Coordinate getHeight() const { return _height; }
|
T& get(const Position& position)
|
||||||
Coordinate getWidth() const { return _width; }
|
{
|
||||||
|
assert(position.x < _width);
|
||||||
|
assert(position.y < _height);
|
||||||
|
return _values[position.x + _width * position.y];
|
||||||
|
}
|
||||||
|
|
||||||
T& get(const Position& position)
|
const T& get(const Position& position) const
|
||||||
{
|
{
|
||||||
assert(position.x < _width);
|
assert(position.x < _width);
|
||||||
assert(position.y < _height);
|
assert(position.y < _height);
|
||||||
return _values[position.x + _width*position.y];
|
return _values[position.x + _width * position.y];
|
||||||
}
|
}
|
||||||
|
|
||||||
const T& get(const Position& position) const
|
T& operator[](const Position& position) { return get(position); }
|
||||||
{
|
const T& operator[](const Position& position) const { return get(position); }
|
||||||
assert(position.x < _width);
|
|
||||||
assert(position.y < _height);
|
|
||||||
return _values[position.x + _width*position.y];
|
|
||||||
}
|
|
||||||
|
|
||||||
T& operator[](const Position& position) { return get(position); }
|
template <typename Func>
|
||||||
const T& operator[](const Position& position) const { return get(position); }
|
Position getPositionMinElement(Func func) const
|
||||||
|
{
|
||||||
|
assert(!_values.empty());
|
||||||
|
|
||||||
template <typename Func>
|
const auto it{ std::min_element(_values.begin(), _values.end(), std::move(func)) };
|
||||||
Position getPositionMinElement(Func func) const
|
const auto index{ static_cast<Coordinate>(std::distance(_values.begin(), it)) };
|
||||||
{
|
|
||||||
assert(!_values.empty());
|
|
||||||
|
|
||||||
auto it {std::min_element(_values.begin(), _values.end(), std::move(func))};
|
return Position{ index % _height, index / _height };
|
||||||
auto index {static_cast<Coordinate>(std::distance(_values.begin(), it))};
|
}
|
||||||
|
|
||||||
return {index % _height, index / _height};
|
private:
|
||||||
}
|
Coordinate _width{};
|
||||||
|
Coordinate _height{};
|
||||||
private:
|
std::vector<T> _values;
|
||||||
Coordinate _width {};
|
};
|
||||||
Coordinate _height {};
|
|
||||||
std::vector<T> _values;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // ns SOM
|
} // ns SOM
|
||||||
|
|
||||||
namespace std {
|
namespace std
|
||||||
|
|
||||||
template<>
|
|
||||||
class hash<SOM::Position>
|
|
||||||
{
|
{
|
||||||
public:
|
template<>
|
||||||
size_t operator()(const SOM::Position& s) const
|
class hash<SOM::Position>
|
||||||
{
|
{
|
||||||
size_t h1 = std::hash<SOM::Coordinate>()(s.x);
|
public:
|
||||||
size_t h2 = std::hash<SOM::Coordinate>()(s.y);
|
size_t operator()(const SOM::Position& s) const
|
||||||
return h1 ^ (h2 << 1);
|
{
|
||||||
}
|
size_t h1 = std::hash<SOM::Coordinate>()(s.x);
|
||||||
};
|
size_t h2 = std::hash<SOM::Coordinate>()(s.y);
|
||||||
|
return h1 ^ (h2 << 1);
|
||||||
|
}
|
||||||
|
};
|
||||||
} // ns std
|
} // ns std
|
||||||
|
|
||||||
|
|||||||
@@ -30,78 +30,78 @@
|
|||||||
namespace SOM
|
namespace SOM
|
||||||
{
|
{
|
||||||
|
|
||||||
using LearningFactor = InputVector::value_type;
|
using LearningFactor = InputVector::value_type;
|
||||||
|
using Norm = InputVector::value_type;
|
||||||
|
|
||||||
void checkSameDimensions(const InputVector& a, const InputVector& b);
|
void checkSameDimensions(const InputVector& a, const InputVector& b);
|
||||||
void checkSameDimensions(const InputVector& a, std::size_t inputDimCount);
|
void checkSameDimensions(const InputVector& a, std::size_t inputDimCount);
|
||||||
std::ostream& operator<<(std::ostream& os, const InputVector& a);
|
std::ostream& operator<<(std::ostream& os, const InputVector& a);
|
||||||
|
|
||||||
|
class Network
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// Init a network with random values
|
||||||
|
Network(Coordinate width, Coordinate height, std::size_t inputDimCount);
|
||||||
|
|
||||||
class Network
|
Coordinate getWidth() const { return _refVectors.getWidth(); }
|
||||||
{
|
Coordinate getHeight() const { return _refVectors.getHeight(); }
|
||||||
public:
|
std::size_t getInputDimCount() const { return _inputDimCount; }
|
||||||
// Init a network with random values
|
const InputVector& getDataWeights() const { return _weights; }
|
||||||
Network(Coordinate width, Coordinate height, std::size_t inputDimCount);
|
|
||||||
|
|
||||||
Coordinate getWidth() const { return _refVectors.getWidth(); }
|
// Set weight for each dimension (default is 1 for each weight)
|
||||||
Coordinate getHeight() const { return _refVectors.getHeight(); }
|
void setDataWeights(const InputVector& weights);
|
||||||
std::size_t getInputDimCount() const { return _inputDimCount; }
|
|
||||||
const InputVector& getDataWeights() const { return _weights; }
|
|
||||||
|
|
||||||
// Set weight for each dimension (default is 1 for each weight)
|
// use this to manually construct a network without training
|
||||||
void setDataWeights(const InputVector& weights);
|
void setRefVector(const Position& position, const InputVector& data);
|
||||||
|
|
||||||
// use this to manually construct a network without training
|
// <!> data must be normalized
|
||||||
void setRefVector(const Position& position, const InputVector& data);
|
struct CurrentIteration
|
||||||
|
{
|
||||||
|
std::size_t idIteration;
|
||||||
|
std::size_t iterationCount;
|
||||||
|
};
|
||||||
|
using ProgressCallback = std::function<void(const CurrentIteration&)>;
|
||||||
|
using RequestStopCallback = std::function<bool()>;
|
||||||
|
void train(const std::vector<InputVector>& dataSamples, std::size_t nbIterations, ProgressCallback = ProgressCallback{}, RequestStopCallback = RequestStopCallback{});
|
||||||
|
|
||||||
// <!> data must be normalized
|
const InputVector& getRefVector(const Position& position) const;
|
||||||
struct CurrentIteration
|
Position getClosestRefVectorPosition(const InputVector& data) const;
|
||||||
{
|
std::optional<Position> getClosestRefVectorPosition(const InputVector& data, InputVector::Distance maxDistance) const;
|
||||||
std::size_t idIteration;
|
|
||||||
std::size_t iterationCount;
|
|
||||||
};
|
|
||||||
using ProgressCallback = std::function<void(const CurrentIteration&)>;
|
|
||||||
using RequestStopCallback = std::function<bool()>;
|
|
||||||
void train(const std::vector<InputVector>& dataSamples, std::size_t nbIterations, ProgressCallback = ProgressCallback{}, RequestStopCallback = RequestStopCallback{});
|
|
||||||
|
|
||||||
const InputVector& getRefVector(const Position& position) const;
|
std::optional<Position> getClosestRefVectorPosition(const std::vector<Position>& refVectorsPosition, InputVector::Distance maxDistance) const;
|
||||||
Position getClosestRefVectorPosition(const InputVector& data) const;
|
|
||||||
std::optional<Position> getClosestRefVectorPosition(const InputVector& data, InputVector::Distance maxDistance) const;
|
|
||||||
|
|
||||||
std::optional<Position> getClosestRefVectorPosition(const std::vector<Position>& refVectorsPosition, InputVector::Distance maxDistance) const;
|
InputVector::Distance getRefVectorsDistance(const Position& position1, const Position& position2) const;
|
||||||
|
|
||||||
InputVector::Distance getRefVectorsDistance(const Position& position1, const Position& position2) const;
|
InputVector::Distance computeRefVectorsDistanceMean() const;
|
||||||
|
InputVector::Distance computeRefVectorsDistanceMedian() const;
|
||||||
|
|
||||||
InputVector::Distance computeRefVectorsDistanceMean() const;
|
void dump(std::ostream& os) const;
|
||||||
InputVector::Distance computeRefVectorsDistanceMedian() const;
|
|
||||||
|
|
||||||
void dump(std::ostream& os) const;
|
// For each ref vector, update formula is:
|
||||||
|
// i is the current iteration
|
||||||
|
// refVector(i+1) = refVector(i) + LearningFactor(i) * NeighbourhoodFunc(i) * (MatchingRefVector - refVector)
|
||||||
|
|
||||||
// For each ref vector, update formula is:
|
using DistanceFunc = std::function<InputVector::Distance(const InputVector& /* a */, const InputVector& /* b */, const InputVector& /* weights */)>;
|
||||||
// i is the current iteration
|
void setDistanceFunc(DistanceFunc distanceFunc);
|
||||||
// refVector(i+1) = refVector(i) + LearningFactor(i) * NeighbourhoodFunc(i) * (MatchingRefVector - refVector)
|
DistanceFunc getDistanceFunc() { return _distanceFunc; }
|
||||||
|
|
||||||
using DistanceFunc = std::function<InputVector::Distance(const InputVector& /* a */, const InputVector& /* b */, const InputVector& /* weights */)>;
|
using LearningFactorFunc = std::function<LearningFactor(const CurrentIteration&)>;
|
||||||
void setDistanceFunc(DistanceFunc distanceFunc);
|
void setLearningFactorFunc(LearningFactorFunc learningFactorFunc);
|
||||||
DistanceFunc getDistanceFunc() { return _distanceFunc; }
|
|
||||||
|
|
||||||
using LearningFactorFunc = std::function<LearningFactor(const CurrentIteration&)>;
|
using NeighbourhoodFunc = std::function<InputVector::value_type(Norm /* norm(Position - CoordMatchingRefVector) */, const CurrentIteration&)>;
|
||||||
void setLearningFactorFunc(LearningFactorFunc learningFactorFunc);
|
void setNeighbourhoodFunc(NeighbourhoodFunc neighbourhoodFunc);
|
||||||
|
|
||||||
using NeighbourhoodFunc = std::function<InputVector::value_type(Norm /* norm(Position - CoordMatchingRefVector) */, const CurrentIteration&)>;
|
private:
|
||||||
void setNeighbourhoodFunc(NeighbourhoodFunc neighbourhoodFunc);
|
|
||||||
|
|
||||||
private:
|
void updateRefVectors(const Position& closestRefVectorPosition, const InputVector& input, LearningFactor learningFactor, const CurrentIteration& iteration);
|
||||||
|
|
||||||
void updateRefVectors(const Position& closestRefVectorPosition, const InputVector& input, LearningFactor learningFactor, const CurrentIteration& iteration);
|
std::size_t _inputDimCount{};
|
||||||
|
InputVector _weights; // weight for each dimension
|
||||||
|
Matrix<InputVector> _refVectors;
|
||||||
|
|
||||||
std::size_t _inputDimCount {};
|
DistanceFunc _distanceFunc;
|
||||||
InputVector _weights; // weight for each dimension
|
LearningFactorFunc _learningFactorFunc;
|
||||||
Matrix<InputVector> _refVectors;
|
NeighbourhoodFunc _neighbourhoodFunc;
|
||||||
|
};
|
||||||
DistanceFunc _distanceFunc;
|
|
||||||
LearningFactorFunc _learningFactorFunc;
|
|
||||||
NeighbourhoodFunc _neighbourhoodFunc;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace SOM
|
} // namespace SOM
|
||||||
|
|||||||
Reference in New Issue
Block a user