-
Notifications
You must be signed in to change notification settings - Fork 0
/
NeuralNetwork.h
69 lines (57 loc) · 1.48 KB
/
NeuralNetwork.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <stdlib.h>
#define MAX_NODES_PER_LAYER 50
#define MAX_NUM_LAYERS 20
typedef unsigned int u32;
typedef int s32;
typedef long int s64;
typedef float f32;
typedef double f64;
typedef u32 bool;
typedef f64 Weight;
typedef f64 NodeValue;
typedef struct {
char* m_Memory;
u32 m_UsedSize;
u32 m_MaxSize;
} MemoryBuffer;
typedef struct {
Weight m_Weight;
Weight m_Delta;
} Synapse;
typedef struct {
// Witholds synapses that connect to the previous layer
u32 m_SynapseCount;
Synapse* m_Synapses;
NodeValue m_Value;
NodeValue m_PreActivatedValue;
} Node;
typedef struct {
u32 m_Size;
Node* m_Nodes;
} NodeLayer;
typedef struct {
f64 m_Momentum;
f64 m_LearningRate;
u32 m_NumLayers;
MemoryBuffer m_Memory;
NodeLayer* m_NodeLayers;
} Network;
typedef struct {
u32 m_Size;
NodeValue m_Values[MAX_NODES_PER_LAYER]; // Alloc max num of node values! Just so we can stack-allocate results & inputs
} NodeBuffer;
typedef struct {
u32 m_Size;
u32 m_LayerSizes[MAX_NUM_LAYERS];
f64 m_LearningRate;
f64 m_Momentum;
} NetworkSettings;
typedef NodeBuffer Input;
typedef NodeBuffer Result;
Network* LNN_CreateNetwork(NetworkSettings* settings);
void LNN_FreeNetwork(Network* network);
Network* LNN_AllocateNetwork(u32 numLayers, u32* layerSizes);
Result LNN_ForwardPropagate(Network* network, Input input);
f64 LNN_Learn(Network* network, Input input, Result expectedResult);
void LNN_SetSynapseWeight(Network* network, u32 layer, u32 node, u32 synapse, Weight weight);