1
0
mirror of https://github.com/lxsang/antd-lua-plugin synced 2025-07-27 11:20:04 +02:00

mimgrating from another repo

This commit is contained in:
Xuan Sang LE
2018-09-19 15:08:49 +02:00
parent 91320521e8
commit 38bd13b46b
600 changed files with 362490 additions and 1 deletions

View File

@ -0,0 +1,10 @@
########### install files ###############
IF(NOT OPENMP_FOUND OR DISABLE_PARALLEL_FANN)
SET(PARALLEL_INCLUDES "")
ELSE(NOT OPENMP_FOUND OR DISABLE_PARALLEL_FANN)
SET(PARALLEL_INCLUDES parallel_fann.h parallel_fann.hpp)
ENDIF(NOT OPENMP_FOUND OR DISABLE_PARALLEL_FANN)
install (FILES fann.h doublefann.h fann_internal.h floatfann.h fann_data.h fixedfann.h fann_activation.h fann_cascade.h fann_error.h fann_train.h fann_io.h fann_cpp.h fann_data_cpp.h fann_training_data_cpp.h ${PARALLEL_INCLUDES} DESTINATION ${INCLUDE_INSTALL_DIR})

View File

@ -0,0 +1,8 @@
/* Name of package */
/* #undef PACKAGE */
/* Version number of package */
#define VERSION "2.2.0"
/* Define for the x86_64 CPU famyly */
/* #undef X86_64 */

View File

@ -0,0 +1,33 @@
/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2016 Steffen Nissen (steffen.fann@gmail.com)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __doublefann_h__
#define __doublefann_h__
typedef double fann_type;
#undef DOUBLEFANN
#define DOUBLEFANN
#define FANNPRINTF "%.20e"
#define FANNSCANF "%le"
#define FANN_INCLUDE
#include "fann.h"
#endif

View File

@ -0,0 +1,669 @@
/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2016 Steffen Nissen (steffen.fann@gmail.com)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* This file defines the user interface to the fann library.
It is included from fixedfann.h, floatfann.h and doublefann.h and should
NOT be included directly. If included directly it will react as if
floatfann.h was included.
*/
/* Section: FANN Creation/Execution
The FANN library is designed to be very easy to use.
A feedforward ann can be created by a simple <fann_create_standard> function, while
other ANNs can be created just as easily. The ANNs can be trained by <fann_train_on_file>
and executed by <fann_run>.
All of this can be done without much knowledge of the internals of ANNs, although the ANNs created will
still be powerful and effective. If you have more knowledge about ANNs, and desire more control, almost
every part of the ANNs can be parametrized to create specialized and highly optimal ANNs.
*/
/* Group: Creation, Destruction & Execution */
#ifndef FANN_INCLUDE
/* just to allow for inclusion of fann.h in normal stuations where only floats are needed */
#ifdef FIXEDFANN
#include "fixedfann.h"
#else
#include "floatfann.h"
#endif /* FIXEDFANN */
#else
/* COMPAT_TIME REPLACEMENT */
#ifndef _WIN32
#include <sys/time.h>
#else /* _WIN32 */
#if !defined(_MSC_EXTENSIONS) && !defined(_INC_WINDOWS)
extern unsigned long __stdcall GetTickCount(void);
#else /* _MSC_EXTENSIONS */
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif /* _MSC_EXTENSIONS */
#endif /* _WIN32 */
#ifndef __fann_h__
#define __fann_h__
#ifdef __cplusplus
extern "C"
{
#ifndef __cplusplus
} /* to fool automatic indention engines */
#endif
#endif /* __cplusplus */
#ifndef NULL
#define NULL 0
#endif /* NULL */
/* ----- Macros used to define DLL external entrypoints ----- */
/*
DLL Export, import and calling convention for Windows.
Only defined for Microsoft VC++ FANN_EXTERNAL indicates
that a function will be exported/imported from a dll
FANN_API ensures that the DLL calling convention
will be used for a function regardless of the calling convention
used when compiling.
For a function to be exported from a DLL its prototype and
declaration must be like this:
FANN_EXTERNAL void FANN_API function(char *argument)
The following ifdef block is a way of creating macros which
make exporting from a DLL simple. All files within a DLL are
compiled with the FANN_DLL_EXPORTS symbol defined on the
command line. This symbol should not be defined on any project
that uses this DLL. This way any other project whose source
files include this file see FANN_EXTERNAL functions as being imported
from a DLL, whereas a DLL sees symbols defined with this
macro as being exported which makes calls more efficient.
The __stdcall calling convention is used for functions in a
windows DLL.
The callback functions for fann_set_callback must be declared as FANN_API
so the DLL and the application program both use the same
calling convention.
*/
/*
The following sets the default for MSVC++ 2003 or later to use
the fann dll's. To use a lib or fixedfann.c, floatfann.c or doublefann.c
with those compilers FANN_NO_DLL has to be defined before
including the fann headers.
The default for previous MSVC compilers such as VC++ 6 is not
to use dll's. To use dll's FANN_USE_DLL has to be defined before
including the fann headers.
*/
#if (_MSC_VER > 1300)
#ifndef FANN_NO_DLL
#define FANN_USE_DLL
#endif /* FANN_USE_LIB */
#endif /* _MSC_VER */
#if defined(_MSC_VER) && (defined(FANN_USE_DLL) || defined(FANN_DLL_EXPORTS))
#ifdef FANN_DLL_EXPORTS
#define FANN_EXTERNAL __declspec(dllexport)
#else /* */
#define FANN_EXTERNAL __declspec(dllimport)
#endif /* FANN_DLL_EXPORTS*/
#define FANN_API __stdcall
#else /* */
#define FANN_EXTERNAL
#define FANN_API
#endif /* _MSC_VER */
/* ----- End of macros used to define DLL external entrypoints ----- */
#include "fann_error.h"
#include "fann_activation.h"
#include "fann_data.h"
#include "fann_internal.h"
#include "fann_train.h"
#include "fann_cascade.h"
#include "fann_io.h"
/* Function: fann_create_standard
Creates a standard fully connected backpropagation neural network.
There will be a bias neuron in each layer (except the output layer),
and this bias neuron will be connected to all neurons in the next layer.
When running the network, the bias nodes always emits 1.
To destroy a <struct fann> use the <fann_destroy> function.
Parameters:
num_layers - The total number of layers including the input and the output layer.
... - Integer values determining the number of neurons in each layer starting with the
input layer and ending with the output layer.
Returns:
A pointer to the newly created <struct fann>.
Example:
> // Creating an ANN with 2 input neurons, 1 output neuron,
> // and two hidden layers with 8 and 9 neurons
> struct fann *ann = fann_create_standard(4, 2, 8, 9, 1);
See also:
<fann_create_standard_array>, <fann_create_sparse>, <fann_create_shortcut>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL struct fann *FANN_API fann_create_standard(unsigned int num_layers, ...);
/* Function: fann_create_standard_array
Just like <fann_create_standard>, but with an array of layer sizes
instead of individual parameters.
Example:
> // Creating an ANN with 2 input neurons, 1 output neuron,
> // and two hidden layers with 8 and 9 neurons
> unsigned int layers[4] = {2, 8, 9, 1};
> struct fann *ann = fann_create_standard_array(4, layers);
See also:
<fann_create_standard>, <fann_create_sparse>, <fann_create_shortcut>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL struct fann *FANN_API fann_create_standard_array(unsigned int num_layers,
const unsigned int *layers);
/* Function: fann_create_sparse
Creates a standard backpropagation neural network, which is not fully connected.
Parameters:
connection_rate - The connection rate controls how many connections there will be in the
network. If the connection rate is set to 1, the network will be fully
connected, but if it is set to 0.5 only half of the connections will be set.
A connection rate of 1 will yield the same result as <fann_create_standard>
num_layers - The total number of layers including the input and the output layer.
... - Integer values determining the number of neurons in each layer starting with the
input layer and ending with the output layer.
Returns:
A pointer to the newly created <struct fann>.
See also:
<fann_create_sparse_array>, <fann_create_standard>, <fann_create_shortcut>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL struct fann *FANN_API fann_create_sparse(float connection_rate,
unsigned int num_layers, ...);
/* Function: fann_create_sparse_array
Just like <fann_create_sparse>, but with an array of layer sizes
instead of individual parameters.
See <fann_create_standard_array> for a description of the parameters.
See also:
<fann_create_sparse>, <fann_create_standard>, <fann_create_shortcut>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL struct fann *FANN_API fann_create_sparse_array(float connection_rate,
unsigned int num_layers,
const unsigned int *layers);
/* Function: fann_create_shortcut
Creates a standard backpropagation neural network, which is fully connected and which
also has shortcut connections.
Shortcut connections are connections that skip layers. A fully connected network with shortcut
connections is a network where all neurons are connected to all neurons in later layers.
Including direct connections from the input layer to the output layer.
See <fann_create_standard> for a description of the parameters.
See also:
<fann_create_shortcut_array>, <fann_create_standard>, <fann_create_sparse>,
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL struct fann *FANN_API fann_create_shortcut(unsigned int num_layers, ...);
/* Function: fann_create_shortcut_array
Just like <fann_create_shortcut>, but with an array of layer sizes
instead of individual parameters.
See <fann_create_standard_array> for a description of the parameters.
See also:
<fann_create_shortcut>, <fann_create_standard>, <fann_create_sparse>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL struct fann *FANN_API fann_create_shortcut_array(unsigned int num_layers,
const unsigned int *layers);
/* Function: fann_destroy
Destroys the entire network, properly freeing all the associated memory.
This function appears in FANN >= 1.0.0.
*/
FANN_EXTERNAL void FANN_API fann_destroy(struct fann *ann);
/* Function: fann_copy
Creates a copy of a fann structure.
Data in the user data <fann_set_user_data> is not copied, but the user data pointer is copied.
This function appears in FANN >= 2.2.0.
*/
FANN_EXTERNAL struct fann * FANN_API fann_copy(struct fann *ann);
/* Function: fann_run
Will run input through the neural network, returning an array of outputs, the number of which being
equal to the number of neurons in the output layer.
See also:
<fann_test>
This function appears in FANN >= 1.0.0.
*/
FANN_EXTERNAL fann_type * FANN_API fann_run(struct fann *ann, fann_type * input);
/* Function: fann_randomize_weights
Give each connection a random weight between *min_weight* and *max_weight*
From the beginning the weights are random between -0.1 and 0.1.
See also:
<fann_init_weights>
This function appears in FANN >= 1.0.0.
*/
FANN_EXTERNAL void FANN_API fann_randomize_weights(struct fann *ann, fann_type min_weight,
fann_type max_weight);
/* Function: fann_init_weights
Initialize the weights using Widrow + Nguyen's algorithm.
This function behaves similarly to fann_randomize_weights. It will use the algorithm developed
by Derrick Nguyen and Bernard Widrow to set the weights in such a way
as to speed up training. This technique is not always successful, and in some cases can be less
efficient than a purely random initialization.
The algorithm requires access to the range of the input data (ie, largest and smallest input),
and therefore accepts a second argument, data, which is the training data that will be used to
train the network.
See also:
<fann_randomize_weights>, <fann_read_train_from_file>
This function appears in FANN >= 1.1.0.
*/
FANN_EXTERNAL void FANN_API fann_init_weights(struct fann *ann, struct fann_train_data *train_data);
/* Function: fann_print_connections
Will print the connections of the ann in a compact matrix, for easy viewing of the internals
of the ann.
The output from fann_print_connections on a small (2 2 1) network trained on the xor problem
>Layer / Neuron 012345
>L 1 / N 3 BBa...
>L 1 / N 4 BBA...
>L 1 / N 5 ......
>L 2 / N 6 ...BBA
>L 2 / N 7 ......
This network has five real neurons and two bias neurons. This gives a total of seven neurons
named from 0 to 6. The connections between these neurons can be seen in the matrix. "." is a
place where there is no connection, while a character tells how strong the connection is on a
scale from a-z. The two real neurons in the hidden layer (neuron 3 and 4 in layer 1) have
connections from the three neurons in the previous layer as is visible in the first two lines.
The output neuron (6) has connections from the three neurons in the hidden layer 3 - 5 as is
visible in the fourth line.
To simplify the matrix output neurons are not visible as neurons that connections can come from,
and input and bias neurons are not visible as neurons that connections can go to.
This function appears in FANN >= 1.2.0.
*/
FANN_EXTERNAL void FANN_API fann_print_connections(struct fann *ann);
/* Group: Parameters */
/* Function: fann_print_parameters
Prints all of the parameters and options of the ANN
This function appears in FANN >= 1.2.0.
*/
FANN_EXTERNAL void FANN_API fann_print_parameters(struct fann *ann);
/* Function: fann_get_num_input
Get the number of input neurons.
This function appears in FANN >= 1.0.0.
*/
FANN_EXTERNAL unsigned int FANN_API fann_get_num_input(struct fann *ann);
/* Function: fann_get_num_output
Get the number of output neurons.
This function appears in FANN >= 1.0.0.
*/
FANN_EXTERNAL unsigned int FANN_API fann_get_num_output(struct fann *ann);
/* Function: fann_get_total_neurons
Get the total number of neurons in the entire network. This number does also include the
bias neurons, so a 2-4-2 network has 2+4+2 +2(bias) = 10 neurons.
This function appears in FANN >= 1.0.0.
*/
FANN_EXTERNAL unsigned int FANN_API fann_get_total_neurons(struct fann *ann);
/* Function: fann_get_total_connections
Get the total number of connections in the entire network.
This function appears in FANN >= 1.0.0.
*/
FANN_EXTERNAL unsigned int FANN_API fann_get_total_connections(struct fann *ann);
/* Function: fann_get_network_type
Get the type of neural network it was created as.
Parameters:
ann - A previously created neural network structure of
type <struct fann> pointer.
Returns:
The neural network type from enum <fann_network_type_enum>
See Also:
<fann_network_type_enum>
This function appears in FANN >= 2.1.0
*/
FANN_EXTERNAL enum fann_nettype_enum FANN_API fann_get_network_type(struct fann *ann);
/* Function: fann_get_connection_rate
Get the connection rate used when the network was created
Parameters:
ann - A previously created neural network structure of
type <struct fann> pointer.
Returns:
The connection rate
This function appears in FANN >= 2.1.0
*/
FANN_EXTERNAL float FANN_API fann_get_connection_rate(struct fann *ann);
/* Function: fann_get_num_layers
Get the number of layers in the network
Parameters:
ann - A previously created neural network structure of
type <struct fann> pointer.
Returns:
The number of layers in the neural network
Example:
> // Obtain the number of layers in a neural network
> struct fann *ann = fann_create_standard(4, 2, 8, 9, 1);
> unsigned int num_layers = fann_get_num_layers(ann);
This function appears in FANN >= 2.1.0
*/
FANN_EXTERNAL unsigned int FANN_API fann_get_num_layers(struct fann *ann);
/*Function: fann_get_layer_array
Get the number of neurons in each layer in the network.
Bias is not included so the layers match the fann_create functions.
Parameters:
ann - A previously created neural network structure of
type <struct fann> pointer.
The layers array must be preallocated to at least
sizeof(unsigned int) * fann_num_layers() long.
This function appears in FANN >= 2.1.0
*/
FANN_EXTERNAL void FANN_API fann_get_layer_array(struct fann *ann, unsigned int *layers);
/* Function: fann_get_bias_array
Get the number of bias in each layer in the network.
Parameters:
ann - A previously created neural network structure of
type <struct fann> pointer.
The bias array must be preallocated to at least
sizeof(unsigned int) * fann_num_layers() long.
This function appears in FANN >= 2.1.0
*/
FANN_EXTERNAL void FANN_API fann_get_bias_array(struct fann *ann, unsigned int *bias);
/* Function: fann_get_connection_array
Get the connections in the network.
Parameters:
ann - A previously created neural network structure of
type <struct fann> pointer.
The connections array must be preallocated to at least
sizeof(struct fann_connection) * fann_get_total_connections() long.
This function appears in FANN >= 2.1.0
*/
FANN_EXTERNAL void FANN_API fann_get_connection_array(struct fann *ann,
struct fann_connection *connections);
/* Function: fann_set_weight_array
Set connections in the network.
Parameters:
ann - A previously created neural network structure of
type <struct fann> pointer.
Only the weights can be changed, connections and weights are ignored
if they do not already exist in the network.
The array must have sizeof(struct fann_connection) * num_connections size.
This function appears in FANN >= 2.1.0
*/
FANN_EXTERNAL void FANN_API fann_set_weight_array(struct fann *ann,
struct fann_connection *connections, unsigned int num_connections);
/* Function: fann_set_weight
Set a connection in the network.
Parameters:
ann - A previously created neural network structure of
type <struct fann> pointer.
Only the weights can be changed. The connection/weight is
ignored if it does not already exist in the network.
This function appears in FANN >= 2.1.0
*/
FANN_EXTERNAL void FANN_API fann_set_weight(struct fann *ann,
unsigned int from_neuron, unsigned int to_neuron, fann_type weight);
/* Function: fann_get_weights
Get all the network weights.
Parameters:
ann - A previously created neural network structure of
type <struct fann> pointer.
weights - A fann_type pointer to user data. It is the responsibility
of the user to allocate sufficient space to store all the weights.
This function appears in FANN >= x.y.z
*/
FANN_EXTERNAL void FANN_API fann_get_weights(struct fann *ann, fann_type *weights);
/* Function: fann_set_weights
Set network weights.
Parameters:
ann - A previously created neural network structure of
type <struct fann> pointer.
weights - A fann_type pointer to user data. It is the responsibility
of the user to make the weights array sufficient long
to store all the weights.
This function appears in FANN >= x.y.z
*/
FANN_EXTERNAL void FANN_API fann_set_weights(struct fann *ann, fann_type *weights);
/* Function: fann_set_user_data
Store a pointer to user defined data. The pointer can be
retrieved with <fann_get_user_data> for example in a
callback. It is the user's responsibility to allocate and
deallocate any data that the pointer might point to.
Parameters:
ann - A previously created neural network structure of
type <struct fann> pointer.
user_data - A void pointer to user defined data.
This function appears in FANN >= 2.1.0
*/
FANN_EXTERNAL void FANN_API fann_set_user_data(struct fann *ann, void *user_data);
/* Function: fann_get_user_data
Get a pointer to user defined data that was previously set
with <fann_set_user_data>. It is the user's responsibility to
allocate and deallocate any data that the pointer might point to.
Parameters:
ann - A previously created neural network structure of
type <struct fann> pointer.
Returns:
A void pointer to user defined data.
This function appears in FANN >= 2.1.0
*/
FANN_EXTERNAL void * FANN_API fann_get_user_data(struct fann *ann);
/* Function: fann_disable_seed_rand
Disables the automatic random generator seeding that happens in FANN.
Per default FANN will always seed the random generator when creating a new network,
unless FANN_NO_SEED is defined during compilation of the library. This method can
disable this at runtime.
This function appears in FANN >= 2.3.0
*/
FANN_EXTERNAL void FANN_API fann_disable_seed_rand();
/* Function: fann_enable_seed_rand
Enables the automatic random generator seeding that happens in FANN.
Per default FANN will always seed the random generator when creating a new network,
unless FANN_NO_SEED is defined during compilation of the library. This method can
disable this at runtime.
This function appears in FANN >= 2.3.0
*/
FANN_EXTERNAL void FANN_API fann_enable_seed_rand();
#ifdef FIXEDFANN
/* Function: fann_get_decimal_point
Returns the position of the decimal point in the ann.
This function is only available when the ANN is in fixed point mode.
The decimal point is described in greater detail in the tutorial <Fixed Point Usage>.
See also:
<Fixed Point Usage>, <fann_get_multiplier>, <fann_save_to_fixed>, <fann_save_train_to_fixed>
This function appears in FANN >= 1.0.0.
*/
FANN_EXTERNAL unsigned int FANN_API fann_get_decimal_point(struct fann *ann);
/* Function: fann_get_multiplier
returns the multiplier that fix point data is multiplied with.
This function is only available when the ANN is in fixed point mode.
The multiplier is the used to convert between floating point and fixed point notation.
A floating point number is multiplied with the multiplier in order to get the fixed point
number and visa versa.
The multiplier is described in greater detail in the tutorial <Fixed Point Usage>.
See also:
<Fixed Point Usage>, <fann_get_decimal_point>, <fann_save_to_fixed>, <fann_save_train_to_fixed>
This function appears in FANN >= 1.0.0.
*/
FANN_EXTERNAL unsigned int FANN_API fann_get_multiplier(struct fann *ann);
#endif /* FIXEDFANN */
#ifdef __cplusplus
#ifndef __cplusplus
/* to fool automatic indention engines */
{
#endif
}
#endif /* __cplusplus */
#endif /* __fann_h__ */
#endif /* NOT FANN_INCLUDE */

View File

@ -0,0 +1,144 @@
/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2016 Steffen Nissen (steffen.fann@gmail.com)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __fann_activation_h__
#define __fann_activation_h__
/* internal include file, not to be included directly
*/
/* Implementation of the activation functions
*/
/* stepwise linear functions used for some of the activation functions */
/* defines used for the stepwise linear functions */
#define fann_linear_func(v1, r1, v2, r2, sum) (((((r2)-(r1)) * ((sum)-(v1)))/((v2)-(v1))) + (r1))
#define fann_stepwise(v1, v2, v3, v4, v5, v6, r1, r2, r3, r4, r5, r6, min, max, sum) (sum < v5 ? (sum < v3 ? (sum < v2 ? (sum < v1 ? min : fann_linear_func(v1, r1, v2, r2, sum)) : fann_linear_func(v2, r2, v3, r3, sum)) : (sum < v4 ? fann_linear_func(v3, r3, v4, r4, sum) : fann_linear_func(v4, r4, v5, r5, sum))) : (sum < v6 ? fann_linear_func(v5, r5, v6, r6, sum) : max))
/* FANN_LINEAR */
/* #define fann_linear(steepness, sum) fann_mult(steepness, sum) */
#define fann_linear_derive(steepness, value) (steepness)
/* FANN_SIGMOID */
/* #define fann_sigmoid(steepness, sum) (1.0f/(1.0f + exp(-2.0f * steepness * sum))) */
#define fann_sigmoid_real(sum) (1.0f/(1.0f + exp(-2.0f * sum)))
#define fann_sigmoid_derive(steepness, value) (2.0f * steepness * value * (1.0f - value))
/* FANN_SIGMOID_SYMMETRIC */
/* #define fann_sigmoid_symmetric(steepness, sum) (2.0f/(1.0f + exp(-2.0f * steepness * sum)) - 1.0f) */
#define fann_sigmoid_symmetric_real(sum) (2.0f/(1.0f + exp(-2.0f * sum)) - 1.0f)
#define fann_sigmoid_symmetric_derive(steepness, value) steepness * (1.0f - (value*value))
/* FANN_GAUSSIAN */
/* #define fann_gaussian(steepness, sum) (exp(-sum * steepness * sum * steepness)) */
#define fann_gaussian_real(sum) (exp(-sum * sum))
#define fann_gaussian_derive(steepness, value, sum) (-2.0f * sum * value * steepness * steepness)
/* FANN_GAUSSIAN_SYMMETRIC */
/* #define fann_gaussian_symmetric(steepness, sum) ((exp(-sum * steepness * sum * steepness)*2.0)-1.0) */
#define fann_gaussian_symmetric_real(sum) ((exp(-sum * sum)*2.0f)-1.0f)
#define fann_gaussian_symmetric_derive(steepness, value, sum) (-2.0f * sum * (value+1.0f) * steepness * steepness)
/* FANN_ELLIOT */
/* #define fann_elliot(steepness, sum) (((sum * steepness) / 2.0f) / (1.0f + fann_abs(sum * steepness)) + 0.5f) */
#define fann_elliot_real(sum) (((sum) / 2.0f) / (1.0f + fann_abs(sum)) + 0.5f)
#define fann_elliot_derive(steepness, value, sum) (steepness * 1.0f / (2.0f * (1.0f + fann_abs(sum)) * (1.0f + fann_abs(sum))))
/* FANN_ELLIOT_SYMMETRIC */
/* #define fann_elliot_symmetric(steepness, sum) ((sum * steepness) / (1.0f + fann_abs(sum * steepness)))*/
#define fann_elliot_symmetric_real(sum) ((sum) / (1.0f + fann_abs(sum)))
#define fann_elliot_symmetric_derive(steepness, value, sum) (steepness * 1.0f / ((1.0f + fann_abs(sum)) * (1.0f + fann_abs(sum))))
/* FANN_SIN_SYMMETRIC */
#define fann_sin_symmetric_real(sum) (sin(sum))
#define fann_sin_symmetric_derive(steepness, sum) (steepness*cos(steepness*sum))
/* FANN_COS_SYMMETRIC */
#define fann_cos_symmetric_real(sum) (cos(sum))
#define fann_cos_symmetric_derive(steepness, sum) (steepness*-sin(steepness*sum))
/* FANN_SIN */
#define fann_sin_real(sum) (sin(sum)/2.0f+0.5f)
#define fann_sin_derive(steepness, sum) (steepness*cos(steepness*sum)/2.0f)
/* FANN_COS */
#define fann_cos_real(sum) (cos(sum)/2.0f+0.5f)
#define fann_cos_derive(steepness, sum) (steepness*-sin(steepness*sum)/2.0f)
#define fann_activation_switch(activation_function, value, result) \
switch(activation_function) \
{ \
case FANN_LINEAR: \
result = (fann_type)value; \
break; \
case FANN_LINEAR_PIECE: \
result = (fann_type)((value < 0) ? 0 : (value > 1) ? 1 : value); \
break; \
case FANN_LINEAR_PIECE_SYMMETRIC: \
result = (fann_type)((value < -1) ? -1 : (value > 1) ? 1 : value); \
break; \
case FANN_SIGMOID: \
result = (fann_type)fann_sigmoid_real(value); \
break; \
case FANN_SIGMOID_SYMMETRIC: \
result = (fann_type)fann_sigmoid_symmetric_real(value); \
break; \
case FANN_SIGMOID_SYMMETRIC_STEPWISE: \
result = (fann_type)fann_stepwise(-2.64665293693542480469e+00, -1.47221934795379638672e+00, -5.49306154251098632812e-01, 5.49306154251098632812e-01, 1.47221934795379638672e+00, 2.64665293693542480469e+00, -9.90000009536743164062e-01, -8.99999976158142089844e-01, -5.00000000000000000000e-01, 5.00000000000000000000e-01, 8.99999976158142089844e-01, 9.90000009536743164062e-01, -1, 1, value); \
break; \
case FANN_SIGMOID_STEPWISE: \
result = (fann_type)fann_stepwise(-2.64665246009826660156e+00, -1.47221946716308593750e+00, -5.49306154251098632812e-01, 5.49306154251098632812e-01, 1.47221934795379638672e+00, 2.64665293693542480469e+00, 4.99999988824129104614e-03, 5.00000007450580596924e-02, 2.50000000000000000000e-01, 7.50000000000000000000e-01, 9.49999988079071044922e-01, 9.95000004768371582031e-01, 0, 1, value); \
break; \
case FANN_THRESHOLD: \
result = (fann_type)((value < 0) ? 0 : 1); \
break; \
case FANN_THRESHOLD_SYMMETRIC: \
result = (fann_type)((value < 0) ? -1 : 1); \
break; \
case FANN_GAUSSIAN: \
result = (fann_type)fann_gaussian_real(value); \
break; \
case FANN_GAUSSIAN_SYMMETRIC: \
result = (fann_type)fann_gaussian_symmetric_real(value); \
break; \
case FANN_ELLIOT: \
result = (fann_type)fann_elliot_real(value); \
break; \
case FANN_ELLIOT_SYMMETRIC: \
result = (fann_type)fann_elliot_symmetric_real(value); \
break; \
case FANN_SIN_SYMMETRIC: \
result = (fann_type)fann_sin_symmetric_real(value); \
break; \
case FANN_COS_SYMMETRIC: \
result = (fann_type)fann_cos_symmetric_real(value); \
break; \
case FANN_SIN: \
result = (fann_type)fann_sin_real(value); \
break; \
case FANN_COS: \
result = (fann_type)fann_cos_real(value); \
break; \
case FANN_GAUSSIAN_STEPWISE: \
result = 0; \
break; \
}
#endif

View File

@ -0,0 +1,561 @@
/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2016 Steffen Nissen (steffen.fann@gmail.com)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __fann_cascade_h__
#define __fann_cascade_h__
/* Section: FANN Cascade Training
Cascade training differs from ordinary training in the sense that it starts with an empty neural network
and then adds neurons one by one, while it trains the neural network. The main benefit of this approach
is that you do not have to guess the number of hidden layers and neurons prior to training, but cascade
training has also proved better at solving some problems.
The basic idea of cascade training is that a number of candidate neurons are trained separate from the
real network, then the most promising of these candidate neurons is inserted into the neural network.
Then the output connections are trained and new candidate neurons are prepared. The candidate neurons are
created as shortcut connected neurons in a new hidden layer, which means that the final neural network
will consist of a number of hidden layers with one shortcut connected neuron in each.
*/
/* Group: Cascade Training */
#ifndef FIXEDFANN
/* Function: fann_cascadetrain_on_data
Trains on an entire dataset, for a period of time using the Cascade2 training algorithm.
This algorithm adds neurons to the neural network while training, which means that it
needs to start with an ANN without any hidden layers. The neural network should also use
shortcut connections, so <fann_create_shortcut> should be used to create the ANN like this:
>struct fann *ann = fann_create_shortcut(2, fann_num_input_train_data(train_data), fann_num_output_train_data(train_data));
This training uses the parameters set using the fann_set_cascade_..., but it also uses another
training algorithm as it's internal training algorithm. This algorithm can be set to either
FANN_TRAIN_RPROP or FANN_TRAIN_QUICKPROP by <fann_set_training_algorithm>, and the parameters
set for these training algorithms will also affect the cascade training.
Parameters:
ann - The neural network
data - The data, which should be used during training
max_neuron - The maximum number of neurons to be added to neural network
neurons_between_reports - The number of neurons between printing a status report to stdout.
A value of zero means no reports should be printed.
desired_error - The desired <fann_get_MSE> or <fann_get_bit_fail>, depending on which stop function
is chosen by <fann_set_train_stop_function>.
Instead of printing out reports every neurons_between_reports, a callback function can be called
(see <fann_set_callback>).
See also:
<fann_train_on_data>, <fann_cascadetrain_on_file>, <Parameters>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL void FANN_API fann_cascadetrain_on_data(struct fann *ann,
struct fann_train_data *data,
unsigned int max_neurons,
unsigned int neurons_between_reports,
float desired_error);
/* Function: fann_cascadetrain_on_file
Does the same as <fann_cascadetrain_on_data>, but reads the training data directly from a file.
See also:
<fann_cascadetrain_on_data>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL void FANN_API fann_cascadetrain_on_file(struct fann *ann, const char *filename,
unsigned int max_neurons,
unsigned int neurons_between_reports,
float desired_error);
/* Group: Parameters */
/* Function: fann_get_cascade_output_change_fraction
The cascade output change fraction is a number between 0 and 1 determining how large a fraction
the <fann_get_MSE> value should change within <fann_get_cascade_output_stagnation_epochs> during
training of the output connections, in order for the training not to stagnate. If the training
stagnates, the training of the output connections will be ended and new candidates will be prepared.
This means:
If the MSE does not change by a fraction of <fann_get_cascade_output_change_fraction> during a
period of <fann_get_cascade_output_stagnation_epochs>, the training of the output connections
is stopped because the training has stagnated.
If the cascade output change fraction is low, the output connections will be trained more and if the
fraction is high they will be trained less.
The default cascade output change fraction is 0.01, which is equivalent to a 1% change in MSE.
See also:
<fann_set_cascade_output_change_fraction>, <fann_get_MSE>, <fann_get_cascade_output_stagnation_epochs>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL float FANN_API fann_get_cascade_output_change_fraction(struct fann *ann);
/* Function: fann_set_cascade_output_change_fraction
Sets the cascade output change fraction.
See also:
<fann_get_cascade_output_change_fraction>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL void FANN_API fann_set_cascade_output_change_fraction(struct fann *ann,
float cascade_output_change_fraction);
/* Function: fann_get_cascade_output_stagnation_epochs
The number of cascade output stagnation epochs determines the number of epochs training is allowed to
continue without changing the MSE by a fraction of <fann_get_cascade_output_change_fraction>.
See more info about this parameter in <fann_get_cascade_output_change_fraction>.
The default number of cascade output stagnation epochs is 12.
See also:
<fann_set_cascade_output_stagnation_epochs>, <fann_get_cascade_output_change_fraction>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL unsigned int FANN_API fann_get_cascade_output_stagnation_epochs(struct fann *ann);
/* Function: fann_set_cascade_output_stagnation_epochs
Sets the number of cascade output stagnation epochs.
See also:
<fann_get_cascade_output_stagnation_epochs>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL void FANN_API fann_set_cascade_output_stagnation_epochs(struct fann *ann,
unsigned int cascade_output_stagnation_epochs);
/* Function: fann_get_cascade_candidate_change_fraction
The cascade candidate change fraction is a number between 0 and 1 determining how large a fraction
the <fann_get_MSE> value should change within <fann_get_cascade_candidate_stagnation_epochs> during
training of the candidate neurons, in order for the training not to stagnate. If the training
stagnates, the training of the candidate neurons will be ended and the best candidate will be selected.
This means:
If the MSE does not change by a fraction of <fann_get_cascade_candidate_change_fraction> during a
period of <fann_get_cascade_candidate_stagnation_epochs>, the training of the candidate neurons
is stopped because the training has stagnated.
If the cascade candidate change fraction is low, the candidate neurons will be trained more and if the
fraction is high they will be trained less.
The default cascade candidate change fraction is 0.01, which is equivalent to a 1% change in MSE.
See also:
<fann_set_cascade_candidate_change_fraction>, <fann_get_MSE>, <fann_get_cascade_candidate_stagnation_epochs>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL float FANN_API fann_get_cascade_candidate_change_fraction(struct fann *ann);
/* Function: fann_set_cascade_candidate_change_fraction
Sets the cascade candidate change fraction.
See also:
<fann_get_cascade_candidate_change_fraction>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL void FANN_API fann_set_cascade_candidate_change_fraction(struct fann *ann,
float cascade_candidate_change_fraction);
/* Function: fann_get_cascade_candidate_stagnation_epochs
The number of cascade candidate stagnation epochs determines the number of epochs training is allowed to
continue without changing the MSE by a fraction of <fann_get_cascade_candidate_change_fraction>.
See more info about this parameter in <fann_get_cascade_candidate_change_fraction>.
The default number of cascade candidate stagnation epochs is 12.
See also:
<fann_set_cascade_candidate_stagnation_epochs>, <fann_get_cascade_candidate_change_fraction>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL unsigned int FANN_API fann_get_cascade_candidate_stagnation_epochs(struct fann *ann);
/* Function: fann_set_cascade_candidate_stagnation_epochs
Sets the number of cascade candidate stagnation epochs.
See also:
<fann_get_cascade_candidate_stagnation_epochs>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL void FANN_API fann_set_cascade_candidate_stagnation_epochs(struct fann *ann,
unsigned int cascade_candidate_stagnation_epochs);
/* Function: fann_get_cascade_weight_multiplier
The weight multiplier is a parameter which is used to multiply the weights from the candidate neuron
before adding the neuron to the neural network. This parameter is usually between 0 and 1, and is used
to make the training a bit less aggressive.
The default weight multiplier is 0.4
See also:
<fann_set_cascade_weight_multiplier>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL fann_type FANN_API fann_get_cascade_weight_multiplier(struct fann *ann);
/* Function: fann_set_cascade_weight_multiplier
Sets the weight multiplier.
See also:
<fann_get_cascade_weight_multiplier>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL void FANN_API fann_set_cascade_weight_multiplier(struct fann *ann,
fann_type cascade_weight_multiplier);
/* Function: fann_get_cascade_candidate_limit
The candidate limit is a limit for how much the candidate neuron may be trained.
The limit is a limit on the proportion between the MSE and candidate score.
Set this to a lower value to avoid overfitting and to a higher if overfitting is
not a problem.
The default candidate limit is 1000.0
See also:
<fann_set_cascade_candidate_limit>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL fann_type FANN_API fann_get_cascade_candidate_limit(struct fann *ann);
/* Function: fann_set_cascade_candidate_limit
Sets the candidate limit.
See also:
<fann_get_cascade_candidate_limit>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL void FANN_API fann_set_cascade_candidate_limit(struct fann *ann,
fann_type cascade_candidate_limit);
/* Function: fann_get_cascade_max_out_epochs
The maximum out epochs determines the maximum number of epochs the output connections
may be trained after adding a new candidate neuron.
The default max out epochs is 150
See also:
<fann_set_cascade_max_out_epochs>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL unsigned int FANN_API fann_get_cascade_max_out_epochs(struct fann *ann);
/* Function: fann_set_cascade_max_out_epochs
Sets the maximum out epochs.
See also:
<fann_get_cascade_max_out_epochs>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL void FANN_API fann_set_cascade_max_out_epochs(struct fann *ann,
unsigned int cascade_max_out_epochs);
/* Function: fann_get_cascade_min_out_epochs
The minimum out epochs determines the minimum number of epochs the output connections
must be trained after adding a new candidate neuron.
The default min out epochs is 50
See also:
<fann_set_cascade_min_out_epochs>
This function appears in FANN >= 2.2.0.
*/
FANN_EXTERNAL unsigned int FANN_API fann_get_cascade_min_out_epochs(struct fann *ann);
/* Function: fann_set_cascade_min_out_epochs
Sets the minimum out epochs.
See also:
<fann_get_cascade_min_out_epochs>
This function appears in FANN >= 2.2.0.
*/
FANN_EXTERNAL void FANN_API fann_set_cascade_min_out_epochs(struct fann *ann,
unsigned int cascade_min_out_epochs);
/* Function: fann_get_cascade_max_cand_epochs
The maximum candidate epochs determines the maximum number of epochs the input
connections to the candidates may be trained before adding a new candidate neuron.
The default max candidate epochs is 150
See also:
<fann_set_cascade_max_cand_epochs>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL unsigned int FANN_API fann_get_cascade_max_cand_epochs(struct fann *ann);
/* Function: fann_set_cascade_max_cand_epochs
Sets the max candidate epochs.
See also:
<fann_get_cascade_max_cand_epochs>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL void FANN_API fann_set_cascade_max_cand_epochs(struct fann *ann,
unsigned int cascade_max_cand_epochs);
/* Function: fann_get_cascade_min_cand_epochs
The minimum candidate epochs determines the minimum number of epochs the input
connections to the candidates may be trained before adding a new candidate neuron.
The default min candidate epochs is 50
See also:
<fann_set_cascade_min_cand_epochs>
This function appears in FANN >= 2.2.0.
*/
FANN_EXTERNAL unsigned int FANN_API fann_get_cascade_min_cand_epochs(struct fann *ann);
/* Function: fann_set_cascade_min_cand_epochs
Sets the min candidate epochs.
See also:
<fann_get_cascade_min_cand_epochs>
This function appears in FANN >= 2.2.0.
*/
FANN_EXTERNAL void FANN_API fann_set_cascade_min_cand_epochs(struct fann *ann,
unsigned int cascade_min_cand_epochs);
/* Function: fann_get_cascade_num_candidates
The number of candidates used during training (calculated by multiplying <fann_get_cascade_activation_functions_count>,
<fann_get_cascade_activation_steepnesses_count> and <fann_get_cascade_num_candidate_groups>).
The actual candidates is defined by the <fann_get_cascade_activation_functions> and
<fann_get_cascade_activation_steepnesses> arrays. These arrays define the activation functions
and activation steepnesses used for the candidate neurons. If there are 2 activation functions
in the activation function array and 3 steepnesses in the steepness array, then there will be
2x3=6 different candidates which will be trained. These 6 different candidates can be copied into
several candidate groups, where the only difference between these groups is the initial weights.
If the number of groups is set to 2, then the number of candidate neurons will be 2x3x2=12. The
number of candidate groups is defined by <fann_set_cascade_num_candidate_groups>.
The default number of candidates is 6x4x2 = 48
See also:
<fann_get_cascade_activation_functions>, <fann_get_cascade_activation_functions_count>,
<fann_get_cascade_activation_steepnesses>, <fann_get_cascade_activation_steepnesses_count>,
<fann_get_cascade_num_candidate_groups>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL unsigned int FANN_API fann_get_cascade_num_candidates(struct fann *ann);
/* Function: fann_get_cascade_activation_functions_count
The number of activation functions in the <fann_get_cascade_activation_functions> array.
The default number of activation functions is 10.
See also:
<fann_get_cascade_activation_functions>, <fann_set_cascade_activation_functions>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL unsigned int FANN_API fann_get_cascade_activation_functions_count(struct fann *ann);
/* Function: fann_get_cascade_activation_functions
The cascade activation functions array is an array of the different activation functions used by
the candidates.
See <fann_get_cascade_num_candidates> for a description of which candidate neurons will be
generated by this array.
The default activation functions are {FANN_SIGMOID, FANN_SIGMOID_SYMMETRIC, FANN_GAUSSIAN,
FANN_GAUSSIAN_SYMMETRIC, FANN_ELLIOT, FANN_ELLIOT_SYMMETRIC, FANN_SIN_SYMMETRIC,
FANN_COS_SYMMETRIC, FANN_SIN, FANN_COS}
See also:
<fann_get_cascade_activation_functions_count>, <fann_set_cascade_activation_functions>,
<fann_activationfunc_enum>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL enum fann_activationfunc_enum * FANN_API fann_get_cascade_activation_functions(
struct fann *ann);
/* Function: fann_set_cascade_activation_functions
Sets the array of cascade candidate activation functions. The array must be just as long
as defined by the count.
See <fann_get_cascade_num_candidates> for a description of which candidate neurons will be
generated by this array.
See also:
<fann_get_cascade_activation_steepnesses_count>, <fann_get_cascade_activation_steepnesses>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL void FANN_API fann_set_cascade_activation_functions(struct fann *ann,
enum fann_activationfunc_enum *
cascade_activation_functions,
unsigned int
cascade_activation_functions_count);
/* Function: fann_get_cascade_activation_steepnesses_count
The number of activation steepnesses in the <fann_get_cascade_activation_functions> array.
The default number of activation steepnesses is 4.
See also:
<fann_get_cascade_activation_steepnesses>, <fann_set_cascade_activation_functions>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL unsigned int FANN_API fann_get_cascade_activation_steepnesses_count(struct fann *ann);
/* Function: fann_get_cascade_activation_steepnesses
The cascade activation steepnesses array is an array of the different activation functions used by
the candidates.
See <fann_get_cascade_num_candidates> for a description of which candidate neurons will be
generated by this array.
The default activation steepnesses is {0.25, 0.50, 0.75, 1.00}
See also:
<fann_set_cascade_activation_steepnesses>, <fann_get_cascade_activation_steepnesses_count>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL fann_type * FANN_API fann_get_cascade_activation_steepnesses(struct fann *ann);
/* Function: fann_set_cascade_activation_steepnesses
Sets the array of cascade candidate activation steepnesses. The array must be just as long
as defined by the count.
See <fann_get_cascade_num_candidates> for a description of which candidate neurons will be
generated by this array.
See also:
<fann_get_cascade_activation_steepnesses>, <fann_get_cascade_activation_steepnesses_count>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL void FANN_API fann_set_cascade_activation_steepnesses(struct fann *ann,
fann_type *
cascade_activation_steepnesses,
unsigned int
cascade_activation_steepnesses_count);
/* Function: fann_get_cascade_num_candidate_groups
The number of candidate groups is the number of groups of identical candidates which will be used
during training.
This number can be used to have more candidates without having to define new parameters for the candidates.
See <fann_get_cascade_num_candidates> for a description of which candidate neurons will be
generated by this parameter.
The default number of candidate groups is 2
See also:
<fann_set_cascade_num_candidate_groups>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL unsigned int FANN_API fann_get_cascade_num_candidate_groups(struct fann *ann);
/* Function: fann_set_cascade_num_candidate_groups
Sets the number of candidate groups.
See also:
<fann_get_cascade_num_candidate_groups>
This function appears in FANN >= 2.0.0.
*/
FANN_EXTERNAL void FANN_API fann_set_cascade_num_candidate_groups(struct fann *ann,
unsigned int cascade_num_candidate_groups);
#endif /* FIXEDFANN */
#endif

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,826 @@
/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2016 Steffen Nissen (steffen.fann@gmail.com)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __fann_data_h__
#define __fann_data_h__
#include <stdio.h>
/* Section: FANN Datatypes
The two main datatypes used in the fann library are <struct fann>,
which represents an artificial neural network, and <struct fann_train_data>,
which represents training data.
*/
/* Type: fann_type
fann_type is the type used for the weights, inputs and outputs of the neural network.
fann_type is defined as a:
float - if you include fann.h or floatfann.h
double - if you include doublefann.h
int - if you include fixedfann.h (please be aware that fixed point usage is
only to be used during execution, and not during training).
*/
/* Enum: fann_train_enum
The Training algorithms used when training on <struct fann_train_data> with functions like
<fann_train_on_data> or <fann_train_on_file>. The incremental training alters the weights
after each time it is presented an input pattern, while batch only alters the weights once after
it has been presented to all the patterns.
FANN_TRAIN_INCREMENTAL - Standard backpropagation algorithm, where the weights are
updated after each training pattern. This means that the weights are updated many
times during a single epoch. For this reason some problems will train very fast with
this algorithm, while other more advanced problems will not train very well.
FANN_TRAIN_BATCH - Standard backpropagation algorithm, where the weights are updated after
calculating the mean square error for the whole training set. This means that the weights
are only updated once during an epoch. For this reason some problems will train slower with
this algorithm. But since the mean square error is calculated more correctly than in
incremental training, some problems will reach better solutions with this algorithm.
FANN_TRAIN_RPROP - A more advanced batch training algorithm which achieves good results
for many problems. The RPROP training algorithm is adaptive, and does therefore not
use the learning_rate. Some other parameters can however be set to change the way the
RPROP algorithm works, but it is only recommended for users with insight in how the RPROP
training algorithm works. The RPROP training algorithm is described by
[Riedmiller and Braun, 1993], but the actual learning algorithm used here is the
iRPROP- training algorithm which is described by [Igel and Husken, 2000] which
is a variant of the standard RPROP training algorithm.
FANN_TRAIN_QUICKPROP - A more advanced batch training algorithm which achieves good results
for many problems. The quickprop training algorithm uses the learning_rate parameter
along with other more advanced parameters, but it is only recommended to change these
advanced parameters, for users with insight in how the quickprop training algorithm works.
The quickprop training algorithm is described by [Fahlman, 1988].
FANN_TRAIN_SARPROP - THE SARPROP ALGORITHM: A SIMULATED ANNEALING ENHANCEMENT TO RESILIENT BACK PROPAGATION
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.47.8197&rep=rep1&type=pdf
See also:
<fann_set_training_algorithm>, <fann_get_training_algorithm>
*/
enum fann_train_enum
{
FANN_TRAIN_INCREMENTAL = 0,
FANN_TRAIN_BATCH,
FANN_TRAIN_RPROP,
FANN_TRAIN_QUICKPROP,
FANN_TRAIN_SARPROP
};
/* Constant: FANN_TRAIN_NAMES
Constant array consisting of the names for the training algorithms, so that the name of an
training function can be received by:
(code)
char *name = FANN_TRAIN_NAMES[train_function];
(end)
See Also:
<fann_train_enum>
*/
static char const *const FANN_TRAIN_NAMES[] = {
"FANN_TRAIN_INCREMENTAL",
"FANN_TRAIN_BATCH",
"FANN_TRAIN_RPROP",
"FANN_TRAIN_QUICKPROP",
"FANN_TRAIN_SARPROP"
};
/* Enums: fann_activationfunc_enum
The activation functions used for the neurons during training. The activation functions
can either be defined for a group of neurons by <fann_set_activation_function_hidden> and
<fann_set_activation_function_output> or it can be defined for a single neuron by <fann_set_activation_function>.
The steepness of an activation function is defined in the same way by
<fann_set_activation_steepness_hidden>, <fann_set_activation_steepness_output> and <fann_set_activation_steepness>.
The functions are described with functions where:
* x is the input to the activation function,
* y is the output,
* s is the steepness and
* d is the derivation.
FANN_LINEAR - Linear activation function.
* span: -inf < y < inf
* y = x*s, d = 1*s
* Can NOT be used in fixed point.
FANN_THRESHOLD - Threshold activation function.
* x < 0 -> y = 0, x >= 0 -> y = 1
* Can NOT be used during training.
FANN_THRESHOLD_SYMMETRIC - Threshold activation function.
* x < 0 -> y = -1, x >= 0 -> y = 1
* Can NOT be used during training.
FANN_SIGMOID - Sigmoid activation function.
* One of the most used activation functions.
* span: 0 < y < 1
* y = 1/(1 + exp(-2*s*x))
* d = 2*s*y*(1 - y)
FANN_SIGMOID_STEPWISE - Stepwise linear approximation to sigmoid.
* Faster than sigmoid but a bit less precise.
FANN_SIGMOID_SYMMETRIC - Symmetric sigmoid activation function, aka. tanh.
* One of the most used activation functions.
* span: -1 < y < 1
* y = tanh(s*x) = 2/(1 + exp(-2*s*x)) - 1
* d = s*(1-(y*y))
FANN_SIGMOID_SYMMETRIC_STEPWISE - Stepwise linear approximation to symmetric sigmoid.
* Faster than symmetric sigmoid but a bit less precise.
FANN_GAUSSIAN - Gaussian activation function.
* 0 when x = -inf, 1 when x = 0 and 0 when x = inf
* span: 0 < y < 1
* y = exp(-x*s*x*s)
* d = -2*x*s*y*s
FANN_GAUSSIAN_SYMMETRIC - Symmetric gaussian activation function.
* -1 when x = -inf, 1 when x = 0 and 0 when x = inf
* span: -1 < y < 1
* y = exp(-x*s*x*s)*2-1
* d = -2*x*s*(y+1)*s
FANN_ELLIOT - Fast (sigmoid like) activation function defined by David Elliott
* span: 0 < y < 1
* y = ((x*s) / 2) / (1 + |x*s|) + 0.5
* d = s*1/(2*(1+|x*s|)*(1+|x*s|))
FANN_ELLIOT_SYMMETRIC - Fast (symmetric sigmoid like) activation function defined by David Elliott
* span: -1 < y < 1
* y = (x*s) / (1 + |x*s|)
* d = s*1/((1+|x*s|)*(1+|x*s|))
FANN_LINEAR_PIECE - Bounded linear activation function.
* span: 0 <= y <= 1
* y = x*s, d = 1*s
FANN_LINEAR_PIECE_SYMMETRIC - Bounded linear activation function.
* span: -1 <= y <= 1
* y = x*s, d = 1*s
FANN_SIN_SYMMETRIC - Periodical sinus activation function.
* span: -1 <= y <= 1
* y = sin(x*s)
* d = s*cos(x*s)
FANN_COS_SYMMETRIC - Periodical cosinus activation function.
* span: -1 <= y <= 1
* y = cos(x*s)
* d = s*-sin(x*s)
FANN_SIN - Periodical sinus activation function.
* span: 0 <= y <= 1
* y = sin(x*s)/2+0.5
* d = s*cos(x*s)/2
FANN_COS - Periodical cosinus activation function.
* span: 0 <= y <= 1
* y = cos(x*s)/2+0.5
* d = s*-sin(x*s)/2
See also:
<fann_set_activation_function_layer>, <fann_set_activation_function_hidden>,
<fann_set_activation_function_output>, <fann_set_activation_steepness>,
<fann_set_activation_function>
*/
enum fann_activationfunc_enum
{
FANN_LINEAR = 0,
FANN_THRESHOLD,
FANN_THRESHOLD_SYMMETRIC,
FANN_SIGMOID,
FANN_SIGMOID_STEPWISE,
FANN_SIGMOID_SYMMETRIC,
FANN_SIGMOID_SYMMETRIC_STEPWISE,
FANN_GAUSSIAN,
FANN_GAUSSIAN_SYMMETRIC,
/* Stepwise linear approximation to gaussian.
* Faster than gaussian but a bit less precise.
* NOT implemented yet.
*/
FANN_GAUSSIAN_STEPWISE,
FANN_ELLIOT,
FANN_ELLIOT_SYMMETRIC,
FANN_LINEAR_PIECE,
FANN_LINEAR_PIECE_SYMMETRIC,
FANN_SIN_SYMMETRIC,
FANN_COS_SYMMETRIC,
FANN_SIN,
FANN_COS
};
/* Constant: FANN_ACTIVATIONFUNC_NAMES
Constant array consisting of the names for the activation function, so that the name of an
activation function can be received by:
(code)
char *name = FANN_ACTIVATIONFUNC_NAMES[activation_function];
(end)
See Also:
<fann_activationfunc_enum>
*/
static char const *const FANN_ACTIVATIONFUNC_NAMES[] = {
"FANN_LINEAR",
"FANN_THRESHOLD",
"FANN_THRESHOLD_SYMMETRIC",
"FANN_SIGMOID",
"FANN_SIGMOID_STEPWISE",
"FANN_SIGMOID_SYMMETRIC",
"FANN_SIGMOID_SYMMETRIC_STEPWISE",
"FANN_GAUSSIAN",
"FANN_GAUSSIAN_SYMMETRIC",
"FANN_GAUSSIAN_STEPWISE",
"FANN_ELLIOT",
"FANN_ELLIOT_SYMMETRIC",
"FANN_LINEAR_PIECE",
"FANN_LINEAR_PIECE_SYMMETRIC",
"FANN_SIN_SYMMETRIC",
"FANN_COS_SYMMETRIC",
"FANN_SIN",
"FANN_COS"
};
/* Enum: fann_errorfunc_enum
Error function used during training.
FANN_ERRORFUNC_LINEAR - Standard linear error function.
FANN_ERRORFUNC_TANH - Tanh error function, usually better
but can require a lower learning rate. This error function aggressively targets outputs that
differ much from the desired, while not targeting outputs that only differ a little that much.
This activation function is not recommended for cascade training and incremental training.
See also:
<fann_set_train_error_function>, <fann_get_train_error_function>
*/
enum fann_errorfunc_enum
{
FANN_ERRORFUNC_LINEAR = 0,
FANN_ERRORFUNC_TANH
};
/* Constant: FANN_ERRORFUNC_NAMES
Constant array consisting of the names for the training error functions, so that the name of an
error function can be received by:
(code)
char *name = FANN_ERRORFUNC_NAMES[error_function];
(end)
See Also:
<fann_errorfunc_enum>
*/
static char const *const FANN_ERRORFUNC_NAMES[] = {
"FANN_ERRORFUNC_LINEAR",
"FANN_ERRORFUNC_TANH"
};
/* Enum: fann_stopfunc_enum
Stop criteria used during training.
FANN_STOPFUNC_MSE - Stop criterion is Mean Square Error (MSE) value.
FANN_STOPFUNC_BIT - Stop criterion is number of bits that fail. The number of bits; means the
number of output neurons which differ more than the bit fail limit
(see <fann_get_bit_fail_limit>, <fann_set_bit_fail_limit>).
The bits are counted in all of the training data, so this number can be higher than
the number of training data.
See also:
<fann_set_train_stop_function>, <fann_get_train_stop_function>
*/
enum fann_stopfunc_enum
{
FANN_STOPFUNC_MSE = 0,
FANN_STOPFUNC_BIT
};
/* Constant: FANN_STOPFUNC_NAMES
Constant array consisting of the names for the training stop functions, so that the name of a
stop function can be received by:
(code)
char *name = FANN_STOPFUNC_NAMES[stop_function];
(end)
See Also:
<fann_stopfunc_enum>
*/
static char const *const FANN_STOPFUNC_NAMES[] = {
"FANN_STOPFUNC_MSE",
"FANN_STOPFUNC_BIT"
};
/* Enum: fann_network_type_enum
Definition of network types used by <fann_get_network_type>
FANN_NETTYPE_LAYER - Each layer only has connections to the next layer
FANN_NETTYPE_SHORTCUT - Each layer has connections to all following layers
See Also:
<fann_get_network_type>
This enumeration appears in FANN >= 2.1.0
*/
enum fann_nettype_enum
{
FANN_NETTYPE_LAYER = 0, /* Each layer only has connections to the next layer */
FANN_NETTYPE_SHORTCUT /* Each layer has connections to all following layers */
};
/* Constant: FANN_NETWORK_TYPE_NAMES
Constant array consisting of the names for the network types, so that the name of an
network type can be received by:
(code)
char *network_type_name = FANN_NETWORK_TYPE_NAMES[fann_get_network_type(ann)];
(end)
See Also:
<fann_get_network_type>
This constant appears in FANN >= 2.1.0
*/
static char const *const FANN_NETTYPE_NAMES[] = {
"FANN_NETTYPE_LAYER",
"FANN_NETTYPE_SHORTCUT"
};
/* forward declarations for use with the callback */
struct fann;
struct fann_train_data;
/* Type: fann_callback_type
This callback function can be called during training when using <fann_train_on_data>,
<fann_train_on_file> or <fann_cascadetrain_on_data>.
>typedef int (FANN_API * fann_callback_type) (struct fann *ann, struct fann_train_data *train,
> unsigned int max_epochs,
> unsigned int epochs_between_reports,
> float desired_error, unsigned int epochs);
The callback can be set by using <fann_set_callback> and is very useful for doing custom
things during training. It is recommended to use this function when implementing custom
training procedures, or when visualizing the training in a GUI etc. The parameters which the
callback function takes are the parameters given to <fann_train_on_data>, plus an epochs
parameter which tells how many epochs the training has taken so far.
The callback function should return an integer, if the callback function returns -1, the training
will terminate.
Example of a callback function:
>int FANN_API test_callback(struct fann *ann, struct fann_train_data *train,
> unsigned int max_epochs, unsigned int epochs_between_reports,
> float desired_error, unsigned int epochs)
>{
> printf("Epochs %8d. MSE: %.5f. Desired-MSE: %.5f\n", epochs, fann_get_MSE(ann), desired_error);
> return 0;
>}
See also:
<fann_set_callback>, <fann_train_on_data>
*/
FANN_EXTERNAL typedef int (FANN_API * fann_callback_type) (struct fann *ann, struct fann_train_data *train,
unsigned int max_epochs,
unsigned int epochs_between_reports,
float desired_error, unsigned int epochs);
/* ----- Data structures -----
* No data within these structures should be altered directly by the user.
*/
struct fann_neuron
{
/* Index to the first and last connection
* (actually the last is a past end index)
*/
unsigned int first_con;
unsigned int last_con;
/* The sum of the inputs multiplied with the weights */
fann_type sum;
/* The value of the activation function applied to the sum */
fann_type value;
/* The steepness of the activation function */
fann_type activation_steepness;
/* Used to choose which activation function to use */
enum fann_activationfunc_enum activation_function;
#ifdef __GNUC__
} __attribute__ ((packed));
#else
};
#endif
/* A single layer in the neural network.
*/
struct fann_layer
{
/* A pointer to the first neuron in the layer
* When allocated, all the neurons in all the layers are actually
* in one long array, this is because we want to easily clear all
* the neurons at once.
*/
struct fann_neuron *first_neuron;
/* A pointer to the neuron past the last neuron in the layer */
/* the number of neurons is last_neuron - first_neuron */
struct fann_neuron *last_neuron;
};
/* Struct: struct fann_error
Structure used to store error-related information, both
<struct fann> and <struct fann_train_data> can be casted to this type.
See also:
<fann_set_error_log>, <fann_get_errno>
*/
struct fann_error
{
enum fann_errno_enum errno_f;
FILE *error_log;
char *errstr;
};
/* Struct: struct fann
The fast artificial neural network (fann) structure.
Data within this structure should never be accessed directly, but only by using the
*fann_get_...* and *fann_set_...* functions.
The fann structure is created using one of the *fann_create_...* functions and each of
the functions which operates on the structure takes *struct fann * ann* as the first parameter.
See also:
<fann_create_standard>, <fann_destroy>
*/
struct fann
{
/* The type of error that last occured. */
enum fann_errno_enum errno_f;
/* Where to log error messages. */
FILE *error_log;
/* A string representation of the last error. */
char *errstr;
/* the learning rate of the network */
float learning_rate;
/* The learning momentum used for backpropagation algorithm. */
float learning_momentum;
/* the connection rate of the network
* between 0 and 1, 1 meaning fully connected
*/
float connection_rate;
/* is 1 if shortcut connections are used in the ann otherwise 0
* Shortcut connections are connections that skip layers.
* A fully connected ann with shortcut connections are a ann where
* neurons have connections to all neurons in all later layers.
*/
enum fann_nettype_enum network_type;
/* pointer to the first layer (input layer) in an array af all the layers,
* including the input and outputlayers
*/
struct fann_layer *first_layer;
/* pointer to the layer past the last layer in an array af all the layers,
* including the input and outputlayers
*/
struct fann_layer *last_layer;
/* Total number of neurons.
* very useful, because the actual neurons are allocated in one long array
*/
unsigned int total_neurons;
/* Number of input neurons (not calculating bias) */
unsigned int num_input;
/* Number of output neurons (not calculating bias) */
unsigned int num_output;
/* The weight array */
fann_type *weights;
/* The connection array */
struct fann_neuron **connections;
/* Used to contain the errors used during training
* Is allocated during first training session,
* which means that if we do not train, it is never allocated.
*/
fann_type *train_errors;
/* Training algorithm used when calling fann_train_on_..
*/
enum fann_train_enum training_algorithm;
#ifdef FIXEDFANN
/* the decimal_point, used for shifting the fix point
* in fixed point integer operatons.
*/
unsigned int decimal_point;
/* the multiplier, used for multiplying the fix point
* in fixed point integer operatons.
* Only used in special cases, since the decimal_point is much faster.
*/
unsigned int multiplier;
/* When in choosen (or in fixed point), the sigmoid function is
* calculated as a stepwise linear function. In the
* activation_results array, the result is saved, and in the
* two values arrays, the values that gives the results are saved.
*/
fann_type sigmoid_results[6];
fann_type sigmoid_values[6];
fann_type sigmoid_symmetric_results[6];
fann_type sigmoid_symmetric_values[6];
#endif
/* Total number of connections.
* very useful, because the actual connections
* are allocated in one long array
*/
unsigned int total_connections;
/* used to store outputs in */
fann_type *output;
/* the number of data used to calculate the mean square error.
*/
unsigned int num_MSE;
/* the total error value.
* the real mean square error is MSE_value/num_MSE
*/
float MSE_value;
/* The number of outputs which would fail (only valid for classification problems)
*/
unsigned int num_bit_fail;
/* The maximum difference between the actual output and the expected output
* which is accepted when counting the bit fails.
* This difference is multiplied by two when dealing with symmetric activation functions,
* so that symmetric and not symmetric activation functions can use the same limit.
*/
fann_type bit_fail_limit;
/* The error function used during training. (default FANN_ERRORFUNC_TANH)
*/
enum fann_errorfunc_enum train_error_function;
/* The stop function used during training. (default FANN_STOPFUNC_MSE)
*/
enum fann_stopfunc_enum train_stop_function;
/* The callback function used during training. (default NULL)
*/
fann_callback_type callback;
/* A pointer to user defined data. (default NULL)
*/
void *user_data;
/* Variables for use with Cascade Correlation */
/* The error must change by at least this
* fraction of its old value to count as a
* significant change.
*/
float cascade_output_change_fraction;
/* No change in this number of epochs will cause
* stagnation.
*/
unsigned int cascade_output_stagnation_epochs;
/* The error must change by at least this
* fraction of its old value to count as a
* significant change.
*/
float cascade_candidate_change_fraction;
/* No change in this number of epochs will cause
* stagnation.
*/
unsigned int cascade_candidate_stagnation_epochs;
/* The current best candidate, which will be installed.
*/
unsigned int cascade_best_candidate;
/* The upper limit for a candidate score
*/
fann_type cascade_candidate_limit;
/* Scale of copied candidate output weights
*/
fann_type cascade_weight_multiplier;
/* Maximum epochs to train the output neurons during cascade training
*/
unsigned int cascade_max_out_epochs;
/* Maximum epochs to train the candidate neurons during cascade training
*/
unsigned int cascade_max_cand_epochs;
/* Minimum epochs to train the output neurons during cascade training
*/
unsigned int cascade_min_out_epochs;
/* Minimum epochs to train the candidate neurons during cascade training
*/
unsigned int cascade_min_cand_epochs;
/* An array consisting of the activation functions used when doing
* cascade training.
*/
enum fann_activationfunc_enum *cascade_activation_functions;
/* The number of elements in the cascade_activation_functions array.
*/
unsigned int cascade_activation_functions_count;
/* An array consisting of the steepnesses used during cascade training.
*/
fann_type *cascade_activation_steepnesses;
/* The number of elements in the cascade_activation_steepnesses array.
*/
unsigned int cascade_activation_steepnesses_count;
/* The number of candidates of each type that will be present.
* The actual number of candidates is then
* cascade_activation_functions_count *
* cascade_activation_steepnesses_count *
* cascade_num_candidate_groups
*/
unsigned int cascade_num_candidate_groups;
/* An array consisting of the score of the individual candidates,
* which is used to decide which candidate is the best
*/
fann_type *cascade_candidate_scores;
/* The number of allocated neurons during cascade correlation algorithms.
* This number might be higher than the actual number of neurons to avoid
* allocating new space too often.
*/
unsigned int total_neurons_allocated;
/* The number of allocated connections during cascade correlation algorithms.
* This number might be higher than the actual number of neurons to avoid
* allocating new space too often.
*/
unsigned int total_connections_allocated;
/* Variables for use with Quickprop training */
/* Decay is used to make the weights not go so high */
float quickprop_decay;
/* Mu is a factor used to increase and decrease the stepsize */
float quickprop_mu;
/* Variables for use with with RPROP training */
/* Tells how much the stepsize should increase during learning */
float rprop_increase_factor;
/* Tells how much the stepsize should decrease during learning */
float rprop_decrease_factor;
/* The minimum stepsize */
float rprop_delta_min;
/* The maximum stepsize */
float rprop_delta_max;
/* The initial stepsize */
float rprop_delta_zero;
/* Defines how much the weights are constrained to smaller values at the beginning */
float sarprop_weight_decay_shift;
/* Decides if the stepsize is too big with regard to the error */
float sarprop_step_error_threshold_factor;
/* Defines how much the stepsize is influenced by the error */
float sarprop_step_error_shift;
/* Defines how much the epoch influences weight decay and noise */
float sarprop_temperature;
/* Current training epoch */
unsigned int sarprop_epoch;
/* Used to contain the slope errors used during batch training
* Is allocated during first training session,
* which means that if we do not train, it is never allocated.
*/
fann_type *train_slopes;
/* The previous step taken by the quickprop/rprop procedures.
* Not allocated if not used.
*/
fann_type *prev_steps;
/* The slope values used by the quickprop/rprop procedures.
* Not allocated if not used.
*/
fann_type *prev_train_slopes;
/* The last delta applied to a connection weight.
* This is used for the momentum term in the backpropagation algorithm.
* Not allocated if not used.
*/
fann_type *prev_weights_deltas;
#ifndef FIXEDFANN
/* Arithmetic mean used to remove steady component in input data. */
float *scale_mean_in;
/* Standart deviation used to normalize input data (mostly to [-1;1]). */
float *scale_deviation_in;
/* User-defined new minimum for input data.
* Resulting data values may be less than user-defined minimum.
*/
float *scale_new_min_in;
/* Used to scale data to user-defined new maximum for input data.
* Resulting data values may be greater than user-defined maximum.
*/
float *scale_factor_in;
/* Arithmetic mean used to remove steady component in output data. */
float *scale_mean_out;
/* Standart deviation used to normalize output data (mostly to [-1;1]). */
float *scale_deviation_out;
/* User-defined new minimum for output data.
* Resulting data values may be less than user-defined minimum.
*/
float *scale_new_min_out;
/* Used to scale data to user-defined new maximum for output data.
* Resulting data values may be greater than user-defined maximum.
*/
float *scale_factor_out;
#endif
};
/* Type: fann_connection
Describes a connection between two neurons and its weight
from_neuron - Unique number used to identify source neuron
to_neuron - Unique number used to identify destination neuron
weight - The numerical value of the weight
See Also:
<fann_get_connection_array>, <fann_set_weight_array>
This structure appears in FANN >= 2.1.0
*/
struct fann_connection
{
/* Unique number used to identify source neuron */
unsigned int from_neuron;
/* Unique number used to identify destination neuron */
unsigned int to_neuron;
/* The numerical value of the weight */
fann_type weight;
};
#endif

View File

@ -0,0 +1,303 @@
/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2016 Steffen Nissen (steffen.fann@gmail.com)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef FANN_FANN_DATA_CPP_H_H
#define FANN_FANN_DATA_CPP_H_H
#include <stdarg.h>
#include <string>
/* Section: FANN C++ Datatypes
This section includes enums and helper data types used by the two main classes <neural_net> and <training_data>
*/
/* Type: fann_type
fann_type is the type used for the weights, inputs and outputs of the neural network.
fann_type is defined as a:
float - if you include fann.h or floatfann.h
double - if you include doublefann.h
int - if you include fixedfann.h (please be aware that fixed point usage is
only to be used during execution, and not during training).
*/
namespace FANN {
/* Enum: error_function_enum
Error function used during training.
ERRORFUNC_LINEAR - Standard linear error function.
ERRORFUNC_TANH - Tanh error function, usually better
but can require a lower learning rate. This error function aggressively targets outputs that
differ much from the desired, while not targeting outputs that only differ a little that much.
This activation function is not recommended for cascade training and incremental training.
See also:
<neural_net::set_train_error_function>, <neural_net::get_train_error_function>
*/
enum error_function_enum {
ERRORFUNC_LINEAR = FANN_ERRORFUNC_LINEAR,
ERRORFUNC_TANH
};
/* Enum: stop_function_enum
Stop criteria used during training.
STOPFUNC_MSE - Stop criteria is Mean Square Error (MSE) value.
STOPFUNC_BIT - Stop criteria is number of bits that fail. The number of bits; means the
number of output neurons which differ more than the bit fail limit
(see <neural_net::get_bit_fail_limit>, <neural_net::set_bit_fail_limit>).
The bits are counted in all of the training data, so this number can be higher than
the number of training data.
See also:
<neural_net::set_train_stop_function>, <neural_net::get_train_stop_function>
*/
enum stop_function_enum {
STOPFUNC_MSE = FANN_STOPFUNC_MSE,
STOPFUNC_BIT
};
/* Enum: training_algorithm_enum
The Training algorithms used when training on <training_data> with functions like
<neural_net::train_on_data> or <neural_net::train_on_file>. The incremental training
looks alters the weights after each time it is presented an input pattern, while batch
only alters the weights once after it has been presented to all the patterns.
TRAIN_INCREMENTAL - Standard backpropagation algorithm, where the weights are
updated after each training pattern. This means that the weights are updated many
times during a single epoch. For this reason some problems, will train very fast with
this algorithm, while other more advanced problems will not train very well.
TRAIN_BATCH - Standard backpropagation algorithm, where the weights are updated after
calculating the mean square error for the whole training set. This means that the weights
are only updated once during an epoch. For this reason some problems, will train slower with
this algorithm. But since the mean square error is calculated more correctly than in
incremental training, some problems will reach a better solutions with this algorithm.
TRAIN_RPROP - A more advanced batch training algorithm which achieves good results
for many problems. The RPROP training algorithm is adaptive, and does therefore not
use the learning_rate. Some other parameters can however be set to change the way the
RPROP algorithm works, but it is only recommended for users with insight in how the RPROP
training algorithm works. The RPROP training algorithm is described by
[Riedmiller and Braun, 1993], but the actual learning algorithm used here is the
iRPROP- training algorithm which is described by [Igel and Husken, 2000] which
is a variant of the standard RPROP training algorithm.
TRAIN_QUICKPROP - A more advanced batch training algorithm which achieves good results
for many problems. The quickprop training algorithm uses the learning_rate parameter
along with other more advanced parameters, but it is only recommended to change these
advanced parameters, for users with insight in how the quickprop training algorithm works.
The quickprop training algorithm is described by [Fahlman, 1988].
FANN_TRAIN_SARPROP - THE SARPROP ALGORITHM: A SIMULATED ANNEALING ENHANCEMENT TO RESILIENT BACK PROPAGATION
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.47.8197&rep=rep1&type=pdf
See also:
<neural_net::set_training_algorithm>, <neural_net::get_training_algorithm>
*/
enum training_algorithm_enum {
TRAIN_INCREMENTAL = FANN_TRAIN_INCREMENTAL,
TRAIN_BATCH,
TRAIN_RPROP,
TRAIN_QUICKPROP,
TRAIN_SARPROP
};
/* Enum: activation_function_enum
The activation functions used for the neurons during training. The activation functions
can either be defined for a group of neurons by <neural_net::set_activation_function_hidden>
and <neural_net::set_activation_function_output> or it can be defined for a single neuron by
<neural_net::set_activation_function>.
The steepness of an activation function is defined in the same way by
<neural_net::set_activation_steepness_hidden>, <neural_net::set_activation_steepness_output>
and <neural_net::set_activation_steepness>.
The functions are described with functions where:
* x is the input to the activation function,
* y is the output,
* s is the steepness and
* d is the derivation.
FANN_LINEAR - Linear activation function.
* span: -inf < y < inf
* y = x*s, d = 1*s
* Can NOT be used in fixed point.
FANN_THRESHOLD - Threshold activation function.
* x < 0 -> y = 0, x >= 0 -> y = 1
* Can NOT be used during training.
FANN_THRESHOLD_SYMMETRIC - Threshold activation function.
* x < 0 -> y = 0, x >= 0 -> y = 1
* Can NOT be used during training.
FANN_SIGMOID - Sigmoid activation function.
* One of the most used activation functions.
* span: 0 < y < 1
* y = 1/(1 + exp(-2*s*x))
* d = 2*s*y*(1 - y)
FANN_SIGMOID_STEPWISE - Stepwise linear approximation to sigmoid.
* Faster than sigmoid but a bit less precise.
FANN_SIGMOID_SYMMETRIC - Symmetric sigmoid activation function, aka. tanh.
* One of the most used activation functions.
* span: -1 < y < 1
* y = tanh(s*x) = 2/(1 + exp(-2*s*x)) - 1
* d = s*(1-(y*y))
FANN_SIGMOID_SYMMETRIC - Stepwise linear approximation to symmetric sigmoid.
* Faster than symmetric sigmoid but a bit less precise.
FANN_GAUSSIAN - Gaussian activation function.
* 0 when x = -inf, 1 when x = 0 and 0 when x = inf
* span: 0 < y < 1
* y = exp(-x*s*x*s)
* d = -2*x*s*y*s
FANN_GAUSSIAN_SYMMETRIC - Symmetric gaussian activation function.
* -1 when x = -inf, 1 when x = 0 and 0 when x = inf
* span: -1 < y < 1
* y = exp(-x*s*x*s)*2-1
* d = -2*x*s*(y+1)*s
FANN_ELLIOT - Fast (sigmoid like) activation function defined by David Elliott
* span: 0 < y < 1
* y = ((x*s) / 2) / (1 + |x*s|) + 0.5
* d = s*1/(2*(1+|x*s|)*(1+|x*s|))
FANN_ELLIOT_SYMMETRIC - Fast (symmetric sigmoid like) activation function defined by David Elliott
* span: -1 < y < 1
* y = (x*s) / (1 + |x*s|)
* d = s*1/((1+|x*s|)*(1+|x*s|))
FANN_LINEAR_PIECE - Bounded linear activation function.
* span: 0 < y < 1
* y = x*s, d = 1*s
FANN_LINEAR_PIECE_SYMMETRIC - Bounded Linear activation function.
* span: -1 < y < 1
* y = x*s, d = 1*s
FANN_SIN_SYMMETRIC - Periodical sinus activation function.
* span: -1 <= y <= 1
* y = sin(x*s)
* d = s*cos(x*s)
FANN_COS_SYMMETRIC - Periodical cosinus activation function.
* span: -1 <= y <= 1
* y = cos(x*s)
* d = s*-sin(x*s)
See also:
<neural_net::set_activation_function_hidden>,
<neural_net::set_activation_function_output>
*/
enum activation_function_enum {
LINEAR = FANN_LINEAR,
THRESHOLD,
THRESHOLD_SYMMETRIC,
SIGMOID,
SIGMOID_STEPWISE,
SIGMOID_SYMMETRIC,
SIGMOID_SYMMETRIC_STEPWISE,
GAUSSIAN,
GAUSSIAN_SYMMETRIC,
GAUSSIAN_STEPWISE,
ELLIOT,
ELLIOT_SYMMETRIC,
LINEAR_PIECE,
LINEAR_PIECE_SYMMETRIC,
SIN_SYMMETRIC,
COS_SYMMETRIC
};
/* Enum: network_type_enum
Definition of network types used by <neural_net::get_network_type>
LAYER - Each layer only has connections to the next layer
SHORTCUT - Each layer has connections to all following layers
See Also:
<neural_net::get_network_type>, <fann_get_network_type>
This enumeration appears in FANN >= 2.1.0
*/
enum network_type_enum {
LAYER = FANN_NETTYPE_LAYER,
SHORTCUT
};
/* Type: connection
Describes a connection between two neurons and its weight
from_neuron - Unique number used to identify source neuron
to_neuron - Unique number used to identify destination neuron
weight - The numerical value of the weight
See Also:
<neural_net::get_connection_array>, <neural_net::set_weight_array>
This structure appears in FANN >= 2.1.0
*/
typedef struct fann_connection connection;
/* Forward declaration of class neural_net and training_data */
class neural_net;
class training_data;
/* Type: callback_type
This callback function can be called during training when using <neural_net::train_on_data>,
<neural_net::train_on_file> or <neural_net::cascadetrain_on_data>.
>typedef int (*callback_type) (neural_net &net, training_data &train,
> unsigned int max_epochs, unsigned int epochs_between_reports,
> float desired_error, unsigned int epochs, void *user_data);
The callback can be set by using <neural_net::set_callback> and is very useful for doing custom
things during training. It is recommended to use this function when implementing custom
training procedures, or when visualizing the training in a GUI etc. The parameters which the
callback function takes is the parameters given to the <neural_net::train_on_data>, plus an epochs
parameter which tells how many epochs the training have taken so far.
The callback function should return an integer, if the callback function returns -1, the training
will terminate.
Example of a callback function that prints information to cout:
>int print_callback(FANN::neural_net &net, FANN::training_data &train,
> unsigned int max_epochs, unsigned int epochs_between_reports,
> float desired_error, unsigned int epochs, void *user_data)
>{
> cout << "Epochs " << setw(8) << epochs << ". "
> << "Current Error: " << left << net.get_MSE() << right << endl;
> return 0;
>}
See also:
<neural_net::set_callback>, <fann_callback_type>
*/
typedef int (*callback_type)(neural_net &net, training_data &train,
unsigned int max_epochs, unsigned int epochs_between_reports,
float desired_error, unsigned int epochs, void *user_data);
}
#endif //FANN_FANN_DATA_CPP_H_H

View File

@ -0,0 +1,167 @@
/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2016 Steffen Nissen (steffen.fann@gmail.com)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __fann_error_h__
#define __fann_error_h__
#include <stdio.h>
#define FANN_ERRSTR_MAX 128
struct fann_error;
/* Section: FANN Error Handling
Errors from the fann library are usually reported on stderr.
It is however possible to redirect these error messages to a file,
or completely ignore them by the <fann_set_error_log> function.
It is also possible to inspect the last error message by using the
<fann_get_errno> and <fann_get_errstr> functions.
*/
/* Enum: fann_errno_enum
Used to define error events on <struct fann> and <struct fann_train_data>.
See also:
<fann_get_errno>, <fann_reset_errno>, <fann_get_errstr>
FANN_E_NO_ERROR - No error
FANN_E_CANT_OPEN_CONFIG_R - Unable to open configuration file for reading
FANN_E_CANT_OPEN_CONFIG_W - Unable to open configuration file for writing
FANN_E_WRONG_CONFIG_VERSION - Wrong version of configuration file
FANN_E_CANT_READ_CONFIG - Error reading info from configuration file
FANN_E_CANT_READ_NEURON - Error reading neuron info from configuration file
FANN_E_CANT_READ_CONNECTIONS - Error reading connections from configuration file
FANN_E_WRONG_NUM_CONNECTIONS - Number of connections not equal to the number expected
FANN_E_CANT_OPEN_TD_W - Unable to open train data file for writing
FANN_E_CANT_OPEN_TD_R - Unable to open train data file for reading
FANN_E_CANT_READ_TD - Error reading training data from file
FANN_E_CANT_ALLOCATE_MEM - Unable to allocate memory
FANN_E_CANT_TRAIN_ACTIVATION - Unable to train with the selected activation function
FANN_E_CANT_USE_ACTIVATION - Unable to use the selected activation function
FANN_E_TRAIN_DATA_MISMATCH - Irreconcilable differences between two <struct fann_train_data> structures
FANN_E_CANT_USE_TRAIN_ALG - Unable to use the selected training algorithm
FANN_E_TRAIN_DATA_SUBSET - Trying to take subset which is not within the training set
FANN_E_INDEX_OUT_OF_BOUND - Index is out of bound
FANN_E_SCALE_NOT_PRESENT - Scaling parameters not present
FANN_E_INPUT_NO_MATCH - The number of input neurons in the ann and data don't match
FANN_E_OUTPUT_NO_MATCH - The number of output neurons in the ann and data don't match
FANN_E_WRONG_PARAMETERS_FOR_CREATE - The parameters for create_standard are wrong, either too few parameters provided or a negative/very high value provided
*/
enum fann_errno_enum
{
FANN_E_NO_ERROR = 0,
FANN_E_CANT_OPEN_CONFIG_R,
FANN_E_CANT_OPEN_CONFIG_W,
FANN_E_WRONG_CONFIG_VERSION,
FANN_E_CANT_READ_CONFIG,
FANN_E_CANT_READ_NEURON,
FANN_E_CANT_READ_CONNECTIONS,
FANN_E_WRONG_NUM_CONNECTIONS,
FANN_E_CANT_OPEN_TD_W,
FANN_E_CANT_OPEN_TD_R,
FANN_E_CANT_READ_TD,
FANN_E_CANT_ALLOCATE_MEM,
FANN_E_CANT_TRAIN_ACTIVATION,
FANN_E_CANT_USE_ACTIVATION,
FANN_E_TRAIN_DATA_MISMATCH,
FANN_E_CANT_USE_TRAIN_ALG,
FANN_E_TRAIN_DATA_SUBSET,
FANN_E_INDEX_OUT_OF_BOUND,
FANN_E_SCALE_NOT_PRESENT,
FANN_E_INPUT_NO_MATCH,
FANN_E_OUTPUT_NO_MATCH,
FANN_E_WRONG_PARAMETERS_FOR_CREATE
};
/* Group: Error Handling */
/* Function: fann_set_error_log
Change where errors are logged to. Both <struct fann> and <struct fann_data> can be
casted to <struct fann_error>, so this function can be used to set either of these.
If log_file is NULL, no errors will be printed.
If errdat is NULL, the default log will be set. The default log is the log used when creating
<struct fann> and <struct fann_data>. This default log will also be the default for all new structs
that are created.
The default behavior is to log them to stderr.
See also:
<struct fann_error>
This function appears in FANN >= 1.1.0.
*/
FANN_EXTERNAL void FANN_API fann_set_error_log(struct fann_error *errdat, FILE * log_file);
/* Function: fann_get_errno
Returns the last error number.
See also:
<fann_errno_enum>, <fann_reset_errno>
This function appears in FANN >= 1.1.0.
*/
FANN_EXTERNAL enum fann_errno_enum FANN_API fann_get_errno(struct fann_error *errdat);
/* Function: fann_reset_errno
Resets the last error number.
This function appears in FANN >= 1.1.0.
*/
FANN_EXTERNAL void FANN_API fann_reset_errno(struct fann_error *errdat);
/* Function: fann_reset_errstr
Resets the last error string.
This function appears in FANN >= 1.1.0.
*/
FANN_EXTERNAL void FANN_API fann_reset_errstr(struct fann_error *errdat);
/* Function: fann_get_errstr
Returns the last errstr.
This function calls <fann_reset_errno> and <fann_reset_errstr>
This function appears in FANN >= 1.1.0.
*/
FANN_EXTERNAL char *FANN_API fann_get_errstr(struct fann_error *errdat);
/* Function: fann_print_error
Prints the last error to stderr.
This function appears in FANN >= 1.1.0.
*/
FANN_EXTERNAL void FANN_API fann_print_error(struct fann_error *errdat);
FANN_EXTERNAL extern FILE * fann_default_error_log;
#endif

View File

@ -0,0 +1,155 @@
/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2016 Steffen Nissen (steffen.fann@gmail.com)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __fann_internal_h__
#define __fann_internal_h__
/* internal include file, not to be included directly
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "fann_data.h"
#define FANN_FIX_VERSION "FANN_FIX_2.0"
#define FANN_FLO_VERSION "FANN_FLO_2.1"
#ifdef FIXEDFANN
#define FANN_CONF_VERSION FANN_FIX_VERSION
#else
#define FANN_CONF_VERSION FANN_FLO_VERSION
#endif
#define FANN_GET(type, name) \
FANN_EXTERNAL type FANN_API fann_get_ ## name(struct fann *ann) \
{ \
return ann->name; \
}
#define FANN_SET(type, name) \
FANN_EXTERNAL void FANN_API fann_set_ ## name(struct fann *ann, type value) \
{ \
ann->name = value; \
}
#define FANN_GET_SET(type, name) \
FANN_GET(type, name) \
FANN_SET(type, name)
struct fann_train_data;
struct fann *fann_allocate_structure(unsigned int num_layers);
void fann_allocate_neurons(struct fann *ann);
void fann_allocate_connections(struct fann *ann);
int fann_save_internal(struct fann *ann, const char *configuration_file,
unsigned int save_as_fixed);
int fann_save_internal_fd(struct fann *ann, FILE * conf, const char *configuration_file,
unsigned int save_as_fixed);
int fann_save_train_internal(struct fann_train_data *data, const char *filename,
unsigned int save_as_fixed, unsigned int decimal_point);
int fann_save_train_internal_fd(struct fann_train_data *data, FILE * file, const char *filename,
unsigned int save_as_fixed, unsigned int decimal_point);
void fann_update_stepwise(struct fann *ann);
void fann_seed_rand();
void fann_error(struct fann_error *errdat, const enum fann_errno_enum errno_f, ...);
void fann_init_error_data(struct fann_error *errdat);
struct fann *fann_create_from_fd(FILE * conf, const char *configuration_file);
struct fann_train_data *fann_read_train_from_fd(FILE * file, const char *filename);
void fann_compute_MSE(struct fann *ann, fann_type * desired_output);
void fann_update_output_weights(struct fann *ann);
void fann_backpropagate_MSE(struct fann *ann);
void fann_update_weights(struct fann *ann);
void fann_update_slopes_batch(struct fann *ann, struct fann_layer *layer_begin,
struct fann_layer *layer_end);
void fann_update_weights_quickprop(struct fann *ann, unsigned int num_data,
unsigned int first_weight, unsigned int past_end);
void fann_update_weights_batch(struct fann *ann, unsigned int num_data, unsigned int first_weight,
unsigned int past_end);
void fann_update_weights_irpropm(struct fann *ann, unsigned int first_weight,
unsigned int past_end);
void fann_update_weights_sarprop(struct fann *ann, unsigned int epoch, unsigned int first_weight,
unsigned int past_end);
void fann_clear_train_arrays(struct fann *ann);
fann_type fann_activation(struct fann * ann, unsigned int activation_function, fann_type steepness,
fann_type value);
fann_type fann_activation_derived(unsigned int activation_function,
fann_type steepness, fann_type value, fann_type sum);
int fann_desired_error_reached(struct fann *ann, float desired_error);
/* Some functions for cascade */
int fann_train_outputs(struct fann *ann, struct fann_train_data *data, float desired_error);
float fann_train_outputs_epoch(struct fann *ann, struct fann_train_data *data);
int fann_train_candidates(struct fann *ann, struct fann_train_data *data);
fann_type fann_train_candidates_epoch(struct fann *ann, struct fann_train_data *data);
void fann_install_candidate(struct fann *ann);
int fann_check_input_output_sizes(struct fann *ann, struct fann_train_data *data);
int fann_initialize_candidates(struct fann *ann);
void fann_set_shortcut_connections(struct fann *ann);
int fann_allocate_scale(struct fann *ann);
FANN_EXTERNAL void FANN_API fann_scale_data_to_range(fann_type ** data, unsigned int num_data, unsigned int num_elem,
fann_type old_min, fann_type old_max, fann_type new_min, fann_type new_max);
/* called fann_max, in order to not interferre with predefined versions of max */
#define fann_max(x, y) (((x) > (y)) ? (x) : (y))
#define fann_min(x, y) (((x) < (y)) ? (x) : (y))
#define fann_safe_free(x) {if(x) { free(x); x = NULL; }}
#define fann_clip(x, lo, hi) (((x) < (lo)) ? (lo) : (((x) > (hi)) ? (hi) : (x)))
#define fann_exp2(x) exp(0.69314718055994530942*(x))
/*#define fann_clip(x, lo, hi) (x)*/
#define fann_rand(min_value, max_value) (((float)(min_value))+(((float)(max_value)-((float)(min_value)))*rand()/(RAND_MAX+1.0f)))
#define fann_abs(value) (((value) > 0) ? (value) : -(value))
#ifdef FIXEDFANN
#define fann_mult(x,y) ((x*y) >> decimal_point)
#define fann_div(x,y) (((x) << decimal_point)/y)
#define fann_random_weight() (fann_type)(fann_rand(0,multiplier/10))
#define fann_random_bias_weight() (fann_type)(fann_rand((0-multiplier)/10,multiplier/10))
#else
#define fann_mult(x,y) (x*y)
#define fann_div(x,y) (x/y)
#define fann_random_weight() (fann_rand(-0.1f,0.1f))
#define fann_random_bias_weight() (fann_rand(-0.1f,0.1f))
#endif
#endif

View File

@ -0,0 +1,100 @@
/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2016 Steffen Nissen (steffen.fann@gmail.com)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __fann_io_h__
#define __fann_io_h__
/* Section: FANN File Input/Output
It is possible to save an entire ann to a file with <fann_save> for future loading with <fann_create_from_file>.
*/
/* Group: File Input and Output */
/* Function: fann_create_from_file
Constructs a backpropagation neural network from a configuration file, which has been saved by <fann_save>.
See also:
<fann_save>, <fann_save_to_fixed>
This function appears in FANN >= 1.0.0.
*/
FANN_EXTERNAL struct fann *FANN_API fann_create_from_file(const char *configuration_file);
/* Function: fann_save
Save the entire network to a configuration file.
The configuration file contains all information about the neural network and enables
<fann_create_from_file> to create an exact copy of the neural network and all of the
parameters associated with the neural network.
These three parameters (<fann_set_callback>, <fann_set_error_log>,
<fann_set_user_data>) are *NOT* saved to the file because they cannot safely be
ported to a different location. Also temporary parameters generated during training
like <fann_get_MSE> are not saved.
Return:
The function returns 0 on success and -1 on failure.
See also:
<fann_create_from_file>, <fann_save_to_fixed>
This function appears in FANN >= 1.0.0.
*/
FANN_EXTERNAL int FANN_API fann_save(struct fann *ann, const char *configuration_file);
/* Function: fann_save_to_fixed
Saves the entire network to a configuration file.
But it is saved in fixed point format no matter which
format it is currently in.
This is useful for training a network in floating points,
and then later executing it in fixed point.
The function returns the bit position of the fix point, which
can be used to find out how accurate the fixed point network will be.
A high value indicates high precision, and a low value indicates low
precision.
A negative value indicates very low precision, and a very
strong possibility for overflow.
(the actual fix point will be set to 0, since a negative
fix point does not make sense).
Generally, a fix point lower than 6 is bad, and should be avoided.
The best way to avoid this, is to have less connections to each neuron,
or just less neurons in each layer.
The fixed point use of this network is only intended for use on machines that
have no floating point processor, like an iPAQ. On normal computers the floating
point version is actually faster.
See also:
<fann_create_from_file>, <fann_save>
This function appears in FANN >= 1.0.0.
*/
FANN_EXTERNAL int FANN_API fann_save_to_fixed(struct fann *ann, const char *configuration_file);
#endif

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,561 @@
/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2016 Steffen Nissen (steffen.fann@gmail.com)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef FANN_FANN_TRAINING_DATA_CPP_H
#define FANN_FANN_TRAINING_DATA_CPP_H
#include <stdarg.h>
#include <string>
namespace FANN {
/* Section: FANN C++ Training Data
*/
/* Class: training_data
<training_data> is used to create and manipulate training data used by the <neural_net>
Encapsulation of a training data set <struct fann_train_data> and
associated C API functions.
*/
class training_data {
public:
/* Constructor: training_data
Default constructor creates an empty training data.
Use <read_train_from_file>, <set_train_data> or <create_train_from_callback> to initialize.
*/
training_data() : train_data(NULL) {
}
/* Constructor: training_data
Copy constructor constructs a copy of the training data.
Corresponds to the C API <fann_duplicate_train_data> function.
*/
training_data(const training_data &data) {
train_data = fann_duplicate_train_data(data.train_data);
}
/* Destructor: ~training_data
Provides automatic cleanup of data.
Define USE_VIRTUAL_DESTRUCTOR if you need the destructor to be virtual.
See also:
<destroy>
*/
#ifdef USE_VIRTUAL_DESTRUCTOR
virtual
#endif
~training_data() {
destroy_train();
}
/* Method: destroy
Destructs the training data. Called automatically by the destructor.
See also:
<~training_data>
*/
void destroy_train() {
if (train_data != NULL) {
fann_destroy_train(train_data);
train_data = NULL;
}
}
/* Method: read_train_from_file
Reads a file that stores training data.
The file must be formatted like:
>num_train_data num_input num_output
>inputdata seperated by space
>outputdata seperated by space
>
>.
>.
>.
>
>inputdata seperated by space
>outputdata seperated by space
See also:
<neural_net::train_on_data>, <save_train>, <fann_read_train_from_file>
This function appears in FANN >= 1.0.0
*/
bool read_train_from_file(const std::string &filename) {
destroy_train();
train_data = fann_read_train_from_file(filename.c_str());
return (train_data != NULL);
}
/* Method: save_train
Save the training structure to a file, with the format as specified in <read_train_from_file>
Return:
The function returns true on success and false on failure.
See also:
<read_train_from_file>, <save_train_to_fixed>, <fann_save_train>
This function appears in FANN >= 1.0.0.
*/
bool save_train(const std::string &filename) {
if (train_data == NULL) {
return false;
}
if (fann_save_train(train_data, filename.c_str()) == -1) {
return false;
}
return true;
}
/* Method: save_train_to_fixed
Saves the training structure to a fixed point data file.
This function is very useful for testing the quality of a fixed point network.
Return:
The function returns true on success and false on failure.
See also:
<save_train>, <fann_save_train_to_fixed>
This function appears in FANN >= 1.0.0.
*/
bool save_train_to_fixed(const std::string &filename, unsigned int decimal_point) {
if (train_data == NULL) {
return false;
}
if (fann_save_train_to_fixed(train_data, filename.c_str(), decimal_point) == -1) {
return false;
}
return true;
}
/* Method: shuffle_train_data
Shuffles training data, randomizing the order.
This is recommended for incremental training, while it have no influence during batch training.
This function appears in FANN >= 1.1.0.
*/
void shuffle_train_data() {
if (train_data != NULL) {
fann_shuffle_train_data(train_data);
}
}
/* Method: merge_train_data
Merges the data into the data contained in the <training_data>.
This function appears in FANN >= 1.1.0.
*/
void merge_train_data(const training_data &data) {
fann_train_data *new_data = fann_merge_train_data(train_data, data.train_data);
if (new_data != NULL) {
destroy_train();
train_data = new_data;
}
}
/* Method: length_train_data
Returns the number of training patterns in the <training_data>.
See also:
<num_input_train_data>, <num_output_train_data>, <fann_length_train_data>
This function appears in FANN >= 2.0.0.
*/
unsigned int length_train_data() {
if (train_data == NULL) {
return 0;
}
else {
return fann_length_train_data(train_data);
}
}
/* Method: num_input_train_data
Returns the number of inputs in each of the training patterns in the <training_data>.
See also:
<num_output_train_data>, <length_train_data>, <fann_num_input_train_data>
This function appears in FANN >= 2.0.0.
*/
unsigned int num_input_train_data() {
if (train_data == NULL) {
return 0;
}
else {
return fann_num_input_train_data(train_data);
}
}
/* Method: num_output_train_data
Returns the number of outputs in each of the training patterns in the <struct fann_train_data>.
See also:
<num_input_train_data>, <length_train_data>, <fann_num_output_train_data>
This function appears in FANN >= 2.0.0.
*/
unsigned int num_output_train_data() {
if (train_data == NULL) {
return 0;
}
else {
return fann_num_output_train_data(train_data);
}
}
/* Method: get_input
Grant access to the encapsulated data since many situations
and applications creates the data from sources other than files
or uses the training data for testing and related functions
Returns:
A pointer to the array of input training data
See also:
<get_output>, <set_train_data>
This function appears in FANN >= 2.0.0.
*/
fann_type **get_input() {
if (train_data == NULL) {
return NULL;
}
else {
return train_data->input;
}
}
/* Method: get_output
Grant access to the encapsulated data since many situations
and applications creates the data from sources other than files
or uses the training data for testing and related functions
Returns:
A pointer to the array of output training data
See also:
<get_input>, <set_train_data>
This function appears in FANN >= 2.0.0.
*/
fann_type **get_output() {
if (train_data == NULL) {
return NULL;
}
else {
return train_data->output;
}
}
/* Method: get_train_input
Gets the training input data at the given position
Returns:
A pointer to the array of input training data at the given position
See also:
<get_train_output>, <set_train_data>
This function appears in FANN >= 2.3.0.
*/
fann_type *get_train_input(unsigned int position) {
return fann_get_train_input(train_data, position);
}
/* Method: get_train_output
Gets the training output data at the given position
Returns:
A pointer to the array of output training data at the given position
See also:
<get_train_input>
This function appears in FANN >= 2.3.0.
*/
fann_type *get_train_output(unsigned int position) {
return fann_get_train_output(train_data, position);
}
/* Method: set_train_data
Set the training data to the input and output data provided.
A copy of the data is made so there are no restrictions on the
allocation of the input/output data and the caller is responsible
for the deallocation of the data pointed to by input and output.
Parameters:
num_data - The number of training data
num_input - The number of inputs per training data
num_output - The number of ouputs per training data
input - The set of inputs (a pointer to an array of pointers to arrays of floating point data)
output - The set of desired outputs (a pointer to an array of pointers to arrays of floating point data)
See also:
<get_input>, <get_output>
*/
void set_train_data(unsigned int num_data,
unsigned int num_input, fann_type **input,
unsigned int num_output, fann_type **output) {
set_train_data(fann_create_train_pointer_array(num_data, num_input, input, num_output, output));
}
/* Method: set_train_data
Set the training data to the input and output data provided.
A copy of the data is made so there are no restrictions on the
allocation of the input/output data and the caller is responsible
for the deallocation of the data pointed to by input and output.
Parameters:
num_data - The number of training data
num_input - The number of inputs per training data
num_output - The number of ouputs per training data
input - The set of inputs (an array with the dimension num_data*num_input)
output - The set of desired outputs (an array with the dimension num_data*num_output)
See also:
<get_input>, <get_output>
*/
void set_train_data(unsigned int num_data,
unsigned int num_input, fann_type *input,
unsigned int num_output, fann_type *output) {
set_train_data(fann_create_train_array(num_data, num_input, input, num_output, output));
}
private:
/* Set the training data to the struct fann_training_data pointer.
The struct has to be allocated with malloc to be compatible
with fann_destroy. */
void set_train_data(struct fann_train_data *data) {
destroy_train();
train_data = data;
}
public:
/*********************************************************************/
/* Method: create_train_from_callback
Creates the training data struct from a user supplied function.
As the training data are numerable (data 1, data 2...), the user must write
a function that receives the number of the training data set (input,output)
and returns the set.
Parameters:
num_data - The number of training data
num_input - The number of inputs per training data
num_output - The number of ouputs per training data
user_function - The user suplied function
Parameters for the user function:
num - The number of the training data set
num_input - The number of inputs per training data
num_output - The number of ouputs per training data
input - The set of inputs
output - The set of desired outputs
See also:
<training_data::read_train_from_file>, <neural_net::train_on_data>,
<fann_create_train_from_callback>
This function appears in FANN >= 2.1.0
*/
void create_train_from_callback(unsigned int num_data,
unsigned int num_input,
unsigned int num_output,
void (FANN_API *user_function)(unsigned int,
unsigned int,
unsigned int,
fann_type *,
fann_type *)) {
destroy_train();
train_data = fann_create_train_from_callback(num_data, num_input, num_output, user_function);
}
#ifndef FIXEDFANN
/* Function: get_min_input
Get the minimum value of all in the input data
This function appears in FANN >= 2.3.0
*/
fann_type get_min_input() {
return fann_get_min_train_input(train_data);
}
/* Function: get_max_input
Get the maximum value of all in the input data
This function appears in FANN >= 2.3.0
*/
fann_type get_max_input() {
return fann_get_max_train_input(train_data);
}
/* Function: get_min_output
Get the minimum value of all in the output data
This function appears in FANN >= 2.3.0
*/
fann_type get_min_output() {
return fann_get_min_train_output(train_data);
}
/* Function: get_max_output
Get the maximum value of all in the output data
This function appears in FANN >= 2.3.0
*/
fann_type get_max_output() {
return fann_get_max_train_output(train_data);
}
#endif /* FIXEDFANN */
/* Method: scale_input_train_data
Scales the inputs in the training data to the specified range.
A simplified scaling method, which is mostly useful in examples where it's known that all the
data will be in one range and it should be transformed to another range.
It is not recommended to use this on subsets of data as the complete input range might not be
available in that subset.
For more powerful scaling, please consider <neural_net::scale_train>
See also:
<scale_output_train_data>, <scale_train_data>, <fann_scale_input_train_data>
This function appears in FANN >= 2.0.0.
*/
void scale_input_train_data(fann_type new_min, fann_type new_max) {
if (train_data != NULL) {
fann_scale_input_train_data(train_data, new_min, new_max);
}
}
/* Method: scale_output_train_data
Scales the outputs in the training data to the specified range.
A simplified scaling method, which is mostly useful in examples where it's known that all the
data will be in one range and it should be transformed to another range.
It is not recommended to use this on subsets of data as the complete input range might not be
available in that subset.
For more powerful scaling, please consider <neural_net::scale_train>
See also:
<scale_input_train_data>, <scale_train_data>, <fann_scale_output_train_data>
This function appears in FANN >= 2.0.0.
*/
void scale_output_train_data(fann_type new_min, fann_type new_max) {
if (train_data != NULL) {
fann_scale_output_train_data(train_data, new_min, new_max);
}
}
/* Method: scale_train_data
Scales the inputs and outputs in the training data to the specified range.
A simplified scaling method, which is mostly useful in examples where it's known that all the
data will be in one range and it should be transformed to another range.
It is not recommended to use this on subsets of data as the complete input range might not be
available in that subset.
For more powerful scaling, please consider <neural_net::scale_train>
See also:
<scale_output_train_data>, <scale_input_train_data>, <fann_scale_train_data>
This function appears in FANN >= 2.0.0.
*/
void scale_train_data(fann_type new_min, fann_type new_max) {
if (train_data != NULL) {
fann_scale_train_data(train_data, new_min, new_max);
}
}
/* Method: subset_train_data
Changes the training data to a subset, starting at position *pos*
and *length* elements forward. Use the copy constructor to work
on a new copy of the training data.
>FANN::training_data full_data_set;
>full_data_set.read_train_from_file("somefile.train");
>FANN::training_data *small_data_set = new FANN::training_data(full_data_set);
>small_data_set->subset_train_data(0, 2); // Only use first two
>// Use small_data_set ...
>delete small_data_set;
See also:
<fann_subset_train_data>
This function appears in FANN >= 2.0.0.
*/
void subset_train_data(unsigned int pos, unsigned int length) {
if (train_data != NULL) {
struct fann_train_data *temp = fann_subset_train_data(train_data, pos, length);
destroy_train();
train_data = temp;
}
}
/*********************************************************************/
protected:
/* The neural_net class has direct access to the training data */
friend class neural_net;
/* Pointer to the encapsulated training data */
struct fann_train_data *train_data;
};
}
#endif //FANN_FANN_TRAINING_DATA_CPP_H

View File

@ -0,0 +1,33 @@
/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2016 Steffen Nissen (steffen.fann@gmail.com)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __fixedfann_h__
#define __fixedfann_h__
typedef int fann_type;
#undef FIXEDFANN
#define FIXEDFANN
#define FANNPRINTF "%d"
#define FANNSCANF "%d"
#define FANN_INCLUDE
#include "fann.h"
#endif

View File

@ -0,0 +1,33 @@
/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2016 Steffen Nissen (steffen.fann@gmail.com)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __floatfann_h__
#define __floatfann_h__
typedef float fann_type;
#undef FLOATFANN
#define FLOATFANN
#define FANNPRINTF "%.20e"
#define FANNSCANF "%f"
#define FANN_INCLUDE
#include "fann.h"
#endif

View File

@ -0,0 +1,45 @@
/*
* parallel_FANN.h
*
* Author: Alessandro Pietro Bardelli
*/
#ifndef DISABLE_PARALLEL_FANN
#ifndef PARALLEL_FANN_H_
#define PARALLEL_FANN_H_
#include "fann.h"
#ifdef __cplusplus
extern "C"
{
#ifndef __cplusplus
} /* to fool automatic indention engines */
#endif
#endif /* __cplusplus */
#ifndef FIXEDFANN
FANN_EXTERNAL float FANN_API fann_train_epoch_batch_parallel(struct fann *ann, struct fann_train_data *data, const unsigned int threadnumb);
FANN_EXTERNAL float FANN_API fann_train_epoch_irpropm_parallel(struct fann *ann, struct fann_train_data *data, const unsigned int threadnumb);
FANN_EXTERNAL float FANN_API fann_train_epoch_quickprop_parallel(struct fann *ann, struct fann_train_data *data, const unsigned int threadnumb);
FANN_EXTERNAL float FANN_API fann_train_epoch_sarprop_parallel(struct fann *ann, struct fann_train_data *data, const unsigned int threadnumb);
FANN_EXTERNAL float FANN_API fann_train_epoch_incremental_mod(struct fann *ann, struct fann_train_data *data);
FANN_EXTERNAL float FANN_API fann_test_data_parallel(struct fann *ann, struct fann_train_data *data, const unsigned int threadnumb);
#endif /* FIXEDFANN */
#ifdef __cplusplus
#ifndef __cplusplus
/* to fool automatic indention engines */
{
#endif
}
#endif /* __cplusplus */
#endif /* PARALLEL_FANN_H_ */
#endif /* DISABLE_PARALLEL_FANN */

View File

@ -0,0 +1,39 @@
/*
* parallel_FANN.hpp
* Author: Alessandro Pietro Bardelli
*/
#ifndef DISABLE_PARALLEL_FANN
#ifndef PARALLEL_FANN_HPP_
#define PARALLEL_FANN_HPP_
#include <omp.h>
#include <vector>
#include "fann.h"
#ifndef FIXEDFANN
namespace parallel_fann {
float train_epoch_batch_parallel(struct fann *ann, struct fann_train_data *data, const unsigned int threadnumb);
float train_epoch_irpropm_parallel(struct fann *ann, struct fann_train_data *data, const unsigned int threadnumb);
float train_epoch_quickprop_parallel(struct fann *ann, struct fann_train_data *data, const unsigned int threadnumb);
float train_epoch_sarprop_parallel(struct fann *ann, struct fann_train_data *data, const unsigned int threadnumb);
float train_epoch_incremental_mod(struct fann *ann, struct fann_train_data *data);
float train_epoch_batch_parallel(struct fann *ann, struct fann_train_data *data, const unsigned int threadnumb,std::vector< std::vector<fann_type> >& predicted_outputs);
float train_epoch_irpropm_parallel(struct fann *ann, struct fann_train_data *data, const unsigned int threadnumb, std::vector< std::vector<fann_type> >& predicted_outputs);
float train_epoch_quickprop_parallel(struct fann *ann, struct fann_train_data *data, const unsigned int threadnumb, std::vector< std::vector<fann_type> >& predicted_outputs);
float train_epoch_sarprop_parallel(struct fann *ann, struct fann_train_data *data, const unsigned int threadnumb, std::vector< std::vector<fann_type> >& predicted_outputs);
float train_epoch_incremental_mod(struct fann *ann, struct fann_train_data *data, std::vector< std::vector<fann_type> >& predicted_outputs);
float test_data_parallel(struct fann *ann, struct fann_train_data *data, const unsigned int threadnumb);
float test_data_parallel(struct fann *ann, struct fann_train_data *data, const unsigned int threadnumb, std::vector< std::vector<fann_type> >& predicted_outputs);
}
#endif /* FIXEDFANN */
#endif /* PARALLEL_FANN_HPP_ */
#endif /* DISABLE_PARALLEL_FANN */