Brett 2024-10-31 14:43:37 -04:00
parent 13aa5af131
commit 1936372611
4 changed files with 49 additions and 15 deletions

View File

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.25) cmake_minimum_required(VERSION 3.25)
project(COSC-4P80-Assignment-2 VERSION 0.1.0) project(COSC-4P80-Assignment-2 VERSION 0.1.1)
option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF) option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF)
option(ENABLE_UBSAN "Enable the ub sanitizer" OFF) option(ENABLE_UBSAN "Enable the ub sanitizer" OFF)

View File

@ -21,10 +21,8 @@
#include <assign2/common.h> #include <assign2/common.h>
namespace assign2 namespace assign2
{ {
struct data_t struct data_t
{ {
bool is_bad = false; bool is_bad = false;
@ -39,6 +37,12 @@ namespace assign2
public: public:
std::vector<data_t> data_points; std::vector<data_t> data_points;
[[nodiscard]] data_file_t normalize() const;
[[nodiscard]] data_file_t with_padding(blt::size_t desired_size, Scalar padding_value = 0) const;
data_file_t& operator+=(const data_file_t& o);
data_file_t friend operator+(const data_file_t& a, const data_file_t& b);
static std::vector<data_file_t> load_data_files_from_path(std::string_view path); static std::vector<data_file_t> load_data_files_from_path(std::string_view path);
private: private:

View File

@ -107,6 +107,39 @@ namespace assign2
return data; return data;
} }
data_file_t data_file_t::normalize() const
{
auto copy = *this;
for (auto& v : copy.data_points)
v = v.normalize();
return copy;
}
data_file_t data_file_t::with_padding(blt::size_t desired_size, Scalar padding_value) const
{
auto copy = *this;
for (auto& v : copy.data_points)
v = v.with_padding(desired_size, padding_value);
return copy;
}
data_file_t& data_file_t::operator+=(const data_file_t& o)
{
data_points.insert(data_points.end(), o.data_points.begin(), o.data_points.end());
return *this;
}
data_file_t operator+(const data_file_t& a, const data_file_t& b)
{
data_file_t file = a;
file.data_points.insert(file.data_points.end(), b.data_points.begin(), b.data_points.end());
return file;
}
void save_as_csv(const std::string& file, const std::vector<std::pair<std::string, std::vector<Scalar>>>& data) void save_as_csv(const std::string& file, const std::vector<std::pair<std::string, std::vector<Scalar>>>& data)
{ {
std::ofstream stream{file}; std::ofstream stream{file};

View File

@ -2,16 +2,13 @@
#include <blt/fs/loader.h> #include <blt/fs/loader.h>
#include <blt/parse/argparse.h> #include <blt/parse/argparse.h>
#include <assign2/common.h> #include <assign2/common.h>
#include <filesystem>
#include "blt/iterator/enumerate.h" #include "blt/iterator/enumerate.h"
#include <assign2/layer.h> #include <assign2/layer.h>
#include <assign2/functions.h> #include <assign2/functions.h>
#include <assign2/network.h> #include <assign2/network.h>
#include <assign2/file.h> #include <assign2/file.h>
#include <memory>
#include <thread> #include <thread>
#include <algorithm> #include <algorithm>
#include <mutex>
using namespace assign2; using namespace assign2;
@ -35,20 +32,17 @@ network_t create_network(blt::i32 input, blt::i32 hidden)
const auto inner_mul = 0.25; const auto inner_mul = 0.25;
auto layer1 = std::make_unique<layer_t>(input, hidden * mul, &sig, randomizer, empty); auto layer1 = std::make_unique<layer_t>(input, hidden * mul, &sig, randomizer, empty);
auto layer2 = std::make_unique<layer_t>(hidden * mul, hidden * inner_mul, &sig, randomizer, empty); auto layer2 = std::make_unique<layer_t>(hidden * mul, hidden * inner_mul, &sig, randomizer, empty);
// auto layer3 = std::make_unique<layer_t>(hidden * inner_mul, hidden * inner_mul, &sig, randomizer, empty); auto layer_output = std::make_unique<layer_t>(hidden * inner_mul, 2, &relu, randomizer, empty);
// auto layer4 = std::make_unique<layer_t>(hidden * inner_mul, hidden * inner_mul, &sig, randomizer, empty);
auto layer_output = std::make_unique<layer_t>(hidden * inner_mul, 2, &sig, randomizer, empty);
std::vector<std::unique_ptr<layer_t>> vec; std::vector<std::unique_ptr<layer_t>> vec;
vec.push_back(std::move(layer1)); vec.push_back(std::move(layer1));
vec.push_back(std::move(layer2)); vec.push_back(std::move(layer2));
// vec.push_back(std::move(layer3));
// vec.push_back(std::move(layer4));
vec.push_back(std::move(layer_output)); vec.push_back(std::move(layer_output));
network_t network{std::move(vec)}; network_t network{std::move(vec)};
if (with_momentum) if (with_momentum)
network.with_momentum(&omega); network.with_momentum(&omega);
return network; return network;
} }
@ -606,7 +600,7 @@ int main(int argc, const char** argv)
int input = static_cast<int>(f.data_points.begin()->bins.size()); int input = static_cast<int>(f.data_points.begin()->bins.size());
int hidden = input; int hidden = input;
if (input != 64) if (input != 16)
continue; continue;
BLT_INFO("-----------------"); BLT_INFO("-----------------");
@ -616,9 +610,12 @@ int main(int argc, const char** argv)
network_t network = create_network(input, hidden); network_t network = create_network(input, hidden);
float o = 0.00001; auto output = network.execute(f.data_points.front().bins);
// network.with_momentum(&o); print_vec(output) << std::endl;
for (blt::size_t i = 0; i < 10000; i++)
float o = 0.0001;
network.with_momentum(&o);
for (blt::size_t i = 0; i < 300; i++)
network.train_epoch(f, 1); network.train_epoch(f, 1);
BLT_INFO("Test Cases:"); BLT_INFO("Test Cases:");