選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * GENANN - Minimal C Artificial Neural Network
  3. *
  4. * Copyright (c) 2015-2018 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. struct genann;
  37. typedef double (*genann_actfun)(const struct genann *ann, double a);
  38. typedef struct genann {
  39. /* How many inputs, outputs, and hidden neurons. */
  40. int inputs, hidden_layers, hidden, outputs;
  41. /* Which activation function to use for hidden neurons. Default: gennann_act_sigmoid_cached*/
  42. genann_actfun activation_hidden;
  43. /* Which activation function to use for output. Default: gennann_act_sigmoid_cached*/
  44. genann_actfun activation_output;
  45. /* Total number of weights, and size of weights buffer. */
  46. int total_weights;
  47. /* Total number of neurons + inputs and size of output buffer. */
  48. int total_neurons;
  49. /* All weights (total_weights long). */
  50. double *weight;
  51. /* Stores input array and output of each neuron (total_neurons long). */
  52. double *output;
  53. /* Stores delta of each hidden and output neuron (total_neurons - inputs long). */
  54. double *delta;
  55. } genann;
  56. /* Creates and returns a new ann. */
  57. genann *genann_init(int inputs, int hidden_layers, int hidden, int outputs);
  58. /* Creates ANN from file saved with genann_write. */
  59. genann *genann_read(FILE *in);
  60. /* Sets weights randomly. Called by init. */
  61. void genann_randomize(genann *ann);
  62. /* Returns a new copy of ann. */
  63. genann *genann_copy(genann const *ann);
  64. /* Frees the memory used by an ann. */
  65. void genann_free(genann *ann);
  66. /* Runs the feedforward algorithm to calculate the ann's output. */
  67. double const *genann_run(genann const *ann, double const *inputs);
  68. /* Does a single backprop update. */
  69. void genann_train(genann const *ann, double const *inputs, double const *desired_outputs, double learning_rate);
  70. /* Saves the ann. */
  71. void genann_write(genann const *ann, FILE *out);
  72. void genann_init_sigmoid_lookup(const genann *ann);
  73. double genann_act_sigmoid(const genann *ann, double a);
  74. double genann_act_sigmoid_cached(const genann *ann, double a);
  75. double genann_act_threshold(const genann *ann, double a);
  76. double genann_act_linear(const genann *ann, double a);
  77. #ifdef __cplusplus
  78. }
  79. #endif
  80. #endif /*GENANN_H*/