Robot Simulator of the Robotics Group for Self-Organization of Control  0.8.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
regularisation.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2011 LpzRobots development team *
3  * Georg Martius <georg dot martius at web dot de> *
4  * Frank Guettler <guettler at informatik dot uni-leipzig dot de *
5  * Frank Hesse <frank at nld dot ds dot mpg dot de> *
6  * Ralf Der <ralfder at mis dot mpg dot de> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the *
20  * Free Software Foundation, Inc., *
21  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22  * *
23  ***************************************************************************/
24 #ifndef __REGULARISATION_H
25 #define __REGULARISATION_H
26 
27 #include <cmath>
28 #include <selforg/controller_misc.h>
29 
30 double inline sqr(double x) {
31  return x*x;
32 }
33 
34 /// neuron transfer function
35 double inline g(double z)
36 {
37  return tanh(z);
38 };
39 
40 /// first dervative
41 double inline g_s(double z)
42 {
43  double k=tanh(z);
44  return 1.025 - k*k;
45  // return 1/((1+0.5 * z*z)*(1+0.5 * z*z)); // softer
46  //return 1/(1+log(1+z*z)); // even softer
47 };
48 
49 
50 /// first dervative with smoothing for large z
51 double inline g_derivative(double z)
52 {
53  return 1/((1+0.5 * z*z)*(1+0.5 * z*z));
54 };
55 
56 /// inverse of the first derivative
57 double inline g_s_inv(double z)
58 {
59  double k=tanh(z);
60  return 1/(1.025 - k*k);
61  // return 1+z*z; // softer
62  //return 1+log(1+z*z); // even softer
63 };
64 
65 /** \f[ g'(z+xsi) = 1-(tanh(z+xsi))^2 \f] with additional clipping */
66 double inline g_s(double z, double xsi) {
67  double Z = clip(z, -3.0, 3.0) + clip(xsi, -1.0, 1.0);
68  double k=tanh(Z); // approximation with Mittelwertsatz
69  return 1 - k*k;
70 };
71 
72 
73 /** soft version: \f[ g'(z+xsi) = 1/(1+(z+xsi)^2 \f] with additional clipping */
74 double inline g_s_soft(double z, double xsi) {
75  double Z = clip(z, -3.0, 3.0) + clip(xsi, -1.0, 1.0);
76  return 1/(1 + Z*Z);//TEST
77 };
78 
79 
80 /// an exact formula for g''/g'= -2g(Z), with clipped Z = z+xsi
81 double inline g_ss_div_s(double z, double xsi) {
82  // for consistency reasons we use the same clipped z as for g'.
83  double Z = clip(z, -3.0, 3.0) + clip(xsi, -1.0, 1.0);
84  // approximation with Mittelwertsatz (z is clipped)
85  return -2*g(Z);
86 };
87 
88 /// an soft formula for g''/g' = -2Z, with clipped Z = z+xsi
89 double inline g_ss_div_s_soft(double z, double xsi) {
90  // for consistency reasons we use the same clipped z as for g'.
91  double Z = clip(z, -3.0, 3.0) + clip(xsi, -1.0, 1.0);
92  return -2*Z;//TEST
93 };
94 
95 /** with \f[ g'(z) = 1-(g(z+\xi))^2 \f] we get
96  \f[\frac{\partial}{\partial z} \frac{1}{g'(Z)} = \frac{g''}{g'^2} \f]
97  again with clipped Z
98  */
99 double inline derive_g_s_inv_exact_clip(double z, double xsi){
100  double Z = clip(z, -3.0, 3.0) + clip(xsi, -1.0, 1.0);
101  double k=tanh(Z); // approximation with Mittelwertsatz
102  return -2*k/(1-k*k);
103 }
104 
105 /** \f[ g'(z) = 1-(z+\xi)^2 \f] which is the series expansion to the second order
106 */
107 double inline g_s_expand2(double z, double xsi){
108  double Z = z + clip(xsi, -fabs(z), fabs(z));
109  return 1/(1+sqr(Z));
110 }
111 
112 /** \f[ \frac{1}{g'(z)} \approx 1+(z+\xi)^2 \f] with geometric series approximation
113 */
114 double inline g_s_inv_expand2(double z, double xsi){
115  double Z = z + clip(xsi, -fabs(z)/2.0, fabs(z)/2.0);
116  return 1+sqr(Z);
117 }
118 
119 /** \f[ \frac{g''(z)}{g'(z)} \approx 2(z+\xi)(1+(z+\xi)^2) \f] with geometric series approximation
120 */
121 double inline g_ss_div_s_expand2(double z, double xsi){
122  double Z = z + clip(xsi, -fabs(z)/2.0, fabs(z)/2.0);
123  // double Z = z + clip(xsi, -fabs(z), fabs(z));
124  return -2*tanh(Z);
125 }
126 
127 
128  /// squashing function (-0.1 to 0.1), to protect against to large weight updates
129 double inline squash(double z)
130  {
131  return clip(z, -0.1, 0.1);
132  //return 0.1 * tanh(10.0 * z);
133  };
134 
135  /// squashing function with adjustable clipping size, to protect against too large weight updates
136 double inline squash(void* d, double z) {
137  double size = *((double*)d);
138  return clip(z, -size, size);
139  };
140 
141 
142 
143 #endif
144 
double derive_g_s_inv_exact_clip(double z, double xsi)
with we get again with clipped Z
Definition: regularisation.h:99
double g_s_expand2(double z, double xsi)
which is the series expansion to the second order
Definition: regularisation.h:107
Definition: trackablemeasure.h:42
double squash(double z)
squashing function (-0.1 to 0.1), to protect against to large weight updates
Definition: regularisation.h:129
double g_ss_div_s_expand2(double z, double xsi)
with geometric series approximation
Definition: regularisation.h:121
double g_ss_div_s_soft(double z, double xsi)
an soft formula for g''/g' = -2Z, with clipped Z = z+xsi
Definition: regularisation.h:89
double g_derivative(double z)
first dervative with smoothing for large z
Definition: regularisation.h:51
double g_s(double z)
first dervative
Definition: regularisation.h:41
double g_s_inv(double z)
inverse of the first derivative
Definition: regularisation.h:57
double clip(double r, double x)
clipping function for mapP
Definition: controller_misc.cpp:39
double g(double z)
neuron transfer function
Definition: regularisation.h:35
double g_ss_div_s(double z, double xsi)
an exact formula for g''/g'= -2g(Z), with clipped Z = z+xsi
Definition: regularisation.h:81
double g_s_soft(double z, double xsi)
soft version: with additional clipping
Definition: regularisation.h:74
double g_s_inv_expand2(double z, double xsi)
with geometric series approximation
Definition: regularisation.h:114
double sqr(double x)
Definition: regularisation.h:30