32 void init(
int nlayers,
const int *layerCounts){
39 int n = layerCounts[i];
54 int n = layerCounts[i];
70 init(nlayers,layerCounts);
73 virtual void setH(
double h){
77 virtual double getH()
const {
151 virtual void save(
double *buf)
const {
162 for(
int k=0;k<layerSizes[i-1];k++){
170 virtual void load(
double *buf){
181 for(
int k=0;k<layerSizes[i-1];k++){
222 double ct = layerSizes[i-1];
226 initrange = 1.0/sqrt(ct);
229 for(
int j=0;j<layerSizes[i];j++)
230 biases[i][j]=
drand(-initrange,initrange);
232 weights[i][j]=
drand(-initrange,initrange);
236 for(
int j=0;j<layerSizes[0];j++)
249 inline double&
getw(
int tolayer,
int toneuron,
int fromneuron)
const {
250 return weights[tolayer][toneuron+largestLayerSize*fromneuron];
259 inline double&
getb(
int layer,
int neuron)
const {
260 return biases[layer][neuron];
272 inline double&
getavggradw(
int tolayer,
int toneuron,
int fromneuron)
const {
273 return gradAvgsWeights[tolayer][toneuron+largestLayerSize*fromneuron];
284 return gradAvgsBiases[l][n];
300 int ol = numLayers-1;
301 for(
int i=0;i<layerSizes[ol];i++){
302 double o = outputs[ol][i];
303 errors[ol][i] = o*(1-o)*(o-out[i]);
307 for(
int l=1;l<numLayers-1;l++){
308 for(
int j=0;j<layerSizes[l];j++){
310 for(
int i=0;i<layerSizes[l+1];i++)
311 e += errors[l+1][i]*
getw(l+1,i,j);
315 errors[l][j] = e * outputs[l][j] * (1-outputs[l][j]);
322 for(
int j=0;j<layerSizes[i];j++){
323 double v = biases[i][j];
324 for(
int k=0;k<layerSizes[i-1];k++){
325 v +=
getw(i,j,k) * outputs[i-1][k];
335 for(
int k=0;k<layerSizes[j];k++)
336 gradAvgsBiases[j][k]=0;
338 gradAvgsWeights[j][i]=0;
344 for(
int nn=0;nn<num;nn++){
345 int exampleIndex = nn+start;
355 for(
int i=0;i<layerSizes[l];i++){
356 for(
int j=0;j<layerSizes[l-1];j++)
357 getavggradw(l,i,j) += errors[l][i]*outputs[l-1][j];
358 gradAvgsBiases[l][i] += errors[l][i];
362 int ol = numLayers-1;
363 for(
int i=0;i<layerSizes[ol];i++){
364 double o = outputs[ol][i];
365 double e = (o-outs[i]);
371 double factor = 1.0/(double)num;
374 for(
int i=0;i<layerSizes[l];i++){
375 for(
int j=0;j<layerSizes[l-1];j++){
378 getw(l,i,j) -= wdelta;
380 double bdelta = eta*gradAvgsBiases[l][i]*factor;
381 biases[l][i] -= bdelta;
385 return totalError*factor;
BPNet(int nlayers, const int *layerCounts)
Constructor - does not initialise the weights to random values so that we can reinitialise networks...
NetType
The different types of network - each has an associated integer for saving/loading file data...
virtual void setInputs(double *d)
Set the inputs to the network before running or training.
double ** gradAvgsBiases
average gradient for each bias (built during training)
BPNet()
Special constructor for subclasses which need to manipulate layer count before initialisation (e...
void init(int nlayers, const int *layerCounts)
Initialiser for use by the main constructor and the ctors of those subclasses mentioned in BPNet() ...
void calcError(double *in, double *out)
run a single example and calculate the errors; used in training.
int numLayers
number of layers, including input and output
double ** biases
array of biases, stored as a rectangular array of [layer][node]
This is the abstract basic network class - the training methods are in each subclass.
double & getavggradw(int tolayer, int toneuron, int fromneuron) const
get the value of the gradient for a given weight
virtual void load(double *buf)
Given that the pointer points to a data block of the correct size for the current network...
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.
double * getOutputs(int example)
Get a pointer to the outputs for a given example, for reading or writing.
virtual void update()
Run a single update of the network.
virtual double trainBatch(ExampleSet &ex, int start, int num, double eta)
Train a network for batch (or mini-batch) (or single example).
virtual void save(double *buf) const
Serialize the data (not including any network type magic number or layer/node counts) to the given me...
double & getw(int tolayer, int toneuron, int fromneuron) const
get the value of a weight.
virtual int getDataSize() const
Get the length of the serialised data block for this network.
double & getb(int layer, int neuron) const
get the value of a bias
virtual void initWeights(double initr)
initialise weights to random values
double getavggradb(int l, int n) const
get the value of a bias gradient
virtual void setH(double h)
Set the modulator level for subsequent runs and training of this network.
double ** outputs
outputs of each layer: one array of doubles for each
double ** errors
the error for each node, calculated by calcError()
virtual int getLayerCount() const
Get the number of layers.
virtual int getLayerSize(int n) const
Get the number of nodes in a given layer.
virtual double * getOutputs() const
Get the outputs after running.
virtual ~BPNet()
destructor
void setInput(int n, double d)
Used to set inputs manually, typically in HInputNet.
double getH(int example) const
Get the h (modulator) for a given example.
double ** gradAvgsWeights
average gradient for each weight (built during training)
virtual double getH() const
get the modulator level
double ** weights
Array of weights as [tolayer][tonode+largestLayerSize*fromnode].
The abstract network type upon which all others are based. It's not pure virtual, in that it encapsul...
int largestLayerSize
number of nodes in largest layer
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...
int * layerSizes
array of layer sizes