pulsatrix
Loading...
Searching...
No Matches
fixed_topology_xor_network.hpp
Go to the documentation of this file.
1
15#pragma once
16
17#include <array>
18#include <cmath>
19#include <stdexcept>
20#include <vector>
21
22namespace pulsatrix {
23
26inline constexpr size_t kFixedTopologyXORNumParams = 9;
27
28namespace detail {
29inline double Sigmoid(double x) { return 1.0 / (1.0 + std::exp(-x)); }
30} // namespace detail
31
39inline double FixedTopologyXORForward(const std::vector<double>& theta, const std::array<double, 2>& inputs) {
40 if (theta.size() != kFixedTopologyXORNumParams) {
41 throw std::invalid_argument("FixedTopologyXORForward: theta must have exactly kFixedTopologyXORNumParams entries");
42 }
43 double h0 = detail::Sigmoid(theta[0] * inputs[0] + theta[1] * inputs[1] + theta[2]);
44 double h1 = detail::Sigmoid(theta[3] * inputs[0] + theta[4] * inputs[1] + theta[5]);
45 return detail::Sigmoid(theta[6] * h0 + theta[7] * h1 + theta[8]);
46}
47
54inline double FixedTopologyXORFitness(const std::vector<double>& theta) {
55 static const std::array<std::array<double, 2>, 4> kInputs{
56 {{0.0, 0.0}, {0.0, 1.0}, {1.0, 0.0}, {1.0, 1.0}}};
57 static const std::array<double, 4> kExpected{0.0, 1.0, 1.0, 0.0};
58
59 double sum_squared_error = 0.0;
60 for (size_t i = 0; i < kInputs.size(); ++i) {
61 double actual = FixedTopologyXORForward(theta, kInputs[i]);
62 double error = kExpected[i] - actual;
63 sum_squared_error += error * error;
64 }
65 return 4.0 - sum_squared_error;
66}
67
68} // namespace pulsatrix
double Sigmoid(double x)
Definition fixed_topology_xor_network.hpp:29
Definition acquisition_functions.hpp:16
double FixedTopologyXORFitness(const std::vector< double > &theta)
Scores theta against all four XOR patterns as 4.0 minus the sum of squared errors – identical convent...
Definition fixed_topology_xor_network.hpp:54
double FixedTopologyXORForward(const std::vector< double > &theta, const std::array< double, 2 > &inputs)
Forward pass: theta layout is [w1_00, w1_01, b1_0, w1_10, w1_11, b1_1, w2_0, w2_1,...
Definition fixed_topology_xor_network.hpp:39
constexpr size_t kFixedTopologyXORNumParams
Total flat-parameter count: 2*2 (input->hidden weights) + 2 (hidden biases) + 2 (hidden->output weigh...
Definition fixed_topology_xor_network.hpp:26
@ Sigmoid
1 / (1 + exp(-x))