UESMANN CPP  1.0
Reference implementation of UESMANN
/home/travis/build/jimfinnis/uesmanncpp/obnet.hpp
Go to the documentation of this file.
1 
7 #ifndef __OBNET_HPP
8 #define __OBNET_HPP
9 
10 #include "data.hpp"
11 
18 class OutputBlendingNet : public Net {
19 private:
23  double modulator;
24 
25 
26 public:
33  OutputBlendingNet(int nlayers,const int *layerCounts)
35  // we create two networks, one for each modulator level.
36  net0 = new BPNet(nlayers,layerCounts);
37  net1 = new BPNet(nlayers,layerCounts);
38  interpolatedOutputs = new double [net0->getOutputCount()];
39  }
40 
44  virtual ~OutputBlendingNet(){
45  delete net0;
46  delete net1;
47  delete [] interpolatedOutputs;
48  }
49 
50  virtual int getLayerSize(int n) const {
51  return net0->getLayerSize(n);
52  }
53 
54  virtual int getLayerCount() const {
55  return net0->getLayerCount();
56  }
57 
58  virtual void setH(double h){
59  modulator = h;
60  }
61 
62  virtual double getH() const {
63  return modulator;
64  }
65 
66 
67 
68  virtual void setInputs(double *d) {
69  // a bit inefficient, since we should only need to do this
70  // for the network currently being trained.
71  net0->setInputs(d);
72  net1->setInputs(d);
73  }
74 
75  virtual double *getOutputs() const {
76  // constructed during the update
77  return interpolatedOutputs;
78  }
79 
80  virtual int getDataSize() const {
81  // need room for the two (equally-sized) nets
82  return net0->getDataSize()*2;
83  }
84 
85  virtual void save(double *buf) const {
86  // just save the two networks, one after the other
87  net0->save(buf);
88  buf+=net0->getDataSize();
89  net1->save(buf);
90  }
91 
92  virtual void load(double *buf){
93  net0->load(buf);
94  buf+=net0->getDataSize();
95  net1->load(buf);
96  }
97 
98 protected:
99 
103 
104  virtual void initWeights(double initr){
105  net0->initWeights(initr);
106  net1->initWeights(initr);
107  }
108 
113  virtual void update(){
114  net0->update();
115  net1->update();
116 
117  // interpolate the outputs
118  double *o0 = net0->getOutputs();
119  double *o1 = net1->getOutputs();
120  double h = getH();
121  for(int i=0;i<getOutputCount();i++){
122  interpolatedOutputs[i] = h*o1[i] + (1.0-h)*o0[i];
123  }
124  }
125 
126  double lastError = -1;
127 
133  virtual double trainBatch(ExampleSet& ex,int start,int num,double eta){
136  if(num!=1)
137  std::runtime_error("num!=1 (i.e. batch training) not implemented");
138 
139  // what we do here depends on the modulator for the first and only
140  // example
141  double hzero = (ex.getH(start)<0.5);
142  Net *net = hzero ? net0 : net1;
143 
144  double e = net->trainBatch(ex,start,1,eta);
145  // return avg of 0/1 error rate, so this will change once every two cycles;
146  // but the first one will just be the error for h=0
147  double rv;
148  if(lastError<0){
149  // nothing yet, we've done one h=0, return it.
150  lastError=e;
151  rv=e;
152  } else if(hzero) {
153  // this is the h=0 the second and subsequent times; return the last mean.
154  rv=lastError;
155  } else {
156  // this is the h=1 - calculate the new mean and return it. Set this to
157  // also be the value that will be returned on the next h=0 run.
158  lastError = rv = (e+lastError)*0.5;
159  }
160  return rv;
161  }
162 };
163 
164 
165 
166 #endif /* __OBNET_HPP */
NetType
The different types of network - each has an associated integer for saving/loading file data...
Definition: netType.hpp:15
virtual int getLayerCount() const =0
Get the number of layers.
virtual void update()
Update the two networks, and interpolate linearly between the outputs with the modulator.
Definition: obnet.hpp:113
OutputBlendingNet(int nlayers, const int *layerCounts)
Constructor - does not initialise the weights to random values so that we can reinitialise networks...
Definition: obnet.hpp:33
A modulatory network architecture which uses two plain backprop networks, each of which is trained se...
Definition: obnet.hpp:18
virtual void save(double *buf) const =0
Serialize the data (not including any network type magic number or layer/node counts) to the given me...
virtual int getLayerCount() const
Get the number of layers.
Definition: obnet.hpp:54
virtual ~OutputBlendingNet()
destructor to delete subnets and outputs
Definition: obnet.hpp:44
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
int getOutputCount() const
get the number of outputs
Definition: net.hpp:82
virtual void save(double *buf) const
Serialize the data (not including any network type magic number or layer/node counts) to the given me...
Definition: obnet.hpp:85
plain back-propagation
virtual int getLayerSize(int n) const
Get the number of nodes in a given layer.
Definition: obnet.hpp:50
virtual double * getOutputs() const =0
Get the outputs after running.
virtual int getDataSize() const
Get the length of the serialised data block for this network.
Definition: obnet.hpp:80
Contains formats for example data.
virtual void setInputs(double *d)=0
Set the inputs to the network before running or training.
Net * net1
the network trained by h=1 examples
Definition: obnet.hpp:101
virtual int getLayerSize(int n) const =0
Get the number of nodes in a given layer.
double lastError
Definition: obnet.hpp:126
double * interpolatedOutputs
the interpolated result after update()
Definition: obnet.hpp:102
virtual void setH(double h)
Set the modulator level for subsequent runs and training of this network.
Definition: obnet.hpp:58
virtual void initWeights(double initr)
initialise weights to random values
Definition: obnet.hpp:104
virtual double getH() const
get the modulator level
Definition: obnet.hpp:62
virtual void initWeights(double initr)=0
initialise weights to random values
virtual double trainBatch(ExampleSet &ex, int start, int num, double eta)=0
Train a network for batch (or mini-batch) (or single example).
virtual double trainBatch(ExampleSet &ex, int start, int num, double eta)
Train the network - see Net::trainBatch for more details, but this version is only suitable for SGD; ...
Definition: obnet.hpp:133
virtual double * getOutputs() const
Get the outputs after running.
Definition: obnet.hpp:75
virtual void update()=0
Run a single update of the network.
virtual int getDataSize() const =0
Get the length of the serialised data block for this network.
virtual void load(double *buf)=0
Given that the pointer points to a data block of the correct size for the current network...
virtual void setInputs(double *d)
Set the inputs to the network before running or training.
Definition: obnet.hpp:68
virtual void load(double *buf)
Given that the pointer points to a data block of the correct size for the current network...
Definition: obnet.hpp:92
Net * net0
the network trained by h=0 examples
Definition: obnet.hpp:100
double getH(int example) const
Get the h (modulator) for a given example.
Definition: data.hpp:359
The abstract network type upon which all others are based. It&#39;s not pure virtual, in that it encapsul...
Definition: net.hpp:39
A set of example data. Each datum consists of hormone (i.e. modulator value), inputs and outputs...
Definition: data.hpp:57