diff --git a/include/mat.hpp b/include/mat.hpp new file mode 100644 index 0000000..6b3026e --- /dev/null +++ b/include/mat.hpp @@ -0,0 +1,239 @@ +#ifndef MAT_HPP +#define MAT_HPP + +#include +#include +#include +#include + +template = 1)>> +class mat_col { + template + friend class mat; +private: + mat_col(T* data) + : m_data(data) {} +public: + T& operator[](std::size_t idx) { return m_data[idx]; } + const T& operator[](std::size_t idx) const { return m_data[idx]; } +private: + T* m_data; +}; + +template = 1 && N >= 1)>> +class mat { +public: + using col_type = mat_col; +public: + mat() { std::memset(m_items, 0, sizeof(m_items)); } + + mat(T init) { + for (std::size_t i = 0; i < M; ++i) { + for (std::size_t j = 0; j < N; ++j) { + (*this)[i][j] = init; + } + } + } + + mat(col_type col) { + static_assert((M == 1), "Invalid"); + std::memcpy(m_items, col.m_data, sizeof(m_items)); + } + + ~mat() = default; + + mat(mat&& other) noexcept { std::memcpy(m_items, other.m_items, sizeof(m_items)); } + mat& operator=(mat&& other) noexcept { + if (this == &other) return *this; + std::memcpy(m_items, other.m_items, sizeof(m_items)); + return *this; + } + + mat(const mat& other) { std::memcpy(m_items, other.m_items, sizeof(m_items)); } + mat& operator=(const mat& other) { + if (this == &other) return *this; + std::memcpy(m_items, other.m_items, sizeof(m_items)); + return *this; + } +public: + template + static mat random(Dist dist) { + static std::random_device s_rd; + static std::mt19937 s_mt(s_rd()); + + mat res; + for (std::size_t i = 0; i < M; ++i) { + for (std::size_t j = 0; j < N; ++j) { + res[i][j] = dist(s_mt); + } + } + + return res; + } +public: + mat_col operator[](std::size_t col) { return { m_items + (col * M) }; } + + mat_col operator[](std::size_t col) const { return { m_items + (col * M) }; } +public: + mat operator+(const mat& rhs) const { + mat res; + for (std::size_t i = 0; i < M; ++i) { + for (std::size_t j = 0; j < N; ++j) { + res[i][j] = (*this)[i][j] + rhs[i][j]; + } + } + + return res; + } + + mat& operator+=(const mat& rhs) { + for (std::size_t i = 0; i < M; ++i) { + for (std::size_t j = 0; j < N; ++j) { + (*this)[i][j] += rhs[i][j]; + } + } + + return *this; + } + + mat operator-(const mat& rhs) const { + mat res; + for (std::size_t i = 0; i < M; ++i) { + for (std::size_t j = 0; j < N; ++j) { + res[i][j] = (*this)[i][j] - rhs[i][j]; + } + } + + return res; + } + + mat& operator-=(const mat& rhs) { + for (std::size_t i = 0; i < M; ++i) { + for (std::size_t j = 0; j < N; ++j) { + (*this)[i][j] -= rhs[i][j]; + } + } + + return *this; + } +public: + mat hadamard(const mat& rhs) const { + mat res = *this; + for (std::size_t i = 0; i < M; ++i) { + for (std::size_t j = 0; j < N; ++j) { + res[i][j] *= rhs[i][j]; + } + } + + return res; + } +public: + mat transpose() const { + mat res; + for (std::size_t i = 0; i < M; ++i) { + for (std::size_t j = 0; j < N; ++j) { + res[j][i] = (*this)[i][j]; + } + } + + return res; + } +private: + T m_items[M * N]; +}; + +template +mat operator+(T scalar, const mat& rhs) { + mat res; + for (std::size_t i = 0; i < M; ++i) { + for (std::size_t j = 0; j < N; ++j) { + res[i][j] = scalar + rhs[i][j]; + } + } + + return res; +} + +template +mat operator-(T scalar, const mat& rhs) { + mat res; + for (std::size_t i = 0; i < M; ++i) { + for (std::size_t j = 0; j < N; ++j) { + res[i][j] = scalar - rhs[i][j]; + } + } + + return res; +} + +template +mat<1, N, T> operator+(T scalar, const mat_col& rhs) { + mat<1, N, T> res; + for (std::size_t j = 0; j < N; ++j) { + res[0][j] = scalar + rhs[j]; + } + + return res; +} + +template +mat<1, N, T> operator-(T scalar, const mat_col& rhs) { + mat<1, N, T> res; + for (std::size_t j = 0; j < N; ++j) { + res[0][j] = scalar - rhs[j]; + } + + return res; +} + +template +mat operator*(const mat& lhs, T rhs) { + mat res; + for (std::size_t i = 0; i < M; ++i) { + for (std::size_t j = 0; j < N; ++j) { + res[i][j] = lhs[i][j] * rhs; + } + } + + return res; +} + +template +mat operator*(T lhs, const mat& rhs) { + mat res; + for (std::size_t i = 0; i < M; ++i) { + for (std::size_t j = 0; j < N; ++j) { + res[i][j] = rhs[i][j] * lhs; + } + } + + return res; +} + +template +mat operator*(const mat& lhs, const mat& rhs) { + mat res; + for (std::size_t i = 0; i < M; ++i) { + for (std::size_t j = 0; j < P; ++j) { + for (std::size_t k = 0; k < N; ++k) { + res[i][j] += lhs[i][k] * rhs[k][j]; + } + } + } + + return res; +} + +template +mat operator*(const mat& lhs, const mat_col& rhs) { + mat res; + for (std::size_t i = 0; i < M; ++i) { + for (std::size_t j = 0; j < P; ++j) { + res[i][j] += lhs[i][0] * rhs[j]; + } + } + + return res; +} + +#endif // MAT_HPP diff --git a/notes/xor.kra b/notes/xor.kra index b196ecb..635be32 100644 Binary files a/notes/xor.kra and b/notes/xor.kra differ diff --git a/src/main.cpp b/src/main.cpp index b2ba1ac..b54b3bc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,5 @@ +#include "mat.hpp" + #include #include #include @@ -7,51 +9,58 @@ static double sigmoid(double input) { return 1 / (1 + std::exp(-input)); } -static double random_double() { - static std::random_device s_rd; - static std::mt19937 s_mt(s_rd()); - static std::uniform_real_distribution<> s_dist(0, 1); - return s_dist(s_mt); -} +static std::uniform_real_distribution<> dist(-1, 1); struct network { - double l1ws[4] = { random_double(), random_double(), random_double(), random_double() }; - double l1bs[2] = { random_double(), random_double() }; - double l2ws[2] = { random_double(), random_double() }; - double l2bs[1] = { random_double() }; + mat<2, 2> hiddenWeights = mat<2, 2>::random(dist); + mat<1, 2> hiddenBiases = mat<1, 2>::random(dist); + mat<1, 2> hiddenOutputs; + mat<2, 1> outputWeights = mat<2, 1>::random(dist); + mat<1, 1> outputBiases = mat<1, 1>::random(dist); + mat<1, 1> output; - double feed(double x1, double x2) const { return sigmoid(z21(x1, x2)); } + void feed(const mat<1, 2>& xs) { + hidden(xs); + output[0][0] = sigmoid(((hiddenOutputs * outputWeights) + outputBiases)[0][0]); + } - void apply(double x1, double x2, double yHat, double yEx, double eps) { - double h1Comp = h1(x1, x2); - double h1Sq = h1Comp * h1Comp; - double h2Comp = h2(x1, x2); - double h2Sq = h2Comp * h2Comp; + void apply(const mat<1, 2>& xs, double yHat, double yEx, double eps) { + auto outputErr = (yHat - yEx) * yHat * (1 - yHat); + auto owGradient = hiddenOutputs.transpose() * outputErr; + auto backpropErr = outputErr * outputWeights.transpose(); + auto hiddenErr = backpropErr.hadamard((hiddenOutputs.hadamard(1.0 - hiddenOutputs))); + auto hwGradient = xs.transpose() * hiddenErr; - double dZ21 = (yHat - yEx) * yHat * (1.0 - yHat); - double dZ11 = dZ21 * l2ws[0] * h1Comp * (1.0 - h1Comp); - double dZ12 = dZ21 * l2ws[1] * h2Comp * (1.0 - h2Comp); + outputWeights -= eps * owGradient; + outputBiases -= mat<1, 1>{ eps * outputErr }; + hiddenWeights -= eps * hwGradient; + hiddenBiases -= eps * hiddenErr; - l1ws[0] -= eps * dZ11 * x1; - l1ws[1] -= eps * dZ12 * x1; - l1ws[2] -= eps * dZ11 * x2; - l1ws[3] -= eps * dZ12 * x2; - l1bs[0] -= eps * dZ11; - l1bs[1] -= eps * dZ12; - l2ws[0] -= eps * dZ21 * h1Comp; - l2ws[1] -= eps * dZ21 * h2Comp; - l2bs[0] -= eps * dZ21; + // double h1Comp = h1(x1, x2); + // double h1Sq = h1Comp * h1Comp; + // double h2Comp = h2(x1, x2); + // double h2Sq = h2Comp * h2Comp; + + // double dZ21 = (yHat - yEx) * yHat * (1.0 - yHat); + // double dZ11 = dZ21 * l2ws[0] * h1Comp * (1.0 - h1Comp); + // double dZ12 = dZ21 * l2ws[1] * h2Comp * (1.0 - h2Comp); + + // l1ws[0] -= eps * dZ11 * x1; + // l1ws[1] -= eps * dZ12 * x1; + // l1ws[2] -= eps * dZ11 * x2; + // l1ws[3] -= eps * dZ12 * x2; + // l1bs[0] -= eps * dZ11; + // l1bs[1] -= eps * dZ12; + // l2ws[0] -= eps * dZ21 * h1Comp; + // l2ws[1] -= eps * dZ21 * h2Comp; + // l2bs[0] -= eps * dZ21; } private: - double z21(double x1, double x2) const { return (h1(x1, x2) * l2ws[0]) + (h2(x1, x2) * l2ws[1]) + l2bs[0]; } - - double h1(double x1, double x2) const { return sigmoid(z11(x1, x2)); } - - double z11(double x1, double x2) const { return (x1 * l1ws[0]) + (x2 * l1ws[2]) + l1bs[0]; } - - double h2(double x1, double x2) const { return sigmoid(z12(x1, x2)); } - - double z12(double x1, double x2) const { return (x1 * l1ws[1]) + (x2 * l1ws[3]) + l1bs[1]; } + void hidden(const mat<1, 2>& xs) { + hiddenOutputs = (xs * hiddenWeights) + hiddenBiases; + for (std::size_t i = 0; i < 2; ++i) + hiddenOutputs[0][i] = sigmoid(hiddenOutputs[0][i]); + } }; int main(void) { @@ -64,29 +73,46 @@ int main(void) { network net; - static constexpr double EPSILON = 1.0; + static constexpr double EPSILON = 0.2; std::size_t gen = 0; - while (true) { - std::cout << "--- Generation " << ++gen << '\n'; - double yHats[4]; + + while (gen++ < 100'000) { for (std::size_t i = 0; i < 4; ++i) { - auto x1 = static_cast(i & 1); - auto x2 = static_cast(i > 1); - double yHat = yHats[i] = net.feed(x1, x2); - std::cout << x1 << " ^ " << x2 << " = " << yHat << " (expected: " << s_trainData[i] - << "), cost = " << (1.0 / 2.0 * std::pow(yHat - s_trainData[i], 2)) << "\n"; + mat<1, 2> xs; + xs[0][0] = static_cast(i & 1); + xs[0][1] = static_cast(i > 1); + + net.feed(xs); + + double yHat = net.output[0][0]; + + net.apply(xs, yHat, s_trainData[i], EPSILON); } - std::cout << "Total cost = "; + double acc = 0.0; - for (std::size_t i = 0; i < 4; ++i) - acc += std::pow(yHats[i] - s_trainData[i], 2); - std::cout << (acc / 8) << '\n'; - if (gen == 1'000'000) break; + for (std::size_t i = 0; i < 4; ++i) { + mat<1, 2> xs; + xs[0][0] = static_cast(i & 1); + xs[0][1] = static_cast(i > 1); - for (std::size_t i = 0; i < 4; ++i) - net.apply(static_cast(i & 1), static_cast(i > 1), yHats[i], s_trainData[i], EPSILON); + net.feed(xs); + double yHat = net.output[0][0]; + double error = yHat - s_trainData[i]; + + if ((gen % 100) == 0) { + std::cout << xs[0][0] << " ^ " << xs[0][1] << " = " << yHat << " (error = " << error << ")\n"; + } + + acc += error * error; + } + + double cost = acc / 8.0; + + if (gen % 100 == 0) { + std::cout << "Generation " << gen << ", cost = " << cost << '\n'; + } } return 0;