~ultradvorka/+junk/mindforger

« back to all changes in this revision

Viewing changes to lib/src/mind/ai/nn/genann.h

  • Committer: Martin Dvorak (Dvorka)
  • Date: 2018-05-15 07:08:52 UTC
  • Revision ID: martin.dvorak@mindforger.com-20180515070852-qt83zifaqrbj0a2f
Update for mindforger_0.7.6 at 2018-05-15--09-08-40.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * GENANN - Minimal C Artificial Neural Network
 
3
 *
 
4
 * Copyright (c) 2015, 2016 Lewis Van Winkle
 
5
 *
 
6
 * http://CodePlea.com
 
7
 *
 
8
 * This software is provided 'as-is', without any express or implied
 
9
 * warranty. In no event will the authors be held liable for any damages
 
10
 * arising from the use of this software.
 
11
 *
 
12
 * Permission is granted to anyone to use this software for any purpose,
 
13
 * including commercial applications, and to alter it and redistribute it
 
14
 * freely, subject to the following restrictions:
 
15
 *
 
16
 * 1. The origin of this software must not be misrepresented; you must not
 
17
 *    claim that you wrote the original software. If you use this software
 
18
 *    in a product, an acknowledgement in the product documentation would be
 
19
 *    appreciated but is not required.
 
20
 * 2. Altered source versions must be plainly marked as such, and must not be
 
21
 *    misrepresented as being the original software.
 
22
 * 3. This notice may not be removed or altered from any source distribution.
 
23
 *
 
24
 */
 
25
 
 
26
 
 
27
#ifndef __GENANN_H__
 
28
#define __GENANN_H__
 
29
 
 
30
#include <stdio.h>
 
31
 
 
32
#ifdef __cplusplus
 
33
extern "C" {
 
34
#endif
 
35
 
 
36
#ifndef GENANN_RANDOM
 
37
/* We use the following for uniform random numbers between 0 and 1.
 
38
 * If you have a better function, redefine this macro. */
 
39
#define GENANN_RANDOM() (((double)rand())/RAND_MAX)
 
40
#endif
 
41
 
 
42
 
 
43
typedef double (*genann_actfun)(double a);
 
44
 
 
45
 
 
46
typedef struct genann {
 
47
    /* How many inputs, outputs, and hidden neurons. */
 
48
    int inputs, hidden_layers, hidden, outputs;
 
49
 
 
50
    /* Which activation function to use for hidden neurons. Default: gennann_act_sigmoid_cached*/
 
51
    genann_actfun activation_hidden;
 
52
 
 
53
    /* Which activation function to use for output. Default: gennann_act_sigmoid_cached*/
 
54
    genann_actfun activation_output;
 
55
 
 
56
    /* Total number of weights, and size of weights buffer. */
 
57
    int total_weights;
 
58
 
 
59
    /* Total number of neurons + inputs and size of output buffer. */
 
60
    int total_neurons;
 
61
 
 
62
    /* All weights (total_weights long). */
 
63
    double *weight;
 
64
 
 
65
    /* Stores input array and output of each neuron (total_neurons long). */
 
66
    double *output;
 
67
 
 
68
    /* Stores delta of each hidden and output neuron (total_neurons - inputs long). */
 
69
    double *delta;
 
70
 
 
71
} genann;
 
72
 
 
73
 
 
74
 
 
75
/* Creates and returns a new ann. */
 
76
genann *genann_init(int inputs, int hidden_layers, int hidden, int outputs);
 
77
 
 
78
/* Creates ANN from file saved with genann_write. */
 
79
genann *genann_read(FILE *in);
 
80
 
 
81
/* Sets weights randomly. Called by init. */
 
82
void genann_randomize(genann *ann);
 
83
 
 
84
/* Returns a new copy of ann. */
 
85
genann *genann_copy(genann const *ann);
 
86
 
 
87
/* Frees the memory used by an ann. */
 
88
void genann_free(genann *ann);
 
89
 
 
90
/* Runs the feedforward algorithm to calculate the ann's output. */
 
91
double const *genann_run(genann const *ann, double const *inputs);
 
92
 
 
93
/* Does a single backprop update. */
 
94
void genann_train(genann const *ann, double const *inputs, double const *desired_outputs, double learning_rate);
 
95
 
 
96
/* Saves the ann. */
 
97
void genann_write(genann const *ann, FILE *out);
 
98
 
 
99
 
 
100
double genann_act_sigmoid(double a);
 
101
double genann_act_sigmoid_cached(double a);
 
102
double genann_act_threshold(double a);
 
103
double genann_act_linear(double a);
 
104
 
 
105
 
 
106
#ifdef __cplusplus
 
107
}
 
108
#endif
 
109
 
 
110
#endif /*__GENANN_H__*/