UESMANN CPP  1.0
Reference implementation of UESMANN
/home/travis/build/jimfinnis/uesmanncpp/uesnet.hpp
Go to the documentation of this file.
1 
8 #ifndef __UESNET_HPP
9 #define __UESNET_HPP
10 
11 
17 class UESNet: public BPNet {
22  double modulator;
23 public:
28  UESNet(int nlayers,const int *layerCounts) : BPNet(nlayers,layerCounts),
29  modulator(0)
30  {
31  // replace the net type, it's not a plain net any more
33 
34  }
35 
36  virtual void setH(double h){
37  modulator = h;
38  }
39 
40  virtual double getH() const {
41  return modulator;
42  }
43 
44 protected:
45 
46  void calcError(double *in,double *out){
47  // first run the network forwards
48  setInputs(in);
49  update();
50 
51  // first, calculate the error in the output layer
52  // This does the THIRD of the backprop equations, Eq. 4.15, giving dLj.
53  int ol = numLayers-1;
54  for(int i=0;i<layerSizes[ol];i++){
55  double o = outputs[ol][i];
56  errors[ol][i] = o*(1-o)*(o-out[i]);
57  }
58 
59  // then work out the errors in all the other layers
60  // factoring in (rather inefficiently) the hormone.
61  // This is the FOURTH backprop equation, Eq. 4.16.
62  for(int l=1;l<numLayers-1;l++){
63  for(int j=0;j<layerSizes[l];j++){
64  double e = 0;
65  for(int i=0;i<layerSizes[l+1];i++)
66  e += errors[l+1][i]*getw(l+1,i,j);
67 
68  // produce the \delta^l_i term where l is the layer and i
69  // the index of the node. Here is where we factor in the modulator.
70 
71  errors[l][j] = e * (modulator+1.0) * outputs[l][j] * (1-outputs[l][j]);
72  }
73  }
74  }
75 
76  virtual void update(){
77  double hfactor = modulator+1.0;
78  for(int i=1;i<numLayers;i++){
79  for(int j=0;j<layerSizes[i];j++){
80  double v = 0.0;
81  for(int k=0;k<layerSizes[i-1];k++){
82  v += getw(i,j,k) * outputs[i-1][k];
83  }
84  // factor in the hormone here
85  outputs[i][j]=sigmoid(v*hfactor+biases[i][j]);
86  }
87  }
88  }
89 
90  virtual double trainBatch(ExampleSet& ex,int start,int num,double eta){
91  // zero average gradients
92  for(int j=0;j<numLayers;j++){
93  for(int k=0;k<layerSizes[j];k++)
94  gradAvgsBiases[j][k]=0;
95  for(int i=0;i<largestLayerSize*largestLayerSize;i++)
96  gradAvgsWeights[j][i]=0;
97  }
98 
99  // reset total error
100  double totalError=0;
101  // iterate over examples
102  for(int nn=0;nn<num;nn++){
103  int exampleIndex = nn+start;
104  // set modulator
105  setH(ex.getH(exampleIndex));
106  // get outputs for this example
107  double *outs = ex.getOutputs(exampleIndex);
108  // build errors for each example
109  calcError(ex.getInputs(exampleIndex),outs);
110 
111  // accumulate errors
112  for(int l=1;l<numLayers;l++){
113  for(int i=0;i<layerSizes[l];i++){
114  // this does the FIRST of the backprop equations,
115  // Eq. 4.13, calculating dC/dw(h+1), but the modulator
116  // is dealt with below.
117  for(int j=0;j<layerSizes[l-1];j++)
118  getavggradw(l,i,j) += errors[l][i]*outputs[l-1][j];
119  // this does the SECOND of the backprop equations,
120  // Eq. 4.14.
121  gradAvgsBiases[l][i] += errors[l][i];
122  }
123  }
124  // count up the total error
125  int ol = numLayers-1;
126  for(int i=0;i<layerSizes[ol];i++){
127  double o = outputs[ol][i];
128  double e = (o-outs[i]);
129  totalError += e*e;
130  }
131  }
132 
133  // get modulator factor
134  double hfactor = modulator+1.0;
135 
136 
137  // for calculating average error - 1/number of examples trained
138  double factor = 1.0/(double)num;
139  // we now have a full set of running averages. Time to apply them.
140  for(int l=1;l<numLayers;l++){
141  for(int i=0;i<layerSizes[l];i++){
142  for(int j=0;j<layerSizes[l-1];j++){
143  // this does the modulation part of Eq. 4.13, but a little
144  // later than in the thesis.
145  double wdelta = eta*getavggradw(l,i,j)*factor*hfactor;
146  // printf("WCORR: %f factor %f\n",wdelta,getavggradw(l,i,j));
147  getw(l,i,j) -= wdelta;
148  }
149  // biases are not modulated
150  double bdelta = eta*gradAvgsBiases[l][i]*factor;
151  biases[l][i] -= bdelta;
152  }
153  }
154  // and return total error - this is the SUM of the MSE of each output
155  return totalError*factor;
156  }
157 };
158 
159 #endif /* __UESNET_HPP */
virtual void setInputs(double *d)
Set the inputs to the network before running or training.
Definition: bpnet.hpp:104
double ** gradAvgsBiases
average gradient for each bias (built during training)
Definition: bpnet.hpp:216
int numLayers
number of layers, including input and output
Definition: bpnet.hpp:190
double ** biases
array of biases, stored as a rectangular array of [layer][node]
Definition: bpnet.hpp:208
void calcError(double *in, double *out)
Definition: uesnet.hpp:46
double & getavggradw(int tolayer, int toneuron, int fromneuron) const
get the value of the gradient for a given weight
Definition: bpnet.hpp:272
The "basic" back-propagation network using a logistic sigmoid, as described by Rumelhart, Hinton and Williams (and many others). This class is used by output blending and h-as-input networks.
Definition: bpnet.hpp:18
UESNet(int nlayers, const int *layerCounts)
The constructor is mostly identical to the BPNet constructor.
Definition: uesnet.hpp:28
virtual void update()
Run a single update of the network.
Definition: uesnet.hpp:76
double * getOutputs(int example)
Get a pointer to the outputs for a given example, for reading or writing.
Definition: data.hpp:349
double sigmoid(double x)
Definition: net.hpp:20
double & getw(int tolayer, int toneuron, int fromneuron) const
get the value of a weight.
Definition: bpnet.hpp:249
virtual void setH(double h)
Set the modulator level for subsequent runs and training of this network.
Definition: uesnet.hpp:36
double ** outputs
outputs of each layer: one array of doubles for each
Definition: bpnet.hpp:212
double ** errors
the error for each node, calculated by calcError()
Definition: bpnet.hpp:213
NetType type
type of the network, used for load/save
Definition: net.hpp:49
h-as-input
The UESMANN network, which it itself based on the BPNet code as it has the same architecture as the p...
Definition: uesnet.hpp:17
double getH(int example) const
Get the h (modulator) for a given example.
Definition: data.hpp:359
double ** gradAvgsWeights
average gradient for each weight (built during training)
Definition: bpnet.hpp:215
virtual double getH() const
get the modulator level
Definition: uesnet.hpp:40
int largestLayerSize
number of nodes in largest layer
Definition: bpnet.hpp:192
double * getInputs(int example)
Get a pointer to the inputs for a given example, for reading or writing.
Definition: data.hpp:338
virtual double trainBatch(ExampleSet &ex, int start, int num, double eta)
Train a network for batch (or mini-batch) (or single example).
Definition: uesnet.hpp:90
A set of example data. Each datum consists of hormone (i.e. modulator value), inputs and outputs...
Definition: data.hpp:57
int * layerSizes
array of layer sizes
Definition: bpnet.hpp:191