Package ML :: Package Neural :: Module ActFuncs
[hide private]
[frames] | no frames]

Source Code for Module ML.Neural.ActFuncs

 1  # 
 2  #  Copyright (C) 2000  greg Landrum 
 3  # 
 4  """ Activation functions for neural network nodes 
 5   
 6  Activation functions should implement the following API: 
 7   
 8   - _Eval(input)_: returns the value of the function at a given point 
 9   
10   - _Deriv(input)_: returns the derivative of the function at a given point 
11   
12  The current Backprop implementation also requires: 
13   
14   - _DerivFromVal(val)_: returns the derivative of the function when its 
15                          value is val 
16   
17  In all cases _input_ is a float as is the value returned. 
18   
19  """ 
20  from Numeric import * 
21   
22     
23 -class ActFunc(object):
24 """ "virtual base class" for activation functions 25 26 """
27 - def __call__(self,input):
28 return self.Eval(input)
29 30
31 -class Sigmoid(ActFunc):
32 """ the standard sigmoidal function """
33 - def Eval(self,input):
34 return 1./(1.+exp(-self.beta*input))
35
36 - def Deriv(self,input):
37 val = self.Eval(input) 38 return self.beta * val * (1. - val)
39
40 - def DerivFromVal(self,val):
41 return self.beta * val * (1. - val)
42
43 - def __init__(self,beta=1.):
44 self.beta=beta
45
46 -class TanH(ActFunc):
47 """ the standard hyperbolic tangent function """
48 - def Eval(self,input):
49 v1 = exp(self.beta*input) 50 v2 = exp(-self.beta*input) 51 return (v1 - v2)/(v1 + v2)
52
53 - def Deriv(self,input):
54 val = self.Eval(input) 55 return self.beta * (1 - val*val)
56
57 - def DerivFromVal(self,val):
58 return self.beta * (1 - val*val)
59
60 - def __init__(self,beta=1.):
61 self.beta = beta
62