genann.h 3.1 KB

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