feat: implement a simple XOR gate network
This commit is contained in:
Executable
+67
@@ -0,0 +1,67 @@
|
||||
BasedOnStyle: LLVM
|
||||
|
||||
IndentWidth: 4
|
||||
TabWidth: 4
|
||||
UseTab: Never
|
||||
ContinuationIndentWidth: 4
|
||||
|
||||
ColumnLimit: 120
|
||||
|
||||
BreakBeforeBraces: Attach
|
||||
BraceWrapping:
|
||||
BeforeCatch: true
|
||||
BeforeElse: false
|
||||
BeforeWhile: false
|
||||
BeforeLambdaBody: false
|
||||
|
||||
AllowShortFunctionsOnASingleLine: Inline
|
||||
AllowShortBlocksOnASingleLine: Empty
|
||||
AllowShortCaseLabelsOnASingleLine: true
|
||||
AllowShortEnumsOnASingleLine: false
|
||||
AllowShortIfStatementsOnASingleLine: true
|
||||
AllowShortLoopsOnASingleLine: false
|
||||
AllowShortCompoundRequirementOnASingleLine: true
|
||||
|
||||
NamespaceIndentation: None
|
||||
AccessModifierOffset: -4
|
||||
EmptyLineBeforeAccessModifier: Never
|
||||
|
||||
SpaceBeforeParens: ControlStatementsExceptControlMacros
|
||||
SpaceBeforeInheritanceColon: true
|
||||
SpacesBeforeTrailingComments: 1
|
||||
|
||||
AlignTrailingComments: true
|
||||
|
||||
AlignConsecutiveAssignments:
|
||||
Enabled: true
|
||||
AlignCompound: true
|
||||
PadOperators: false
|
||||
|
||||
AlignConsecutiveDeclarations: Consecutive
|
||||
AlignAfterOpenBracket: DontAlign
|
||||
|
||||
ConstructorInitializerIndentWidth: 2
|
||||
PackConstructorInitializers: NextLineOnly
|
||||
BreakConstructorInitializers: BeforeColon
|
||||
|
||||
BinPackParameters: false
|
||||
BinPackArguments: false
|
||||
AllowAllArgumentsOnNextLine: false
|
||||
|
||||
AlwaysBreakTemplateDeclarations: Yes
|
||||
BreakAfterReturnType: None
|
||||
|
||||
Cpp11BracedListStyle: false
|
||||
BreakArrays: false
|
||||
|
||||
IncludeBlocks: Regroup
|
||||
SortIncludes: true
|
||||
|
||||
ReflowComments: Always
|
||||
KeepEmptyLinesAtTheStartOfBlocks: true
|
||||
|
||||
DerivePointerAlignment: false
|
||||
PointerAlignment: Left
|
||||
AllowAllParametersOfDeclarationOnNextLine: false
|
||||
|
||||
PenaltyReturnTypeOnItsOwnLine: 1000
|
||||
Executable
+105
@@ -0,0 +1,105 @@
|
||||
---
|
||||
Checks: >
|
||||
-*,
|
||||
|
||||
bugprone-*,
|
||||
performance-*,
|
||||
readability-*,
|
||||
portability-*,
|
||||
cppcoreguidelines-*,
|
||||
|
||||
-bugprone-assignment-in-if-condition,
|
||||
-bugprone-easily-swappable-parameters,
|
||||
-bugprone-multi-level-implicit-pointer-conversion,
|
||||
-bugprone-unintended-char-ostream-output,
|
||||
-readability-function-cognitive-complexity,
|
||||
-readability-magic-numbers,
|
||||
-readability-redundant-access-specifiers,
|
||||
-readability-use-anyofallof,
|
||||
-readability-named-parameter,
|
||||
-readability-convert-member-functions-to-static,
|
||||
-performance-enum-size,
|
||||
-cppcoreguidelines-avoid-magic-numbers,
|
||||
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
|
||||
-cppcoreguidelines-pro-type-union-access,
|
||||
-cppcoreguidelines-pro-type-reinterpret-cast,
|
||||
-cppcoreguidelines-avoid-do-while,
|
||||
-cppcoreguidelines-avoid-non-const-global-variables,
|
||||
-cppcoreguidelines-avoid-c-arrays,
|
||||
-cppcoreguidelines-pro-bounds-constant-array-index,
|
||||
-cppcoreguidelines-macro-usage,
|
||||
-cppcoreguidelines-owning-memory,
|
||||
-cppcoreguidelines-non-private-member-variables-in-classes,
|
||||
-cppcoreguidelines-pro-bounds-avoid-unchecked-container-access,
|
||||
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
|
||||
-cppcoreguidelines-use-enum-class,
|
||||
-bugprone-forward-declaration-namespace,
|
||||
-bugprone-tagged-union-member-count,
|
||||
-bugprone-unchecked-optional-access
|
||||
|
||||
WarningsAsErrors: "*"
|
||||
|
||||
HeaderFilterRegex: ".*"
|
||||
|
||||
CheckOptions:
|
||||
- key: readability-braces-around-statements.ShortStatementLines
|
||||
value: 3
|
||||
|
||||
- key: readability-identifier-length.MinimumParameterNameLength
|
||||
value: 2
|
||||
|
||||
- key: readability-identifier-length.MinimumVariableNameLength
|
||||
value: 2
|
||||
|
||||
- key: cppcoreguidelines-rvalue-reference-param-not-moved.AllowPartialMove
|
||||
value: true
|
||||
|
||||
# Namespaces
|
||||
- key: readability-identifier-naming.NamespaceCase
|
||||
value: lower_case
|
||||
|
||||
# Types
|
||||
- key: readability-identifier-naming.StructCase
|
||||
value: lower_case
|
||||
- key: readability-identifier-naming.ClassCase
|
||||
value: lower_case
|
||||
- key: readability-identifier-naming.EnumCase
|
||||
value: lower_case
|
||||
# - key: readability-identifier-naming.AbstractClassPrefix
|
||||
# value: I
|
||||
|
||||
# Functions
|
||||
- key: readability-identifier-naming.FunctionCase
|
||||
value: lower_case
|
||||
|
||||
# Variables
|
||||
- key: readability-identifier-naming.VariableCase
|
||||
value: camelBack
|
||||
|
||||
# Public members
|
||||
- key: readability-identifier-naming.PublicMemberCase
|
||||
value: camelBack
|
||||
|
||||
# Static members
|
||||
- key: readability-identifier-naming.StaticVariableCase
|
||||
value: camelBack
|
||||
- key: readability-identifier-naming.StaticVariablePrefix
|
||||
value: s_
|
||||
- key: readability-identifier-naming.ClassMemberCase
|
||||
value: camelBack
|
||||
- key: readability-identifier-naming.ClassMemberPrefix
|
||||
value: s_
|
||||
|
||||
# Static constexpr
|
||||
- key: readability-identifier-naming.StaticConstexprVariableCase
|
||||
value: aNy_CasE
|
||||
|
||||
# Constants
|
||||
- key: readability-identifier-naming.ConstantCase
|
||||
value: aNy_CasE
|
||||
- key: readability-identifier-naming.StaticConstantCase
|
||||
value: aNy_CasE
|
||||
- key: readability-identifier-naming.EnumConstantCase
|
||||
value: CamelCase
|
||||
- key: readability-identifier-naming.ScopedEnumConstantCase
|
||||
value: CamelCase
|
||||
@@ -0,0 +1,25 @@
|
||||
CompileFlags:
|
||||
Add: [-std=c++17, -Wall, -Werror, -Wpedantic]
|
||||
|
||||
InlayHints:
|
||||
BlockEnd: true
|
||||
Designators: false
|
||||
Enabled: true
|
||||
ParameterNames: true
|
||||
DeducedTypes: true
|
||||
TypeNameLimit: 24
|
||||
|
||||
Completion:
|
||||
HeaderInsertion: IWYU
|
||||
|
||||
Hover:
|
||||
ShowAKA: true
|
||||
|
||||
If:
|
||||
PathMatch: (./.*\.cpp|./.*\.hpp)
|
||||
Diagnostics:
|
||||
UnusedIncludes: Strict
|
||||
MissingIncludes: Strict
|
||||
|
||||
Documentation:
|
||||
CommentFormat: Doxygen
|
||||
@@ -0,0 +1 @@
|
||||
build/
|
||||
@@ -0,0 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
|
||||
project(NeuralNetworks CXX)
|
||||
|
||||
file(GLOB_RECURSE SOURCES src/**.cpp include/**.hpp)
|
||||
add_executable(nn ${SOURCES})
|
||||
target_include_directories(nn PRIVATE include/)
|
||||
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 2.7 MiB |
@@ -0,0 +1,93 @@
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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() };
|
||||
|
||||
double feed(double x1, double x2) const { return sigmoid(z21(x1, x2)); }
|
||||
|
||||
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;
|
||||
|
||||
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]; }
|
||||
};
|
||||
|
||||
int main(void) {
|
||||
static constexpr double s_trainData[4] = {
|
||||
0, // 0 ^ 0
|
||||
1, // 1 ^ 0
|
||||
1, // 0 ^ 1
|
||||
0, // 1 ^ 1
|
||||
};
|
||||
|
||||
network net;
|
||||
|
||||
static constexpr double EPSILON = 1.0;
|
||||
|
||||
std::size_t gen = 0;
|
||||
while (true) {
|
||||
std::cout << "--- Generation " << ++gen << '\n';
|
||||
double yHats[4];
|
||||
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";
|
||||
}
|
||||
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)
|
||||
net.apply(static_cast<double>(i & 1), static_cast<double>(i > 1), yHats[i], s_trainData[i], EPSILON);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user