XCSF  1.4.7
XCSF learning classifier system
neural.h
Go to the documentation of this file.
1 /*
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  */
15 
24 #pragma once
25 
26 #include "utils.h"
27 #include <stdbool.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 
33 struct ArgsLayer;
34 struct Layer;
35 
39 struct Llist {
40  struct Layer *layer;
41  struct Llist *prev;
42  struct Llist *next;
43 };
44 
48 struct Net {
49  int n_layers;
50  int n_inputs;
51  int n_outputs;
52  double *output;
53  struct Llist *head;
54  struct Llist *tail;
55  bool train;
56 };
57 
58 bool
59 neural_mutate(const struct Net *net);
60 
61 char *
62 neural_json_export(const struct Net *net, const bool return_weights);
63 
64 void
65 neural_json_import(struct Net *net, const struct ArgsLayer *arg,
66  const cJSON *json);
67 
68 double
69 neural_output(const struct Net *net, const int IDX);
70 
71 double *
72 neural_outputs(const struct Net *net);
73 
74 double
75 neural_size(const struct Net *net);
76 
77 size_t
78 neural_load(struct Net *net, FILE *fp);
79 
80 size_t
81 neural_save(const struct Net *net, FILE *fp);
82 
83 void
84 neural_copy(struct Net *dest, const struct Net *src);
85 
86 void
87 neural_free(struct Net *net);
88 
89 void
90 neural_init(struct Net *net);
91 
92 void
93 neural_create(struct Net *net, struct ArgsLayer *arg);
94 
95 void
96 neural_insert(struct Net *net, struct Layer *l, const int pos);
97 
98 void
99 neural_remove(struct Net *net, const int pos);
100 
101 void
102 neural_push(struct Net *net, struct Layer *l);
103 
104 void
105 neural_pop(struct Net *net);
106 
107 void
108 neural_learn(const struct Net *net, const double *output, const double *input);
109 
110 void
111 neural_print(const struct Net *net, const bool print_weights);
112 
113 void
114 neural_propagate(struct Net *net, const double *input, const bool train);
115 
116 void
117 neural_rand(const struct Net *net);
118 
119 void
120 neural_resize(const struct Net *net);
double * neural_outputs(const struct Net *net)
Returns the outputs from the output layer of a neural network.
Definition: neural.c:384
void neural_push(struct Net *net, struct Layer *l)
Inserts a layer at the head of a neural network.
Definition: neural.c:187
void neural_learn(const struct Net *net, const double *output, const double *input)
Performs a gradient descent update on a neural network.
Definition: neural.c:328
bool neural_mutate(const struct Net *net)
Mutates a neural network.
Definition: neural.c:257
double neural_size(const struct Net *net)
Returns the total number of non-zero weights in a neural network.
Definition: neural.c:450
void neural_create(struct Net *net, struct ArgsLayer *arg)
Initialises and creates a new neural network from a parameter list.
Definition: neural.c:54
double neural_output(const struct Net *net, const int IDX)
Returns the output of a specified neuron in the output layer of a neural network.
Definition: neural.c:369
void neural_resize(const struct Net *net)
Resizes neural network layers as necessary.
Definition: neural.c:290
size_t neural_load(struct Net *net, FILE *fp)
Reads a neural network from a file.
Definition: neural.c:499
void neural_json_import(struct Net *net, const struct ArgsLayer *arg, const cJSON *json)
Creates a neural network from a cJSON object.
Definition: neural.c:434
void neural_free(struct Net *net)
Frees a neural network.
Definition: neural.c:224
void neural_remove(struct Net *net, const int pos)
Removes a layer from a neural network.
Definition: neural.c:140
void neural_print(const struct Net *net, const bool print_weights)
Prints a neural network.
Definition: neural.c:395
void neural_rand(const struct Net *net)
Randomises the layers within a neural network.
Definition: neural.c:242
void neural_copy(struct Net *dest, const struct Net *src)
Copies a neural network.
Definition: neural.c:208
void neural_init(struct Net *net)
Initialises an empty neural network.
Definition: neural.c:37
void neural_propagate(struct Net *net, const double *input, const bool train)
Forward propagates a neural network.
Definition: neural.c:310
char * neural_json_export(const struct Net *net, const bool return_weights)
Returns a json formatted string representation of a neural network.
Definition: neural.c:407
void neural_pop(struct Net *net)
Removes the layer at the head of a neural network.
Definition: neural.c:197
size_t neural_save(const struct Net *net, FILE *fp)
Writes a neural network to a file.
Definition: neural.c:478
void neural_insert(struct Net *net, struct Layer *l, const int pos)
Inserts a layer into a neural network.
Definition: neural.c:95
Parameters for initialising a neural network layer.
Neural network layer data structure.
Definition: neural_layer.h:73
Forward declaration of layer structure.
Definition: neural.h:39
struct Llist * prev
Pointer to the previous layer (forward)
Definition: neural.h:41
struct Layer * layer
Pointer to the layer data structure.
Definition: neural.h:40
struct Llist * next
Pointer to the next layer (backward)
Definition: neural.h:42
Neural network data structure.
Definition: neural.h:48
int n_layers
Number of layers (hidden + output)
Definition: neural.h:49
double * output
Pointer to the network output.
Definition: neural.h:52
struct Llist * tail
Pointer to the tail layer (first layer)
Definition: neural.h:54
struct Llist * head
Pointer to the head layer (output layer)
Definition: neural.h:53
bool train
Whether the network is in training mode.
Definition: neural.h:55
int n_outputs
Number of network outputs.
Definition: neural.h:51
int n_inputs
Number of network inputs.
Definition: neural.h:50
Utility functions for random number handling, etc.