work
parent
e0b6f13834
commit
1b79238114
|
@ -0,0 +1,7 @@
|
|||
<component name="CopyrightManager">
|
||||
<settings default="GPL3">
|
||||
<module2copyright>
|
||||
<element module="All" copyright="GPL3" />
|
||||
</module2copyright>
|
||||
</settings>
|
||||
</component>
|
|
@ -1,10 +1,10 @@
|
|||
cmake_minimum_required(VERSION 3.25)
|
||||
project(COSC-4P80-Assignment-2 VERSION 0.0.2)
|
||||
project(COSC-4P80-Assignment-2 VERSION 0.0.3)
|
||||
|
||||
option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF)
|
||||
option(ENABLE_UBSAN "Enable the ub sanitizer" OFF)
|
||||
option(ENABLE_TSAN "Enable the thread data race sanitizer" OFF)
|
||||
option(EIGEN_TEST_CXX11 "Enable testing with C++11 and C++11 features (e.g. Tensor module)." ON)
|
||||
#option(EIGEN_TEST_CXX11 "Enable testing with C++11 and C++11 features (e.g. Tensor module)." ON)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
|
@ -14,7 +14,7 @@ endif()
|
|||
|
||||
add_subdirectory(lib/blt)
|
||||
|
||||
add_subdirectory(lib/eigen-3.4.0)
|
||||
#add_subdirectory(lib/eigen-3.4.0)
|
||||
|
||||
include_directories(include/)
|
||||
file(GLOB_RECURSE PROJECT_BUILD_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
|
||||
|
@ -24,7 +24,8 @@ add_executable(COSC-4P80-Assignment-2 ${PROJECT_BUILD_FILES})
|
|||
target_compile_options(COSC-4P80-Assignment-2 PRIVATE -Wall -Wextra -Wpedantic -Wno-comment)
|
||||
target_link_options(COSC-4P80-Assignment-2 PRIVATE -Wall -Wextra -Wpedantic -Wno-comment)
|
||||
|
||||
target_link_libraries(COSC-4P80-Assignment-2 PRIVATE BLT Eigen3::Eigen)
|
||||
#target_link_libraries(COSC-4P80-Assignment-2 PRIVATE BLT Eigen3::Eigen)
|
||||
target_link_libraries(COSC-4P80-Assignment-2 PRIVATE BLT)
|
||||
|
||||
if (${ENABLE_ADDRSAN} MATCHES ON)
|
||||
target_compile_options(COSC-4P80-Assignment-2 PRIVATE -fsanitize=address)
|
||||
|
|
|
@ -19,14 +19,15 @@
|
|||
#ifndef COSC_4P80_ASSIGNMENT_2_COMMON_H
|
||||
#define COSC_4P80_ASSIGNMENT_2_COMMON_H
|
||||
|
||||
#include <Eigen/Dense>
|
||||
|
||||
namespace assign2
|
||||
{
|
||||
using Scalar = float;
|
||||
|
||||
struct data_t
|
||||
{
|
||||
bool is_bad = false;
|
||||
std::vector<float> bins;
|
||||
std::vector<Scalar> bins;
|
||||
};
|
||||
|
||||
struct data_file_t
|
||||
|
@ -37,9 +38,57 @@ namespace assign2
|
|||
class layer_t;
|
||||
class network_t;
|
||||
|
||||
using Scalar = float;
|
||||
using matrix_t = Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>;
|
||||
using vector_t = Eigen::Matrix<Scalar, Eigen::Dynamic, 1>;
|
||||
struct weight_view
|
||||
{
|
||||
public:
|
||||
weight_view(double* data, blt::size_t size): m_data(data), m_size(size)
|
||||
{}
|
||||
|
||||
inline double& operator[](blt::size_t index) const
|
||||
{
|
||||
#if BLT_DEBUG_LEVEL > 0
|
||||
if (index >= size)
|
||||
throw std::runtime_error("Index is out of bounds!");
|
||||
#endif
|
||||
return m_data[index];
|
||||
}
|
||||
|
||||
[[nodiscard]] inline blt::size_t size() const
|
||||
{
|
||||
return m_size;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto begin() const
|
||||
{
|
||||
return m_data;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto end() const
|
||||
{
|
||||
return m_data + m_size;
|
||||
}
|
||||
|
||||
private:
|
||||
double* m_data;
|
||||
blt::size_t m_size;
|
||||
};
|
||||
|
||||
/**
|
||||
* this class exists purely as an optimization
|
||||
*/
|
||||
class weight_t
|
||||
{
|
||||
public:
|
||||
weight_view allocate_view(blt::size_t count)
|
||||
{
|
||||
auto size = data.size();
|
||||
data.resize(size + count);
|
||||
return {&data[size], count};
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<double> data;
|
||||
};
|
||||
}
|
||||
|
||||
#endif //COSC_4P80_ASSIGNMENT_2_COMMON_H
|
||||
|
|
|
@ -35,13 +35,6 @@ namespace assign2
|
|||
{
|
||||
return call(s) * (1 - call(s));
|
||||
}
|
||||
|
||||
vector_t operator()(vector_t out) const
|
||||
{
|
||||
for (auto& v : out)
|
||||
v = call(v);
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
struct linear_function
|
||||
|
|
|
@ -27,50 +27,35 @@ namespace assign2
|
|||
{
|
||||
struct empty_init
|
||||
{
|
||||
template<int rows, int columns>
|
||||
inline void operator()(Eigen::Matrix<Scalar, rows, columns>& matrix) const
|
||||
inline Scalar operator()(blt::i32) const
|
||||
{
|
||||
for (auto r : matrix.rowwise())
|
||||
{
|
||||
for (auto& v : r)
|
||||
v = 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
struct half_init
|
||||
{
|
||||
template<int rows, int columns>
|
||||
inline void operator()(Eigen::Matrix<Scalar, rows, columns>& matrix) const
|
||||
inline Scalar operator()(blt::i32) const
|
||||
{
|
||||
for (auto r : matrix.rowwise())
|
||||
{
|
||||
for (auto& v : r)
|
||||
v = 0.5f;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
struct random_init
|
||||
{
|
||||
public:
|
||||
explicit random_init(blt::size_t seed, float min = 0.5 - 0.125, float max = 0.5 + 0.125): seed(seed), min(min), max(max)
|
||||
explicit random_init(blt::size_t seed, Scalar min = -0.5, Scalar max = 0.5): random(seed), seed(seed), min(min), max(max)
|
||||
{}
|
||||
|
||||
template<int rows, int columns>
|
||||
inline void operator()(Eigen::Matrix<Scalar, rows, columns>& matrix) const
|
||||
inline Scalar operator()(blt::i32)
|
||||
{
|
||||
blt::random::random_t random(seed);
|
||||
for (auto r : matrix.rowwise())
|
||||
{
|
||||
for (auto& v : r)
|
||||
v = random.get_float(min, max);
|
||||
}
|
||||
return static_cast<Scalar>(random.get_double(min, max));
|
||||
}
|
||||
|
||||
private:
|
||||
blt::random::random_t random;
|
||||
blt::size_t seed;
|
||||
float min, max;
|
||||
Scalar min, max;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -21,41 +21,109 @@
|
|||
|
||||
#include <blt/std/types.h>
|
||||
#include <assign2/initializers.h>
|
||||
#include "blt/iterator/zip.h"
|
||||
#include "blt/iterator/iterator.h"
|
||||
|
||||
namespace assign2
|
||||
{
|
||||
class layer_t
|
||||
class neuron_t
|
||||
{
|
||||
public:
|
||||
layer_t(const blt::i32 in, const blt::i32 out): in_size(in), out_size(out)
|
||||
// empty neuron for loading from a stream
|
||||
explicit neuron_t(weight_view weights): weights(weights)
|
||||
{}
|
||||
|
||||
template<typename WeightsFunc = empty_init, typename BiasFunc = empty_init>
|
||||
void init(WeightsFunc weightFunc = empty_init{}, BiasFunc biasFunc = empty_init{})
|
||||
// neuron with bias
|
||||
explicit neuron_t(weight_view weights, Scalar bias): bias(bias), weights(weights)
|
||||
{}
|
||||
|
||||
template<typename ActFunc>
|
||||
Scalar activate(const Scalar* inputs, ActFunc func) const
|
||||
{
|
||||
weights.resize(in_size, out_size);
|
||||
bias.resize(out_size);
|
||||
|
||||
weightFunc(weights);
|
||||
biasFunc(bias);
|
||||
auto sum = bias;
|
||||
for (auto [x, w] : blt::zip_iterator_container({inputs, inputs + weights.size()}, {weights.begin(), weights.end()}))
|
||||
sum += x * w;
|
||||
return func.call(sum);
|
||||
}
|
||||
|
||||
template<typename ActFunction>
|
||||
vector_t call(const vector_t& in, ActFunction func = ActFunction{})
|
||||
template<typename OStream>
|
||||
OStream& serialize(OStream& stream)
|
||||
{
|
||||
vector_t out;
|
||||
out.resize(out_size, Eigen::NoChange_t{});
|
||||
out.noalias() = weights.transpose() * in;
|
||||
out.colwise() += bias;
|
||||
return func(std::move(out));
|
||||
stream << bias;
|
||||
for (auto d : weights)
|
||||
stream << d;
|
||||
}
|
||||
|
||||
template<typename IStream>
|
||||
IStream& deserialize(IStream& stream)
|
||||
{
|
||||
for (auto& d : blt::iterate(weights).rev())
|
||||
stream >> d;
|
||||
stream >> bias;
|
||||
}
|
||||
|
||||
private:
|
||||
Scalar bias = 0;
|
||||
weight_view weights;
|
||||
};
|
||||
|
||||
class layer_t
|
||||
{
|
||||
public:
|
||||
template<typename WeightFunc, typename BiasFunc>
|
||||
layer_t(const blt::i32 in, const blt::i32 out, WeightFunc w, BiasFunc b): in_size(in), out_size(out)
|
||||
{
|
||||
neurons.reserve(out_size);
|
||||
for (blt::i32 i = 0; i < out_size; i++)
|
||||
{
|
||||
auto weight = weights.allocate_view(in_size);
|
||||
for (auto& v : weight)
|
||||
v = w(i);
|
||||
neurons.push_back(neuron_t{weight, b(i)});
|
||||
}
|
||||
}
|
||||
|
||||
template<typename ActFunction>
|
||||
std::vector<Scalar> call(const std::vector<Scalar>& in, ActFunction func = ActFunction{})
|
||||
{
|
||||
std::vector<Scalar> out;
|
||||
out.reserve(out_size);
|
||||
#if BLT_DEBUG_LEVEL > 0
|
||||
if (in.size() != in_size)
|
||||
throw std::runtime_exception("Input vector doesn't match expected input size!");
|
||||
#endif
|
||||
for (auto& n : neurons)
|
||||
out.push_back(n.activate(in.data(), func));
|
||||
return out;
|
||||
}
|
||||
|
||||
template<typename OStream>
|
||||
OStream& serialize(OStream& stream)
|
||||
{
|
||||
for (auto d : neurons)
|
||||
stream << d;
|
||||
}
|
||||
|
||||
template<typename IStream>
|
||||
IStream& deserialize(IStream& stream)
|
||||
{
|
||||
for (auto& d : blt::iterate(neurons).rev())
|
||||
stream >> d;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline blt::i32 get_in_size() const
|
||||
{
|
||||
return in_size;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline blt::i32 get_out_size() const
|
||||
{
|
||||
return out_size;
|
||||
}
|
||||
private:
|
||||
const blt::i32 in_size, out_size;
|
||||
matrix_t weights{};
|
||||
matrix_t dweights{}; // derivative of weights
|
||||
vector_t bias{};
|
||||
vector_t dbias{}; // derivative of bias
|
||||
weight_t weights;
|
||||
std::vector<neuron_t> neurons;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -27,17 +27,98 @@ namespace assign2
|
|||
class network_t
|
||||
{
|
||||
public:
|
||||
network_t(blt::i32 input_size, blt::i32 output_size, blt::i32 hidden_count, blt::i32 hidden_size):
|
||||
input_size(input_size), output_size(output_size), hidden_count(hidden_count), hidden_size(hidden_size)
|
||||
template<typename WeightFunc, typename BiasFunc>
|
||||
network_t(blt::i32 input_size, blt::i32 output_size, blt::i32 layer_count, blt::i32 hidden_size, WeightFunc w, BiasFunc b):
|
||||
input_size(input_size), output_size(output_size), hidden_count(layer_count), hidden_size(hidden_size)
|
||||
{
|
||||
if (hidden_count > 0)
|
||||
if (layer_count > 0)
|
||||
{
|
||||
layers.push_back(layer_t{input_size, hidden_size});
|
||||
for (blt::i32 i = 1; i < hidden_count; i++)
|
||||
layers.push_back(layer_t{hidden_size, hidden_size});
|
||||
layers.push_back(layer_t{hidden_size, output_size});
|
||||
for (blt::i32 i = 0; i < layer_count; i++)
|
||||
{
|
||||
if (i == 0)
|
||||
layers.push_back(layer_t{input_size, hidden_size, w, b});
|
||||
else
|
||||
layers.push_back(layer_t{hidden_size, hidden_size, w, b});
|
||||
}
|
||||
layers.push_back(layer_t{hidden_size, output_size, w, b});
|
||||
} else
|
||||
layers.push_back(layer_t{input_size, output_size});
|
||||
{
|
||||
layers.push_back(layer_t{input_size, output_size, w, b});
|
||||
}
|
||||
}
|
||||
|
||||
template<typename WeightFunc, typename BiasFunc, typename OutputWeightFunc, typename OutputBiasFunc>
|
||||
network_t(blt::i32 input_size, blt::i32 output_size, blt::i32 layer_count, blt::i32 hidden_size,
|
||||
WeightFunc w, BiasFunc b, OutputWeightFunc ow, OutputBiasFunc ob):
|
||||
input_size(input_size), output_size(output_size), hidden_count(layer_count), hidden_size(hidden_size)
|
||||
{
|
||||
if (layer_count > 0)
|
||||
{
|
||||
for (blt::i32 i = 0; i < layer_count; i++)
|
||||
{
|
||||
if (i == 0)
|
||||
layers.push_back(layer_t{input_size, hidden_size, w, b});
|
||||
else
|
||||
layers.push_back(layer_t{hidden_size, hidden_size, w, b});
|
||||
}
|
||||
layers.push_back(layer_t{hidden_size, output_size, ow, ob});
|
||||
} else
|
||||
{
|
||||
layers.push_back(layer_t{input_size, output_size, ow, ob});
|
||||
}
|
||||
}
|
||||
|
||||
explicit network_t(std::vector<layer_t> layers):
|
||||
input_size(layers.begin()->get_in_size()), output_size(layers.end()->get_out_size()),
|
||||
hidden_count(static_cast<blt::i32>(layers.size()) - 1), hidden_size(layers.end()->get_in_size()), layers(std::move(layers))
|
||||
{}
|
||||
|
||||
network_t() = default;
|
||||
|
||||
template<typename ActFunc, typename ActFuncOut>
|
||||
std::vector<Scalar> execute(const std::vector<Scalar>& input, ActFunc func, ActFuncOut outFunc)
|
||||
{
|
||||
std::vector<Scalar> previous_output;
|
||||
std::vector<Scalar> current_output;
|
||||
|
||||
for (auto [i, v] : blt::enumerate(layers))
|
||||
{
|
||||
previous_output = current_output;
|
||||
if (i == 0)
|
||||
current_output = v.call(input, func);
|
||||
else if (i == layers.size() - 1)
|
||||
current_output = v.call(previous_output, outFunc);
|
||||
else
|
||||
current_output = v.call(previous_output, func);
|
||||
}
|
||||
|
||||
return current_output;
|
||||
}
|
||||
|
||||
Scalar train(const data_file_t& example)
|
||||
{
|
||||
const Scalar learn_rate = 0.1;
|
||||
|
||||
Scalar total_error = 0;
|
||||
for (const auto& x : example.data_points)
|
||||
{
|
||||
auto o = execute(x.bins, sigmoid_function{}, sigmoid_function{});
|
||||
auto y = x.is_bad ? 1.0f : 0.0f;
|
||||
|
||||
Scalar is_bad = 0;
|
||||
if (o[0] >= 1)
|
||||
is_bad = 0;
|
||||
else if (o[1] >= 1)
|
||||
is_bad = 1;
|
||||
|
||||
auto error = y - is_bad;
|
||||
if (o[0] >= 1 && o[1] >= 1)
|
||||
error += 1;
|
||||
|
||||
total_error += error;
|
||||
|
||||
}
|
||||
return total_error;
|
||||
}
|
||||
|
||||
private:
|
||||
|
|
37
src/main.cpp
37
src/main.cpp
|
@ -6,6 +6,7 @@
|
|||
#include "blt/iterator/enumerate.h"
|
||||
#include <assign2/layer.h>
|
||||
#include <assign2/functions.h>
|
||||
#include <assign2/network.h>
|
||||
|
||||
using namespace assign2;
|
||||
|
||||
|
@ -67,6 +68,18 @@ std::vector<data_file_t> load_data_files(const std::vector<std::string>& files)
|
|||
return loaded_data;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
decltype(std::cout)& print_vec(const std::vector<T>& vec)
|
||||
{
|
||||
for (auto [i, v] : blt::enumerate(vec))
|
||||
{
|
||||
std::cout << v;
|
||||
if (i != vec.size() - 1)
|
||||
std::cout << ", ";
|
||||
}
|
||||
return std::cout;
|
||||
}
|
||||
|
||||
int main(int argc, const char** argv)
|
||||
{
|
||||
blt::arg_parse parser;
|
||||
|
@ -77,35 +90,29 @@ int main(int argc, const char** argv)
|
|||
|
||||
auto data_files = load_data_files(get_data_files(data_directory));
|
||||
|
||||
vector_t input;
|
||||
std::vector<Scalar> input;
|
||||
input.resize(16);
|
||||
for (auto f : data_files)
|
||||
{
|
||||
if (f.data_points.begin()->bins.size() == 16)
|
||||
{
|
||||
for (auto [i, b] : blt::enumerate(f.data_points.begin()->bins))
|
||||
input(static_cast<Eigen::Index>(i)) = b;
|
||||
input[i] = b;
|
||||
}
|
||||
}
|
||||
|
||||
random_init randomizer{619};
|
||||
sigmoid_function sig;
|
||||
|
||||
layer_t layer1{16, 4};
|
||||
layer_t layer2{4, 4};
|
||||
layer_t layer3{4, 4};
|
||||
layer_t layer_output{4, 1};
|
||||
layer1.init(randomizer, empty_init{});
|
||||
layer2.init(randomizer, empty_init{});
|
||||
layer3.init(randomizer, empty_init{});
|
||||
layer_output.init(randomizer, empty_init{});
|
||||
layer_t layer1{16, 4, randomizer, empty_init{}};
|
||||
layer_t layer2{4, 4, randomizer, empty_init{}};
|
||||
layer_t layer3{4, 4, randomizer, empty_init{}};
|
||||
layer_t layer_output{4, 1, randomizer, empty_init{}};
|
||||
|
||||
auto output = layer1.call(input, sig);
|
||||
output = layer2.call(output, sig);
|
||||
output = layer3.call(output, sig);
|
||||
output = layer_output.call(output, sig);
|
||||
network_t network{{layer1, layer2, layer3, layer_output}};
|
||||
|
||||
std::cout << output << std::endl;
|
||||
auto output = network.execute(input, sig, sig);
|
||||
print_vec(output) << std::endl;
|
||||
|
||||
// for (auto d : data_files)
|
||||
// {
|
||||
|
|
Loading…
Reference in New Issue