21 return 1.0/(1.0+exp(-x));
117 virtual void setH(
double h)=0;
122 virtual double getH()
const =0;
146 if(num<0)num=examples.
getCount()-start;
151 for(
int i=0;i<num;i++){
157 double d = netout[j]-exout[j];
212 nPerSlice = nperslice;
213 cvInterval = interval;
298 void init(
double _eta,
int _iters){
303 bestNetBuffer = NULL;
304 ownsBestNetBuffer =
false;
305 storeBestNet =
false;
310 selectBestWithCV=
false;
333 init(_eta,examples.
getCount()*_iters);
364 int nCV = (int)round(propCV*examples.
getCount());
365 if(nCV==0 || nCV>examples.
getCount())
366 throw std::out_of_range(
"Bad cross-validation count");
368 throw std::out_of_range(
"Zero (or fewer) CV slices is a bad thing");
372 nPerSlice = nCV/cvSlices;
375 throw std::logic_error(
"Too many slices");
377 cvInterval = iterations/cvCount;
379 throw std::logic_error(
"Too many CV events");
381 selectBestWithCV=
true;
383 printf(
"Cross-validation: %d slices, %d items per slice, %d total\n",
384 nSlices,nPerSlice,nSlices*nPerSlice);
395 ownsBestNetBuffer =
true;
404 bool ownsBestNetBuffer;
437 throw std::out_of_range(
"Too many cross-validation examples");
440 throw std::logic_error(
"cannot use CV to select best when no CV is done");
443 int nExamples = examples.
getCount() - nCV;
449 double minError = -1;
471 FILE *log = fopen(
"foo",
"w");
472 fprintf(log,
"x,slice,y\n");
475 int exampleIndex = i % nExamples;
480 if(exampleIndex == 0)
484 double trainingError =
trainBatch(examples,exampleIndex,1,params.
eta);
490 if(minError < 0 || trainingError < minError){
496 minError = trainingError;
502 if(nCV && !--cvCountdown){
509 fprintf(log,
"%d,%d,%f\n",i,cvSlice,error);
513 if(minError < 0 || trainingError < minError){
519 minError = trainingError;
524 cvSlice = (cvSlice+1)%params.
nSlices;
538 return test(nCV?cvExamples:examples);
553 virtual void save(
double *buf)
const = 0;
561 virtual void load(
double *buf) = 0;
573 virtual void update() = 0;
591 inline double drand(
double mn,
double mx){
594 return res*(mx-mn)+mn;
int getCount() const
get the number of examples
Contains integer enum for network types.
NetType
The different types of network - each has an associated integer for saving/loading file data...
SGDParams & setInitRange(double range=-1)
fluent setter for initrange
int initrange
range of initial weights/biases [-n,n], or -1 for Bishop's rule.
virtual int getLayerCount() const =0
Get the number of layers.
SGDParams & crossValidationManual(int slices, int nperslice, int interval)
fluent setter for cross-validation parameters manually; consider using crossValidation instead ...
SGDParams & setSelectBestWithCV(bool v=true)
fluent setter for selectBestWithCV
A modulatory network architecture which uses two plain backprop networks, each of which is trained se...
Training parameters for trainSGD(). This structure holds the parameters for the trainSGD() method...
ShuffleMode
Shuffling mode for shuffle()
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...
SGDParams(double _eta, int _iters)
Constructor which sets up defaults with no information about examples - cross-validation is not set u...
int getInputCount() const
get the number of inputs
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...
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.
int getOutputCount() const
get the number of outputs
double sigmoidDiff(double x)
double * getOutputs(int example)
Get a pointer to the outputs for a given example, for reading or writing.
int cvInterval
how often to cross-validate given as the interval between CV events: 1 is every iteration, 2 is every other iteration and so on.
virtual double getH() const =0
get the modulator level
virtual double * getOutputs() const =0
Get the outputs after running.
int nPerSlice
the number of example per cross-validation slice
void shuffle(drand48_data *rd, ShuffleMode mode, int nExamples=0)
Shuffle the example using a PRNG and a Fisher-Yates shuffle.
SGDParams & storeBest()
set up a "best net buffer" to store the best network found, to which the network will be set on compl...
bool storeBestNet
true if we should store the best net data
Contains formats for example data.
virtual void setInputs(double *d)=0
Set the inputs to the network before running or training.
double * bestNetBuffer
a buffer of at least getDataSize() bytes for the best network. If NULL, the best network is not saved...
virtual int getLayerSize(int n) const =0
Get the number of nodes in a given layer.
Net(NetType tp)
Constructor - protected because others inherit it and it's not used directly.
Shuffle blocks of numHLevels examples, rather than single examples. This is intended for cases where ...
SGDParams & setCVShuffle(bool v=true)
fluent setter for cvShuffle
virtual ~Net()
virtual destructor which does nothing
SGDParams & setSeed(long v)
fluent setter for seed
bool selectBestWithCV
if true, use the minimum CV error to find the best net, otherwise use the training error...
double * run(double *in)
Run the network on some data.
int getOutputCount() const
get the number of outputs in all examples
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 void update()=0
Run a single update of the network.
long seed
seed for random number generator used to initialise weights and also perform shuffling ...
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...
int iterations
number of iterations to run: an iteration is the presentation of a single example, NOT an epoch (or occasionally pair-presentation) as is the case in the thesis when discussing the modulatory network types.
int nSlices
The number of cross-validation slices to use.
NetType type
type of the network, used for load/save
drand48_data rd
PRNG data (thread safe)
SGDParams & setShuffle(ExampleSet::ShuffleMode m)
fluent setter for preserveHAlternation
bool cvShuffle
if true, shuffle the entire CV data set when all slices have been done so that the cross-validation h...
SGDParams(double _eta, const ExampleSet &examples, int _iters)
void setSeed(long seed)
Set this network's random number generator, which is used for weight initialisation done at the start...
double getH(int example) const
Get the h (modulator) for a given example.
double test(ExampleSet &examples, int start=0, int num=-1)
Test a network. Runs the network over a set of examples and returns the mean MSE for all outputs whe...
The abstract network type upon which all others are based. It's not pure virtual, in that it encapsul...
double * getInputs(int example)
Get a pointer to the inputs for a given example, for reading or writing.
double drand(double mn, double mx)
get a random number using this net's PRNG data
A set of example data. Each datum consists of hormone (i.e. modulator value), inputs and outputs...
ExampleSet::ShuffleMode shuffleMode
The shuffle mode to use - see the ExampleSet::ShuffleMode enum for details.