-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnnet.c
96 lines (84 loc) · 1.98 KB
/
nnet.c
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// vim: tabstop=4:softtabstop=4:shiftwidth=4:expandtab:smarttab
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "config.h"
/**
* Structure which contains all information
* pertaining to every neuron in every layer
* and their connection weights
*/
typedef struct
{
/**
* Number of layers
*/
int nls;
/**
* Number of neurons in each layer
*/
int *nns;
/**
* Neuron activation for each neuron
* in each layer; intended to indicate
* "states"
*/
double **sts;
/**
* Neron biases for each neuron in each
* layer; intended to indicate "biases"
* via "bss"
*/
double **bss;
/**
* Weight connecting each adjacent layer
* of neurons to the next; intended to
* indicate "weights"
*/
double **wts;
} nnet_t;
/**
* Initializes all data structures required
* to run an artificial neural network of the
* given input size
* @param obj Neural network object to initialize
* @param
*/
int nnet_init( nnet_t *obj, int nls, int *nns )
{
int i;
/* Make sure good values encountered */
if( nls < 0 )
return -1;
for(i=0;i<nls;i++)
{
if( nns[i] < 0 )
return -2;
}
/* Now allocate all layers and neurons */
obj->nls = nls;
obj->nns = (int*) malloc( nls * sizeof(int) );
if( obj->nns == NULL )
return -3; /* Failure of malloc() */
for(i=0;i<nls;i++)
{
obj->nns[i] = nns[i];
obj->bss = (double**) malloc( nls * sizeof(double*) );
if( obj->bss == NULL )
return -4;
obj->sts[i] = (double*) malloc( nns[i] * sizeof(double) );
// Check malloc() worked
obj->bss[i] = (double*) malloc( nns[i] * sizeof(double) );
// Check malloc() worked
}
return 0;
}
/**
* Clean up data and functions contained in
* the neural network object
* @param obj Neural network object of interest
*/
int nnet_free( nnet_t *obj )
{
}