File size: 2,514 Bytes
00e6582
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
#ifndef NEURAL_NETWORK_H_INCLUDED
#define NEURAL_NETWORK_H_INCLUDED

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

// --------------------------------------------------------
// 1. Structures
// --------------------------------------------------------

typedef struct {
    int num_inputs;      // Inputs from the previous NNLayer
    int num_neurons;     // Neurons in this NNLayer
    double *weights;     // 1D array of size [num_neurons * num_inputs]
    double *biases;      // 1D array of size [num_neurons]
    double *outputs;     // 1D array of size [num_neurons]
    double *deltas;      // 1D array of size [num_neurons] for backprop
} NNLayer;

typedef struct {
    int num_inputs;      // Network input size
    int num_NNLayers;      // Total number of NNLayers (hidden + output)
    NNLayer *NNLayers;       // Array of NNLayers
} Network;

// --------------------------------------------------------
// 2. Math & Activations
// --------------------------------------------------------

// Generates a random double between -0.5 and 0.5
double random_weight();

// Sigmoid activation function
double sigmoid(double x);

// Derivative of sigmoid (assuming x is already activated)
double sigmoid_derivative(double x);

// --------------------------------------------------------
// 3. Initialization & Memory Management
// --------------------------------------------------------

// Initialize a single NNLayer
void init_NNLayer(NNLayer *NNLayer, int num_inputs, int num_neurons);

// Create the network
Network* create_network(int num_inputs, int num_hidden_NNLayers, int hidden_size, int num_outputs);

// Free memory
void free_network(Network *net);

// --------------------------------------------------------
// 4. Feedforward & Backpropagation
// --------------------------------------------------------

// Forward pass
double* feed_forward(Network *net, double *inputs);

// Train a single sample
void backpropagate(Network *net, double *inputs, double *targets, double learning_rate);

// --------------------------------------------------------
// 5. File Saving and Loading
// --------------------------------------------------------

void save_network(Network *net, const char *filename);

Network* load_network(const char *filename);

// --------------------------------------------------------
// 6. Example Usage (Airplane Steering Context)
// --------------------------------------------------------

int CreateNet();
#endif // NEURAL_NETWORK_H_INCLUDED