2024-10-21 16:42:03 -04:00
|
|
|
#pragma once
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2024 Brett Terpstra
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef COSC_4P80_ASSIGNMENT_2_COMMON_H
|
|
|
|
#define COSC_4P80_ASSIGNMENT_2_COMMON_H
|
|
|
|
|
2024-10-25 01:22:32 -04:00
|
|
|
#include <iostream>
|
|
|
|
#include <blt/iterator/enumerate.h>
|
2024-10-21 19:25:00 -04:00
|
|
|
|
2024-10-21 16:42:03 -04:00
|
|
|
namespace assign2
|
|
|
|
{
|
2024-10-23 01:51:32 -04:00
|
|
|
using Scalar = float;
|
2024-10-25 01:22:32 -04:00
|
|
|
const inline Scalar learn_rate = 0.1;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2024-10-23 01:51:32 -04:00
|
|
|
|
2024-10-21 16:42:03 -04:00
|
|
|
struct data_t
|
|
|
|
{
|
|
|
|
bool is_bad = false;
|
2024-10-23 01:51:32 -04:00
|
|
|
std::vector<Scalar> bins;
|
2024-10-21 16:42:03 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct data_file_t
|
|
|
|
{
|
|
|
|
std::vector<data_t> data_points;
|
|
|
|
};
|
2024-10-21 19:25:00 -04:00
|
|
|
|
|
|
|
class layer_t;
|
2024-10-25 01:22:32 -04:00
|
|
|
|
2024-10-21 19:25:00 -04:00
|
|
|
class network_t;
|
|
|
|
|
2024-10-25 01:22:32 -04:00
|
|
|
struct function_t
|
|
|
|
{
|
|
|
|
[[nodiscard]] virtual Scalar call(Scalar) const = 0;
|
|
|
|
|
|
|
|
[[nodiscard]] virtual Scalar derivative(Scalar) const = 0;
|
|
|
|
};
|
|
|
|
|
2024-10-23 01:51:32 -04:00
|
|
|
struct weight_view
|
|
|
|
{
|
|
|
|
public:
|
2024-10-25 01:22:32 -04:00
|
|
|
weight_view(Scalar* data, blt::size_t size): m_data(data), m_size(size)
|
2024-10-23 01:51:32 -04:00
|
|
|
{}
|
|
|
|
|
2024-10-25 01:22:32 -04:00
|
|
|
inline Scalar& operator[](blt::size_t index) const
|
2024-10-23 01:51:32 -04:00
|
|
|
{
|
|
|
|
#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:
|
2024-10-25 01:22:32 -04:00
|
|
|
Scalar* m_data;
|
2024-10-23 01:51:32 -04:00
|
|
|
blt::size_t m_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* this class exists purely as an optimization
|
|
|
|
*/
|
|
|
|
class weight_t
|
|
|
|
{
|
|
|
|
public:
|
2024-10-25 14:20:18 -04:00
|
|
|
void preallocate(blt::size_t amount)
|
|
|
|
{
|
|
|
|
data.resize(amount);
|
|
|
|
}
|
|
|
|
|
2024-10-23 01:51:32 -04:00
|
|
|
weight_view allocate_view(blt::size_t count)
|
|
|
|
{
|
2024-10-25 14:20:18 -04:00
|
|
|
auto size = place;
|
|
|
|
place += count;
|
2024-10-23 01:51:32 -04:00
|
|
|
return {&data[size], count};
|
|
|
|
}
|
2024-10-25 01:22:32 -04:00
|
|
|
|
|
|
|
void debug() const
|
|
|
|
{
|
|
|
|
std::cout << "Weights: ";
|
|
|
|
print_vec(data) << std::endl;
|
|
|
|
}
|
2024-10-23 01:51:32 -04:00
|
|
|
|
|
|
|
private:
|
2024-10-25 14:20:18 -04:00
|
|
|
blt::size_t place = 0;
|
2024-10-25 01:22:32 -04:00
|
|
|
std::vector<Scalar> data;
|
2024-10-23 01:51:32 -04:00
|
|
|
};
|
2024-10-25 01:22:32 -04:00
|
|
|
|
2024-10-21 16:42:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif //COSC_4P80_ASSIGNMENT_2_COMMON_H
|