refactor: use matrices
This commit is contained in:
+79
-53
@@ -1,3 +1,5 @@
|
||||
#include "mat.hpp"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <iostream>
|
||||
@@ -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<double>(i & 1);
|
||||
auto x2 = static_cast<double>(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<double>(i & 1);
|
||||
xs[0][1] = static_cast<double>(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<double>(i & 1);
|
||||
xs[0][1] = static_cast<double>(i > 1);
|
||||
|
||||
for (std::size_t i = 0; i < 4; ++i)
|
||||
net.apply(static_cast<double>(i & 1), static_cast<double>(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;
|
||||
|
||||
Reference in New Issue
Block a user