8 #include <boost/test/unit_test.hpp> 12 BOOST_AUTO_TEST_SUITE(basictrain)
26 const int NUMEXAMPLES=1000;
29 double recipNE = 1.0/(double)NUMEXAMPLES;
33 for(
double i=0;i<NUMEXAMPLES;i++){
52 double mse = net->
trainSGD(e,params);
56 BOOST_REQUIRE(mse<0.005);
58 for(
double i=0;i<NUMEXAMPLES;i++){
60 double o = *(net->
run(&v));
61 printf(
" %f,%f\n",v,o);
73 const int NUMEXAMPLES=100;
76 double recipNE = 1.0/(double)NUMEXAMPLES;
78 for(
double i=0;i<NUMEXAMPLES*2;i+=2){
79 double v = (i/2)*recipNE;
95 double mse = net->
trainSGD(e,params);
98 for(
double i=0;i<NUMEXAMPLES;i++){
100 double o = *(net->
run(&v));
101 printf(
"%f -> %f\n",v,o);
105 BOOST_REQUIRE(mse>0);
106 BOOST_REQUIRE(mse<0.005);
127 for(
int i=0;i<1000;i++){
136 drand48_r(&rd,&a);a*=0.5;
137 drand48_r(&rd,&b);b*=0.5;
168 double mse = net->
trainSGD(e,params);
171 BOOST_REQUIRE(mse<0.03);
175 for(
double a=0;a<0.5;a+=0.02){
176 for(
double b=0;b<0.5;b+=0.02){
182 double out = *(net->
run(runIns));
184 double diff = fabs(out-(a+b));
186 BOOST_REQUIRE(diff<0.05);
216 for(
int i=0;i<1000;i++){
220 drand48_r(&rd,&a);a*=0.5;
221 drand48_r(&rd,&b);b*=0.5;
266 double mse = net->
trainSGD(e,params);
269 BOOST_REQUIRE(mse<0.03);
274 for(
double a=0.1;a<0.4;a+=0.02){
275 for(
double b=0.1;b<0.4;b+=0.02){
282 double out = *(net->
run(runIns));
284 double diff = fabs(out-(a+b));
285 printf(
"%f+%f=%f (%f)\n",a,b,out,diff);
286 BOOST_REQUIRE(diff<0.07);
290 out = *(net->
run(runIns));
291 diff = fabs(out-(a+b)*0.3);
292 printf(
"%f+%f=%f (%f)\n",a,b,out,diff);
293 BOOST_REQUIRE(diff<0.07);
311 MNIST m(
"../testdata/train-labels-idx1-ubyte",
"../testdata/train-images-idx3-ubyte");
338 BOOST_REQUIRE(mse<0.03);
342 MNIST mtest(
"../testdata/t10k-labels-idx1-ubyte",
"../testdata/t10k-images-idx3-ubyte");
347 for(
int i=0;i<testSet.
getCount();i++){
351 double *o = n->
run(ins);
357 if(correctLabel==netLabel)correct++;
362 double ratio = ((double)correct)/(double)testSet.
getCount();
363 printf(
"MSE=%f, correct=%d/%d=%f\n",mse,correct,testSet.
getCount(),ratio);
365 BOOST_REQUIRE(ratio>0.85);
375 BOOST_AUTO_TEST_SUITE_END()
int getCount() const
get the number of examples
void setH(int example, double h)
Set the h (modulator) for a given example.
Training parameters for trainSGD(). This structure holds the parameters for the trainSGD() method...
SGDParams & crossValidation(const ExampleSet &examples, double propCV, int cvCount, int cvSlices, bool cvShuf=true)
Set up the cross-validation parameters given the full training set, the proportion to be used for CV...
This class encapsulates and loads data in the standard MNIST format. The data resides in two files...
Useful stuff for testing.
double trainSGD(ExampleSet &examples, SGDParams ¶ms)
Train using stochastic gradient descent. Note that cross-validation parameters are slightly different...
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.
SGDParams & storeBest()
set up a "best net buffer" to store the best network found, to which the network will be set on compl...
SGDParams & setSeed(long v)
fluent setter for seed
double * run(double *in)
Run the network on some data.
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...
int getOutputCount() const
get the number of outputs in all examples
BOOST_AUTO_TEST_CASE(trainparams)
Test training. This just checks that the network trains.
The abstract network type upon which all others are based. It's not pure virtual, in that it encapsul...
double ins[][2]
possible inputs to boolean functions
double * getInputs(int example)
Get a pointer to the inputs for a given example, for reading or writing.
A set of example data. Each datum consists of hormone (i.e. modulator value), inputs and outputs...