26 template <
class T,
class TestFunc>
void alternate(T *arr,
int nitems,
int cycle,TestFunc f){
30 for(
int i=0;i<nitems;i++){
31 if(f(arr[i])%cycle!=(i%cycle)){
36 if(f(arr[j])%cycle==i%cycle){
65 uint32_t outputOffset;
118 uint32_t exampleSize = ninputs+noutputs+1;
121 outputOffset = ninputs;
122 hOffset = ninputs+noutputs;
124 data =
new double[exampleSize*ct];
125 examples =
new double*[ct];
127 for(
int i=0;i<ct;i++){
129 examples[i] = data+i*exampleSize;
144 if(length > parent.ct - start || start<0 || length<1)
145 throw std::out_of_range(
"subset out of range");
147 ninputs = parent.ninputs;
148 noutputs = parent.noutputs;
149 outputOffset = ninputs;
150 hOffset = ninputs+noutputs;
152 examples =
new double*[length];
154 numHLevels = parent.numHLevels;
158 for(
int i=0;i<ct;i++){
159 examples[i] = parent.examples[start+i];
173 mnist.getMaxLabel()+1,
177 for(
int i=0;i<ct;i++){
179 uint8_t *imgpix = mnist.
getImg(i);
181 for(
int i=0;i<ninputs;i++){
182 double pixval = *imgpix++;
188 for(
int outIdx=0;outIdx<noutputs;outIdx++){
189 out[outIdx] = mnist.
getLabel(i)==outIdx?1:0;
268 blockSize = numHLevels;
271 double **tmp =
new double*[blockSize];
273 for(
int i=(nExamples/blockSize)-1;i>=1;i--){
277 memcpy(tmp,examples+i*blockSize,blockSize*
sizeof(
double*));
278 memcpy(examples+i*blockSize,examples+j*blockSize,blockSize*
sizeof(
double*));
279 memcpy(examples+j*blockSize,tmp,blockSize*
sizeof(
double*));
283 alternate<double*>(examples, nExamples, numHLevels,
287 double d = (e[hOffset]-minH)/(maxH-minH);
288 int i = (int)(d*(numHLevels-1));
340 return examples[example];
351 return examples[example] + outputOffset;
359 double getH(
int example)
const {
361 return *(examples[example] + hOffset);
378 void setH(
int example,
double h){
380 *(examples[example] + hOffset) = h;
388 void dump(
int start=0,
int end=-1){
390 for(
int i=start;i<end;i++){
393 for(
int j=0;j<ninputs;j++){
394 printf(
"%f ",ins[j]);
396 printf(
" modulator %f --> ",
getH(i));
397 for(
int j=0;j<noutputs;j++){
398 printf(
"%f ",outs[j]);
int getNumHLevels()
return the number of different H-levels
int getCount() const
get the number of examples
ExampleSet(int n, int nin, int nout, int levels)
Constructor - creates but doesn't fill in the data.
void setH(int example, double h)
Set the h (modulator) for a given example.
~ExampleSet()
Destructor - deletes data and offset array.
ShuffleMode
Shuffling mode for shuffle()
This class encapsulates and loads data in the standard MNIST format. The data resides in two files...
Shuffle single examples, but follow up by running a pass over the examples to ensure that they altern...
void alternate(T *arr, int nitems, int cycle, TestFunc f)
Ensure array has cycling values of some function f mod n. Given an array, this function will rearrang...
ExampleSet & setHRange(double mn, double mx)
double * getOutputs(int example)
Get a pointer to the outputs for a given example, for reading or writing.
void shuffle(drand48_data *rd, ShuffleMode mode, int nExamples=0)
Shuffle the example using a PRNG and a Fisher-Yates shuffle.
Shuffle single examples, no matter the value of numHLevels.
int getInputCount() const
get the number of inputs in all examples
uint8_t getLabel(int n) const
get the label for a given example
Don't shuffle examples at all.
Shuffle blocks of numHLevels examples, rather than single examples. This is intended for cases where ...
ExampleSet(const ExampleSet &parent, int start, int length)
Constructor for making a subset of another set. This uses the actual data in the parent, but creates a fresh set of offset structures which can be independently shuffled.
int getOutputCount() const
get the number of outputs in all examples
uint8_t * getImg(int n) const
get the bitmap for a given example
Code for converting MNIST data into example sets.
ExampleSet(const MNIST &mnist)
Special constructor for generating a data set from an MNIST database with a single labelling (i...
double getH(int example) const
Get the h (modulator) for a given example.
void dump(int start=0, int end=-1)
dump to stdout
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...