tlegrand@chromium.org | e3ea049 | 2013-10-23 09:13:50 +0000 | [diff] [blame] | 1 | /* Copyright (c) 2008-2011 Octasic Inc. |
| 2 | Written by Jean-Marc Valin */ |
| 3 | /* |
| 4 | Redistribution and use in source and binary forms, with or without |
| 5 | modification, are permitted provided that the following conditions |
| 6 | are met: |
| 7 | |
| 8 | - Redistributions of source code must retain the above copyright |
| 9 | notice, this list of conditions and the following disclaimer. |
| 10 | |
| 11 | - Redistributions in binary form must reproduce the above copyright |
| 12 | notice, this list of conditions and the following disclaimer in the |
| 13 | documentation and/or other materials provided with the distribution. |
| 14 | |
| 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 18 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 19 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 23 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 24 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #ifndef _MLP_TRAIN_H_ |
| 29 | #define _MLP_TRAIN_H_ |
| 30 | |
| 31 | #include <math.h> |
| 32 | #include <stdlib.h> |
| 33 | |
| 34 | double tansig_table[501]; |
| 35 | static inline double tansig_double(double x) |
| 36 | { |
| 37 | return 2./(1.+exp(-2.*x)) - 1.; |
| 38 | } |
| 39 | static inline void build_tansig_table() |
| 40 | { |
| 41 | int i; |
| 42 | for (i=0;i<501;i++) |
| 43 | tansig_table[i] = tansig_double(.04*(i-250)); |
| 44 | } |
| 45 | |
| 46 | static inline double tansig_approx(double x) |
| 47 | { |
| 48 | int i; |
| 49 | double y, dy; |
| 50 | if (x>=10) |
| 51 | return 1; |
| 52 | if (x<=-10) |
| 53 | return -1; |
| 54 | i = lrint(25*x); |
| 55 | x -= .04*i; |
| 56 | y = tansig_table[250+i]; |
| 57 | dy = 1-y*y; |
| 58 | y = y + x*dy*(1 - y*x); |
| 59 | return y; |
| 60 | } |
| 61 | |
| 62 | inline float randn(float sd) |
| 63 | { |
| 64 | float U1, U2, S, x; |
| 65 | do { |
| 66 | U1 = ((float)rand())/RAND_MAX; |
| 67 | U2 = ((float)rand())/RAND_MAX; |
| 68 | U1 = 2*U1-1; |
| 69 | U2 = 2*U2-1; |
| 70 | S = U1*U1 + U2*U2; |
| 71 | } while (S >= 1 || S == 0.0f); |
| 72 | x = sd*sqrt(-2 * log(S) / S) * U1; |
| 73 | return x; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | typedef struct { |
| 78 | int layers; |
| 79 | int *topo; |
| 80 | double **weights; |
| 81 | double **best_weights; |
| 82 | double *in_rate; |
| 83 | } MLPTrain; |
| 84 | |
| 85 | |
| 86 | #endif /* _MLP_TRAIN_H_ */ |