-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmlpnnets.h
61 lines (52 loc) · 1.88 KB
/
mlpnnets.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
/*
* mlpnnets.h
* Feed-forward Multi-Layer Perceptron Neural Networks.
*
* Andrei de A. Formiga, 2012-03-31
*
*/
#ifndef __MLPNNETS_H
#define __MLPNNETS_H
// macro to access weight ij in layer l
#define W(l, i, j) ( l->w[i * (l->prev->n_neurons+1) + j] )
// structure representing a layer in the neural net
typedef struct tagLayer
{
int n_neurons; // number of neurons
double *w; // input weights for the layer
struct tagLayer *prev; // previous layer
struct tagLayer *next; // next layer
double *a; // activations
double *y; // node outputs
} Layer;
// structure representing a neural network
typedef struct tagNetwork
{
int n_layers; // number of layers in network
Layer *input_layer; // the input layer
Layer *output_layer; // the output (last) layer
} Network;
// structure for a dataset
typedef struct tagDataSet
{
int n_cases; // number of cases
int input_size; // size of input in each case
int output_size; // size of output in each case
double **input; // inputs
double **output; // outputs
} DataSet;
// activation functions
double sigmoid(double t);
double dsigmoid(double t);
double threshold(double t);
// network functions
Network *create_network(int n_inputs);
Layer* add_layer(Network *nnet, int n_neurons);
void destroy_network(Network *nnet);
void print_network_structure(Network *nnet);
void initialize_weights(Network *nnet, unsigned int seed);
void forward_prop(Network *nnet, double (*activf)(double), double *input);
double batch_train(Network *nnet, DataSet *dset, double lrate, int epochs,
double (*actf)(double), double (*dactf)(double));
void allocate_dataset_arrays(DataSet *dset);
#endif /* __MLPNETS_H */