UESMANN CPP  1.0
Reference implementation of UESMANN
/home/travis/build/jimfinnis/uesmanncpp/genBoolMap.cpp
Go to the documentation of this file.
1 
13 #include "netFactory.hpp"
14 
16 #define NUM_ATTEMPTS 1000
17 
19 #define ETA 0.1
20 
21 
26 #define EPOCHS 75000
27 
32 double ins[][2]={
33  {0,0},
34  {0,1},
35  {1,0},
36  {1,1}};
37 
41 const char *simpleNames[] = {
42  "f","and","x and !y","x","!x and y","y","xor","or","nor","xnor",
43  "!y","x or !y","!x","!x or y","nand","t"};
44 
50 bool boolFunc(int f,bool a,bool b){
51  // which bit do we want?
52  int bit = 1<<((a?0:2)+(b?0:1));
53 // printf("Bit set is %d, & %d = %d\n",bit,f,bit&f);
54  return (f&bit)!=0;
55 }
56 
63 static void setExample(ExampleSet& e,int exampleIdx,
64  int functionIdx,int xbit,int ybit,double mod){
65  double *ins = e.getInputs(exampleIdx);
66  double *outs = e.getOutputs(exampleIdx);
67  e.setH(exampleIdx,mod);
68  ins[0] = xbit;
69  ins[1] = ybit;
70  bool val = boolFunc(functionIdx,xbit!=0,ybit!=0);
71  *outs = val ? 1 : 0;
72 
73 }
74 
80 bool success(int f1,int f2,Net *n){
81  double in[2];
82  double out;
83  for(int a=0;a<2;a++){
84  for(int b=0;b<2;b++){
85  bool shouldBeHigh1 = boolFunc(f1,a!=0,b!=0);
86  bool shouldBeHigh2 = boolFunc(f2,a!=0,b!=0);
87  in[0]=a;
88  in[1]=b;
89  n->setH(0);
90  out = *(n->run(in));
91 // printf("%d %d at 0 -> %f (should be %d)\n",a,b,out,shouldBeHigh1);
92  if(out>0.5 != shouldBeHigh1)return false;
93  n->setH(1);
94  out = *(n->run(in));
95 // printf("%d %d at 1 -> %f (should be %d)\n",a,b,out,shouldBeHigh2);
96  if(out>0.5 != shouldBeHigh2)return false;
97  }
98  }
99  return true;
100 }
101 
102 
110 double doPairing(int f1,int f2){
111  // first we need to build the examples.
112  // 8 examples (4 at each mod level), 2 in, 1 out, 2 mod levels
113  ExampleSet e(8,2,1,2);
114  // add examples for 0,0
115  setExample(e,0,f1,0,0,0);
116  setExample(e,1,f2,0,0,1);
117  // add examples for 0,1
118  setExample(e,2,f1,0,1,0);
119  setExample(e,3,f2,0,1,1);
120  // add examples for 1,0
121  setExample(e,4,f1,1,0,0);
122  setExample(e,5,f2,1,0,1);
123  // add examples for 1,1
124  setExample(e,6,f1,1,1,0);
125  setExample(e,7,f2,1,1,1);
126 
127  // training parameters.
128  Net::SGDParams params(ETA,e,EPOCHS);
129  // pick the best network by training MSE (not cross-validation
130  // as we're not doing it) and keep it as we go along.
131  // Shuffle the network by stride, which is the number of examples:
132  // on each epoch, pairs of examples will be shuffled rather than
133  // single examples. This is true to the method given in the thesis,
134  // alternating training between h=0 and h=1 examples.
135 
137 
138 
139  int successful = 0; // number of networks which worked
140  for(int i=0;i<NUM_ATTEMPTS;i++){
141  // make a new network
143  // set a new PRNG and train it (this will also set the init
144  // weights)
145  params.setSeed(i);
146  // train the network
147  n->trainSGD(e,params);
148  // and increment the count if it was good
149  if(success(f1,f2,n))
150  successful++;
151  delete n; // remember to delete the network
152  }
153  // return successful proportion
154  return ((double)successful)/(double)NUM_ATTEMPTS;
155 }
156 
160 int main(int argc,char *argv[]){
161  // output is function 1, function 2, and correct network
162  // proportion
163  printf("a,b,correct\n");
164  // run the 256 pairings and output their correctness proportion.
165  for(int f1=0;f1<16;f1++){
166  for(int f2=0;f2<16;f2++){
167  printf("%d,%d,%f\n",f1,f2, doPairing(f1,f2));
168  }
169  }
170 }
void setH(int example, double h)
Set the h (modulator) for a given example.
Definition: data.hpp:378
const char * simpleNames[]
names of functions performed by boolFunc()
Definition: genBoolMap.cpp:41
#define EPOCHS
how many epochs to train each genBoolMap network for - at 8 examples per epoch, this is 600000 traini...
Definition: genBoolMap.cpp:26
Training parameters for trainSGD(). This structure holds the parameters for the trainSGD() method...
Definition: net.hpp:173
double trainSGD(ExampleSet &examples, SGDParams &params)
Train using stochastic gradient descent. Note that cross-validation parameters are slightly different...
Definition: net.hpp:428
virtual void setH(double h)=0
Set the modulator level for subsequent runs and training of this network.
double * getOutputs(int example)
Get a pointer to the outputs for a given example, for reading or writing.
Definition: data.hpp:349
SGDParams & storeBest()
set up a "best net buffer" to store the best network found, to which the network will be set on compl...
Definition: net.hpp:394
double doPairing(int f1, int f2)
Train a large number of networks to do a particular pairing of boolean functions (provided as indices...
Definition: genBoolMap.cpp:110
#define ETA
the learning rate for genBoolMap
Definition: genBoolMap.cpp:19
bool boolFunc(int f, bool a, bool b)
given a function index, perform the appropriate boolean. The index is actually the truth table: four ...
Definition: genBoolMap.cpp:50
Shuffle blocks of numHLevels examples, rather than single examples. This is intended for cases where ...
Definition: data.hpp:228
SGDParams & setSeed(long v)
fluent setter for seed
Definition: net.hpp:275
double * run(double *in)
Run the network on some data.
Definition: net.hpp:107
static Net * makeNet(NetType t, ExampleSet &e, int hnodes)
Construct a single hidden layer network of a given type which conforms to the example set...
Definition: netFactory.hpp:32
bool success(int f1, int f2, Net *n)
test if a given network successfully performs a given pair of boolean functions, modulating from f1 t...
Definition: genBoolMap.cpp:80
I&#39;m not a fan of factories, but here&#39;s one - this makes a network of the appropriate type which confo...
SGDParams & setShuffle(ExampleSet::ShuffleMode m)
fluent setter for preserveHAlternation
Definition: net.hpp:224
h-as-input
#define NUM_ATTEMPTS
How many networks to attempt for each pairing in genBoolMap.
Definition: genBoolMap.cpp:16
int main(int argc, char *argv[])
The main function for genBoolMap.
Definition: genBoolMap.cpp:160
The abstract network type upon which all others are based. It&#39;s not pure virtual, in that it encapsul...
Definition: net.hpp:39
double ins[][2]
possible inputs to boolean functions
Definition: genBoolMap.cpp:32
double * getInputs(int example)
Get a pointer to the inputs for a given example, for reading or writing.
Definition: data.hpp:338
A set of example data. Each datum consists of hormone (i.e. modulator value), inputs and outputs...
Definition: data.hpp:57