ANN  0.1.1.5
A library containing multiple neural network models written in C
train.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
10 
11 #include "ANN/models/PCFNN/train.h"
12 
13 
14 ANN_INLINE void __fisher_yates_shuffle(size_t *array, size_t n)
15 {
16  for (size_t i = 0; i < n - 1; ++i)
17  {
18  size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
19  size_t t = array[j];
20  array[j] = array[i];
21  array[i] = t;
22  }
23 }
24 
25 
26 double *PCFNN_NETWORK_train(struct PCFNN_NETWORK *net, double **data, double **target,
27  size_t size, double validation_split,
28  int shuffle, unsigned long batch_size, size_t epochs, double eta, double alpha,
29  double(*f_cost)(double, double), double(*f_cost_de)(double, double)
30  , double *status)
31 {
32  if (net == NULL || data == NULL || target == NULL || size == 0 || validation_split < 0 || validation_split > 1)
33  return NULL;
34  if (validation_split == 0) {
35  if (batch_size == 0 || epochs == 0 || eta == 0 || f_cost_de == NULL)
36  return NULL;
37  } else {
38  if (f_cost == NULL)
39  return NULL;
40  }
41 
42  size_t validationsize = (size_t)floor(size * validation_split);
43  size_t trainingsize = size - validationsize;
44 
45  double __status; if (status == NULL) status = &__status;
46  *status = 0;
47 
48  size_t order[size];
49  for(size_t i = 0; i < size; ++i) order[i] = i;
51  if (validation_split != 1) {
52  double stepstatus = (1/(double)(epochs * trainingsize)) * 100;
54 
55  for(size_t e = 0; e < epochs; ++e)
56  {
57  if (shuffle)
58  __fisher_yates_shuffle(order, trainingsize);
59  for(size_t i = 0; i < trainingsize; i+=batch_size)
60  {
61  for(size_t j = 0; j < batch_size && i+j < trainingsize; ++j, (*status) += stepstatus)
62  {
63  PCFNN_NETWORK_feedforward(net, data[order[i+j]]);
64  PCFNN_NETWORK_backprop(net, target[order[i+j]], eta, alpha, f_cost_de);
65  }
68  }
69  }
70 
72  }
73  *status = 100.0;
74 
75  if (validation_split == 0)
76  return NULL;
77  double *err = calloc(net->outputl->size, sizeof(double));
78  for(size_t i = trainingsize; i < size; ++i)
79  {
80  PCFNN_NETWORK_feedforward(net, data[order[i]]);
81  for(size_t j = 0; j < net->outputl->size; ++j)
82  err[j] += f_cost(net->outputl->neurons[j]->output, target[order[i]][j]);
83  }
84  for(size_t i = 0; i < net->outputl->size; ++i)
85  err[i] /= (double)(size - trainingsize);
86  return err;
87 }
PCFNN_LAYER.
void PCFNN_NETWORK_feedforward(struct PCFNN_NETWORK *net, double *inputs)
Feedforward the PCFNN_NETWORK net.
Definition: feedforward.c:54
PCFNN_TRAIN.
void PCFNN_NETWORK_apply_delta(struct PCFNN_NETWORK *net)
Apply all delta calculated by PCFNN_NETWORK_backprop on the network net.
Definition: backprop.c:85
void PCFNN_NETWORK_init_batch(struct PCFNN_NETWORK *net)
Initialize batch for the network net.
Definition: batch.c:9
size_t size
Definition: layer.h:55
struct PCFNN_LAYER * outputl
Definition: network.h:30
PCFNN_NEURON.
double output
Definition: neuron.h:38
PCFNN_BACKPROP.
void PCFNN_NETWORK_clear_batch(struct PCFNN_NETWORK *net)
Partially clear batch data of the network net.
Definition: batch.c:38
double * PCFNN_NETWORK_train(struct PCFNN_NETWORK *net, double **data, double **target, size_t size, double validation_split, int shuffle, unsigned long batch_size, size_t epochs, double eta, double alpha, double(*f_cost)(double, double), double(*f_cost_de)(double, double), double *status)
Train the network net.
Definition: train.c:26
PCFNN_FEEDFORWARD.
PCFNN_NETWORK.
struct PCFNN_NEURON ** neurons
Definition: layer.h:56
ANN_INLINE void __fisher_yates_shuffle(size_t *array, size_t n)
Definition: train.c:14
void PCFNN_NETWORK_free_batch(struct PCFNN_NETWORK *net)
Free all memory allocation for the batch of the network net.
Definition: batch.c:23
PCFNN_BATCH.
Network unit.
Definition: network.h:27
size_t size
Definition: neuron.h:36
void PCFNN_NETWORK_backprop(struct PCFNN_NETWORK *net, double *target, double eta, double alpha, double(*f_cost)(double, double))
Run the Backpropagation Algorithm on the network net.
Definition: backprop.c:64