Apply formating to all files. CLeanup
This commit is contained in:
parent
43e374f269
commit
744c01c5d0
928 changed files with 14515 additions and 21480 deletions
|
|
@ -12,12 +12,12 @@ file(GLOB HEADERS "./public/*.hpp" "./public/*/*.hpp" "./applications/*.hpp")
|
|||
add_library(${PROJECT_NAME} STATIC ${SOURCES} ${HEADERS})
|
||||
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC ./public/)
|
||||
target_link_libraries(${PROJECT_NAME} PUBLIC Math Containers)
|
||||
target_link_libraries(${PROJECT_NAME} PUBLIC Math Containers Allocators)
|
||||
|
||||
### -------------------------- Applications -------------------------- ###
|
||||
add_executable(NumberRec ./applications/NumberRecognition.cpp)
|
||||
target_link_libraries(NumberRec ${PROJECT_NAME})
|
||||
#file(COPY "applications/rsc" DESTINATION "${CMAKE_BINARY_DIR}/${PROJECT_NAME}/")
|
||||
target_link_libraries(NumberRec ${PROJECT_NAME} Connection)
|
||||
file(COPY "applications/rsc" DESTINATION "${CMAKE_BINARY_DIR}/${PROJECT_NAME}/")
|
||||
|
||||
### -------------------------- Tests -------------------------- ###
|
||||
enable_testing()
|
||||
|
|
|
|||
|
|
@ -1,3 +1,115 @@
|
|||
#include "FullyConnectedNN.hpp"
|
||||
|
||||
int main() { return 0; }
|
||||
#include "FullyConnectedNN.hpp"
|
||||
#include "LocalConnection.hpp"
|
||||
#include "NewPlacement.hpp"
|
||||
|
||||
using namespace tp;
|
||||
|
||||
struct Dataset {
|
||||
ualni length = 0;
|
||||
Pair<ualni, ualni> imageSize = { 0, 0 };
|
||||
Buffer<uint1> labels;
|
||||
Buffer<Buffer<uint1>> images;
|
||||
};
|
||||
|
||||
bool loadDataset(Dataset& out, const String& location) {
|
||||
LocalConnection dataset;
|
||||
dataset.connect(LocalConnection::Location(location), LocalConnection::Type(true));
|
||||
|
||||
if (!dataset.getConnectionStatus().isOpened()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
LocalConnection::Byte length;
|
||||
dataset.readBytes(&length, 1);
|
||||
|
||||
LocalConnection::Byte sizeX;
|
||||
dataset.readBytes(&sizeX, 1);
|
||||
|
||||
LocalConnection::Byte sizeY;
|
||||
dataset.readBytes(&sizeY, 1);
|
||||
|
||||
out.length = ((ualni) length) * 1000;
|
||||
out.imageSize = { sizeX, sizeY };
|
||||
|
||||
out.labels.reserve(out.length);
|
||||
out.images.reserve(out.length);
|
||||
|
||||
for (auto i : Range(out.length)) {
|
||||
auto& image = out.images[i];
|
||||
image.reserve(sizeX * sizeY);
|
||||
dataset.readBytes((LocalConnection::Byte*) image.getBuff(), sizeX * sizeY);
|
||||
}
|
||||
|
||||
LocalConnection::Byte label;
|
||||
dataset.readBytes((LocalConnection::Byte*) out.labels.getBuff(), out.length);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
halnf test(const Dataset& dataset, FullyConnectedNN& nn, Range<ualni> range) {
|
||||
ualni numFailed = 0;
|
||||
|
||||
for (auto i : range) {
|
||||
auto& image = dataset.images[i];
|
||||
auto label = dataset.labels[i];
|
||||
|
||||
Buffer<halnf> results;
|
||||
Buffer<halnf> input;
|
||||
|
||||
results.reserve(10);
|
||||
input.reserve(image.size());
|
||||
|
||||
for (auto pixelIdx : Range(image.size())) {
|
||||
input[pixelIdx] = (halnf) image[pixelIdx] / 255.f;
|
||||
}
|
||||
|
||||
nn.evaluate(input, results);
|
||||
|
||||
ualni resultNumber = 0;
|
||||
for (auto resIdx : Range(results.size())) {
|
||||
if (results[resIdx] > results[resultNumber]) {
|
||||
resultNumber = resIdx;
|
||||
}
|
||||
}
|
||||
|
||||
if (resultNumber != label) {
|
||||
numFailed++;
|
||||
}
|
||||
}
|
||||
|
||||
return (halnf) numFailed / (halnf) range.idxDiff();
|
||||
}
|
||||
|
||||
void numRec() {
|
||||
Dataset dataset;
|
||||
FullyConnectedNN nn;
|
||||
|
||||
Buffer<halni> layers;
|
||||
layers = { 784, 128, 10 };
|
||||
|
||||
nn.initializeRandom(layers);
|
||||
|
||||
if (!loadDataset(dataset, "rsc/mnist")) {
|
||||
printf("Cant Load Mnist Dataset\n");
|
||||
return;
|
||||
}
|
||||
|
||||
auto errorPercentage = test(dataset, nn, { 0, 100 });
|
||||
|
||||
printf("Percentage error : %f\n", errorPercentage);
|
||||
}
|
||||
|
||||
int main() {
|
||||
ModuleManifest* deps[] = { &gModuleDataAnalysis, &gModuleConnection, nullptr };
|
||||
ModuleManifest module = ModuleManifest("NumRec", nullptr, nullptr, deps);
|
||||
|
||||
if (!module.initialize()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
numRec();
|
||||
|
||||
module.deinitialize();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
BIN
DataAnalysis/applications/rsc/mnist.gz
Normal file
BIN
DataAnalysis/applications/rsc/mnist.gz
Normal file
Binary file not shown.
|
|
@ -1,8 +1,9 @@
|
|||
#include "DataAnalysisCommon.hpp"
|
||||
#include "Allocators.hpp"
|
||||
#include "ContainersCommon.hpp"
|
||||
#include "MathCommon.hpp"
|
||||
|
||||
namespace tp {
|
||||
static ModuleManifest* deps[] = {&gModuleMath, &gModuleContainers, nullptr};
|
||||
ModuleManifest gModuleDataAnalysis = ModuleManifest("DataAnalysis", nullptr, nullptr, deps);
|
||||
static ModuleManifest* deps[] = { &gModuleMath, &gModuleContainers, &gModuleAllocators, nullptr };
|
||||
ModuleManifest gModuleDataAnalysis = ModuleManifest("DataAnalysis", nullptr, nullptr, deps);
|
||||
}
|
||||
|
|
|
|||
53
DataAnalysis/private/FullyConnectedNN.cpp
Normal file
53
DataAnalysis/private/FullyConnectedNN.cpp
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
#include "FullyConnectedNN.hpp"
|
||||
#include "NewPlacement.hpp"
|
||||
#include "Utils.hpp"
|
||||
|
||||
using namespace tp;
|
||||
|
||||
static halnf sigmoid(halnf val) { return 0; }
|
||||
|
||||
static halnf relu(halnf val) { return val < 0 ? 0 : val; }
|
||||
|
||||
void FullyConnectedNN::initializeRandom(Buffer<halni> description) {
|
||||
mLayers.reserve(description.size());
|
||||
|
||||
for (auto i : Range<halni>(0, (halni) description.size())) {
|
||||
mLayers[i].mNeurons.reserve(description[i]);
|
||||
if (i == 0) {
|
||||
continue;
|
||||
}
|
||||
for (auto neuron : mLayers[i].mNeurons) {
|
||||
neuron->mWeights.reserve(description[i - 1]);
|
||||
for (auto weight : neuron->mWeights) {
|
||||
weight.data() = (halnf) randomFloat();
|
||||
}
|
||||
neuron->mBias = (halnf) randomFloat();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void FullyConnectedNN::evaluate(const Buffer<halnf>& input, Buffer<halnf>& output) {
|
||||
ASSERT(output.size() == mLayers.last().mNeurons.size() && input.size() == mLayers.first().mNeurons.size())
|
||||
|
||||
for (auto idx : Range(input.size())) {
|
||||
mLayers.first().mNeurons[idx].mActivationValue = input[idx];
|
||||
}
|
||||
|
||||
for (auto layerIdx : Range<halni>(1, (halni) mLayers.size())) {
|
||||
auto& layer = mLayers[layerIdx];
|
||||
auto& layerPrev = mLayers[layerIdx - 1];
|
||||
|
||||
for (auto neuron : layer.mNeurons) {
|
||||
neuron->mActivationValue = 0;
|
||||
for (auto connectionIdx : Range(neuron->mWeights.size())) {
|
||||
neuron->mActivationValue += neuron->mWeights[connectionIdx] * layerPrev.mNeurons[connectionIdx].mActivationValue;
|
||||
}
|
||||
neuron->mActivationValue += neuron->mBias;
|
||||
neuron->mActivationValue = relu(neuron->mActivationValue);
|
||||
}
|
||||
}
|
||||
|
||||
for (auto idx : Range(output.size())) {
|
||||
output[idx] = mLayers.last().mNeurons[idx].mActivationValue;
|
||||
}
|
||||
}
|
||||
|
|
@ -3,5 +3,5 @@
|
|||
#include "Module.hpp"
|
||||
|
||||
namespace tp {
|
||||
extern ModuleManifest gModuleDataAnalysis;
|
||||
extern ModuleManifest gModuleDataAnalysis;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,17 +4,24 @@
|
|||
#include "DataAnalysisCommon.hpp"
|
||||
|
||||
namespace tp {
|
||||
class FullyConnectedNN {
|
||||
public:
|
||||
struct Layer {
|
||||
halnf mBias;
|
||||
Buffer<halnf> mWeights;
|
||||
};
|
||||
class FullyConnectedNN {
|
||||
struct Layer {
|
||||
struct Neuron {
|
||||
halnf mBias = 0;
|
||||
Buffer<halnf> mWeights;
|
||||
halnf mActivationValue = 0;
|
||||
};
|
||||
|
||||
public:
|
||||
FullyConnectedNN() = default;
|
||||
Buffer<Neuron> mNeurons;
|
||||
};
|
||||
|
||||
private:
|
||||
Buffer<Layer> mLayers;
|
||||
};
|
||||
public:
|
||||
FullyConnectedNN() = default;
|
||||
|
||||
void initializeRandom(Buffer<halni> description);
|
||||
void evaluate(const Buffer<halnf>& input, Buffer<halnf>& output);
|
||||
|
||||
private:
|
||||
Buffer<Layer> mLayers;
|
||||
};
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,26 +1,26 @@
|
|||
|
||||
#include "FullyConnectedNN.hpp"
|
||||
#include "Testing.hpp"
|
||||
#include "Utils.hpp"
|
||||
|
||||
static bool init(const tp::ModuleManifest* self) {
|
||||
tp::gTesting.setRootName(self->getName());
|
||||
return true;
|
||||
}
|
||||
|
||||
void test() {}
|
||||
|
||||
int main() {
|
||||
tp::ModuleManifest* deps[] = {&tp::gModuleDataAnalysis, &tp::gModuleUtils, nullptr};
|
||||
tp::ModuleManifest testModule("DataAnalysisTest", init, nullptr, deps);
|
||||
|
||||
if (!testModule.initialize()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
test();
|
||||
|
||||
testModule.deinitialize();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#include "FullyConnectedNN.hpp"
|
||||
#include "Testing.hpp"
|
||||
#include "Utils.hpp"
|
||||
|
||||
static bool init(const tp::ModuleManifest* self) {
|
||||
tp::gTesting.setRootName(self->getName());
|
||||
return true;
|
||||
}
|
||||
|
||||
void test() {}
|
||||
|
||||
int main() {
|
||||
tp::ModuleManifest* deps[] = { &tp::gModuleDataAnalysis, &tp::gModuleUtils, nullptr };
|
||||
tp::ModuleManifest testModule("DataAnalysisTest", init, nullptr, deps);
|
||||
|
||||
if (!testModule.initialize()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
test();
|
||||
|
||||
testModule.deinitialize();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue