// NetActivation.h // copyright 6 August 1997 Kevin Karplus #ifndef NetActivation_H #define NetActivation_H #include "NeuralNet.h" class OneChain; class ActivationRecord; class NetActivation { const NeuralNet *Net; int NumLayers; //redundant with Net, for consistency checks const OneChain* Chain; // what chain is this activated on? int Length, AllocLength; // length of the chain this is activated on ActivationRecord ***Records; ActivationRecord **Dummies; // result of null input for each layer const float ***Inputs; // this owns the first two levels of the array: // the pointers to arrays for each layer // the arrays of pointers for windowing inputs to layer i // this does not own the final arrays of probabilities, // which are owned by individual ActivityRecord or OneChain // structures. // That is, Inputs[lay][x] is a pointer to a Probs array // that is an input to layer lay. // The width of the array may be different for each layer, // (computed by padded_layer_length(lay)). // as the array of pointers has to be padded with an // adequate number of dummies to make windowing trivial. void DeleteAll(void); // delete all Records void ReAlloc(int new_length); // rebuild Records for new chain length // Doesn't preserve contents of Records. public: NetActivation(const NeuralNet *n, int init_length=500); ~NetActivation(void) {DeleteAll();} void assign_chain(const OneChain *newchain); inline const OneChain *chain(void) const {return Chain;} inline int layer_length(int lay) const { return lay>=0? (Length +2 * Net->overhang(lay)): Length; } inline int padded_layer_length(int lay) const { int len = layer_length(lay); if (lay+1>NumLayers) return len; int len_needed=layer_length(lay+1) + Net->layer(lay+1)->num_wind() -1; return (len < len_needed)? len_needed: len; } // Prints the average phat, correlation matrices, etc. for the ouput // units of each layer void print_unit_usage(ofstream& outfile); inline int num_layers(void) const { return NumLayers; } inline const NeuralNet *which_net(void) const { return Net; } // lay is layer (must be >=0) // pos is -overhang to Length+overhang-1 ActivationRecord* record(int lay, int pos) const; // do forward propagation through network void activate(void); // set the cost fields for trainable layers, // but don't compute partials and don't do back-propagation. void test_outputs(void); // for an already activated record, do back-propagation of // partial derivatives of error function (multiplied by weight). // Doesn't update costs (use test_outputs for that). void back_propagate(double weight); // Added by Frellsen // For an already back-propagated set of activations records, // calculate the partial derivatives of the error with respect // to the inputs. The array partials is filled with the // partials, and is assumed created and cleared. // Each partial is stored in partials[col][Alpha ind] void add_InputPartials(double **partials) const; inline void clear_InputPartials(double **partials) const { // Reset the values of partials int num_units = Net->interface(0)->num_units(); for(int col=0; col