ANN  0.1.1.5
A library containing multiple neural network models written in C
neuron.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 
4 
5 
6 struct PCFNN_NEURON *PCFNN_NEURON_new(size_t size, double(*f_init)(), double(*f_act)(double), double(*f_act_de)(double))
7 {
8  if (f_init == NULL) return NULL;
9  struct PCFNN_NEURON *n = calloc(1, sizeof(struct PCFNN_NEURON));
10  if (n == NULL) return NULL;
11  n->size = size;
12  n->bias = f_init();
14  n->f_init = f_init;
15  n->f_act = f_act;
16  n->f_act_de = f_act_de;
17  return n;
18 }
19 
20 
22 {
23  if (n != NULL)
24  n->output = n->activation = n->delta = 0;
25 }
26 
27 
29 {
30  if (n != NULL)
31  {
32  if (n->inputs != NULL)
33  free(n->inputs);
34  if (n->weights != NULL)
35  free(n->weights);
36  free(n);
37  }
38 }
39 
40 
42 {
43  if (n == NULL || inputs <= 0) return;
44  n->size += inputs;
45 }
46 
47 
49 {
50  if (n == NULL) return;
51  n->weights = malloc(sizeof(double) * n->size);
52  if (n->weights == NULL) return;
53  n->inputs = calloc(n->size, sizeof(struct PCFNN_NEURON*));
54  if (n->inputs == NULL) { free(n->weights); n->weights = NULL; return; }
55  for (size_t i = 0; i < n->size; ++i)
56  n->weights[i] = n->f_init();
57 }
58 
59 
61 {
62  if (n == NULL) return 0;
63  size_t usage = sizeof(struct PCFNN_NEURON);
64  if (n->weights != NULL) usage += sizeof(double) * n->size;
65  if (n->inputs != NULL) usage += sizeof(struct PCFNN_NEURON*) * n->size;
66  return usage;
67 }
68 
69 
71 {
72  if (n == NULL) return NULL;
73  return PCFNN_NEURON_new(n->size, n->f_init, n->f_act, n->f_act_de);
74 }
75 
76 
78 {
79  struct PCFNN_NEURON *b = PCFNN_NEURON_clone(n);
80  if (b == NULL) return NULL;
81  if (n->weights != NULL && n->inputs != NULL)
82  {
84  for(size_t i = 0; i < b->size; ++i)
85  {
86  b->weights[i] = n->weights[i];
87  b->inputs[i] = n->inputs[i];
88  }
89  }
90  b->bias = n->bias; b->output = n->output; b->activation = n->activation; b->delta = n->delta;
91  b->wdelta = b->lastdw = NULL; b->bdelta = 0;
92  b->state = n->state;
93  return b;
94 }
95 
96 
98 {
99  if (n == NULL) return;
100  n->state = state;
101 }
102 
103 void PCFNN_NEURON_summary(struct PCFNN_NEURON *n, size_t param[2])
104 {
105  if (n == NULL || param == NULL) return;
106  if (n->state == PCFNN_NEURON_UNLOCK)
107  param[0] += n->size + 1;
108  else
109  param[1] += n->size + 1;
110 }
double delta
Definition: neuron.h:40
Neuron unit.
Definition: neuron.h:35
struct PCFNN_NEURON * PCFNN_NEURON_new(size_t size, double(*f_init)(), double(*f_act)(double), double(*f_act_de)(double))
Initialize a PCFNN_NEURON.
Definition: neuron.c:6
double bdelta
Definition: neuron.h:40
void PCFNN_NEURON_summary(struct PCFNN_NEURON *n, size_t param[2])
Write on param the number of unlocked parameters and locked parameters.
Definition: neuron.c:103
double activation
Definition: neuron.h:40
void PCFNN_NEURON_addinputs(struct PCFNN_NEURON *n, size_t inputs)
Increase the input size of the PCFNN_NEURON.
Definition: neuron.c:41
void PCFNN_NEURON_build(struct PCFNN_NEURON *n)
Initialize all internal data of a PCFNN_NEURON.
Definition: neuron.c:48
void PCFNN_NEURON_free(struct PCFNN_NEURON *n)
Free all memory allocation of an PCFNN_NEURON.
Definition: neuron.c:28
void PCFNN_NEURON_clear(struct PCFNN_NEURON *n)
Clear a PCFNN_NEURON.
Definition: neuron.c:21
struct PCFNN_NEURON ** inputs
Definition: neuron.h:42
PCFNN_NEURON.
double output
Definition: neuron.h:38
double(* f_act_de)(double)
Definition: neuron.h:45
void PCFNN_NEURON_set_state_lock(struct PCFNN_NEURON *n, enum PCFNN_NEURON_LOCK_STATE state)
Set lock state of the neuron n.
Definition: neuron.c:97
double * lastdw
Definition: neuron.h:41
double(* f_init)()
Definition: neuron.h:43
double bias
Definition: neuron.h:38
double * weights
Definition: neuron.h:37
struct PCFNN_NEURON * PCFNN_NEURON_clone(struct PCFNN_NEURON *n)
Copy the input size, initialisation function and activation function from n and create a new PCFNN_NE...
Definition: neuron.c:70
double * wdelta
Definition: neuron.h:41
size_t PCFNN_NEURON_get_ram_usage(struct PCFNN_NEURON *n)
Give the number of bytes used by the PCFNN_NEURON n.
Definition: neuron.c:60
double(* f_act)(double)
Definition: neuron.h:44
struct PCFNN_NEURON * PCFNN_NEURON_clone_all(struct PCFNN_NEURON *n)
Copy all data from n and create a new PCFNN_NEURON.
Definition: neuron.c:77
enum PCFNN_NEURON_LOCK_STATE state
Definition: neuron.h:46
size_t size
Definition: neuron.h:36
PCFNN_NEURON_LOCK_STATE
Lock state of a neuron.
Definition: neuron.h:22