/* * File : backprop.C * * History * Date Author Description * ------- ----------------- -------------------------------------- * 08/23/91 Andrew H. Fagg Original Release * 09/19/93 John C. Weng Release With NSL 2.5 * */ #include "nsl_include.h" #include "backprop.h" BPForward::BPForward(char* str, NslModule* parent, int inSize,int hidSize) : NslModule(str,parent), fInput("fInput", this, inSize), mf("mf", this, hidSize), mp("mp", this, hidSize), h("h", this, hidSize), dh("dh", this, hidSize), w("w", this, inSize, hidSize), dw("dw", this, inSize, hidSize) { } void BPForward::initSys() { nslRandom(w,-1.0,1.0); nslRandom(h,-1.0,1.0); dw = 0.0; dh = 0.0; } void BPForward::forwardPass() { mp = NSLprod(fInput,w); // mat_mult_col_vec(w, fInput); mf = NSLsigmoid(mp + h); } void BPForward::simRun() { forwardPass(); } void BPForward::simTrain() { w = w + dw; h = h + dh; forwardPass(); } void BPForward::endTrain() { // w = w + dw; // h = h + dh; } // Backward Hidden Layer BPBackward::BPBackward(char* str, NslModule* parent, int inSize,int hidSize) : NslModule(str,parent), fInput("fInput", this, inSize), bInput("bInput", this, hidSize), mf("mf", this, hidSize), delta("delta", this, hidSize), dh("dh", this, hidSize), dw("dw", this, inSize, hidSize), lrate("lrate", this), momentum("momentum", this) { } void BPBackward::simTrain() { backwardPass(); } void BPBackward::backwardPass() { delta = (mf ^ (1.0 - mf)) ^ bInput; dw = lrate * NSLprod2(delta,fInput); // vec_mult_vec_trans(delta, fInput); // + momentum * dw; // dw + dh = lrate * delta; // + momentum * dh; // dh + } // Backward Prop BPBackwardProp::BPBackwardProp(char* str, NslModule* parent, int hidSize, int outSize) : BPBackward(str,parent, hidSize,outSize), bOutput("bOutput", this, hidSize), w("w", this, hidSize,outSize) { } void BPBackwardProp::simTrain() { backwardPass(); bOutput = NSLprod(w,delta); // mat_trans_mult_col_vec(w, delta); } // Backward Error BPBackwardError::BPBackwardError(char* str, NslModule* parent, int outSize) : NslModule(str,parent), mf("mf", this, outSize), desired("desired",this, outSize), eOutput("eOutput",this, outSize), stopError("stopError", this), tss("tss", this), pss("pss", this) { } void BPBackwardError::initSys() { tss = 0.0; } void BPBackwardError::initTrain() { pss = 0.0; // mf = 0; } void BPBackwardError::simTrain() { eOutput = desired - mf; pss = pss + (eOutput ^ eOutput).sum(); } void BPBackwardError::endTrain() { tss = pss; int tepoch = (int) NSL_SYSTEM->getTrainEpochStep(); cmd_out("Epoch: ", tepoch); cmd_out("tss = ", tss.get_value()); if (tss < stopError) { cmd_out("Convergence"); nslBreakEpoch(); return; } } // AllLayers BackPropLayers::BackPropLayers(char* str, NslModule* parent, int inSize,int hidSize, int outSize) : NslModule(str,parent), fh("fh",this, inSize,hidSize), fo("fo",this, hidSize,outSize), be("be",this, outSize), bo("bo",this, hidSize,outSize), bh("bh",this, inSize,hidSize), input("input",this, inSize), output("output",this, outSize), desired("desired",this, outSize) { makeConn(); } void BackPropLayers::makeConn() { nslRelabel(input,fh.fInput); nslRelabel(input,bh.fInput); nslRelabel(fo.mf,output); nslRelabel(desired,be.desired); nslConnect(fh.mf,fo.fInput); nslConnect(fh.mf,bo.fInput); nslConnect(fh.mf,bh.mf); nslConnect(fo.mf,bo.mf); nslConnect(fo.mf,be.mf); nslConnect(bh.dw,fh.dw); nslConnect(bh.dh,fh.dh); nslConnect(bo.dw,fo.dw); nslConnect(bo.dh,fo.dh); nslConnect(fo.w,bo.w); nslConnect(be.eOutput,bo.bInput); nslConnect(bo.bOutput,bh.bInput); } // train manager TrainManager::TrainManager(char* str, NslModule* parent, int nPats,int inSize,int outSize) : NslModule(str,parent), dInput("dInput", this, inSize), dOutput("dOutput", this, outSize), pInput("pInput", this, nPats, inSize), pOutput("pOutput", this, nPats, outSize), numPats("numPats",this) { } void TrainManager::initSys() { pInput.nslPrint(); pOutput.nslPrint(); /* int i,nPats,tmp; int pat; NslFile file(); if (file.open_file(("xor.pat",NSL_INPUT) == -1) cmd_out("Bad file name: ",(("xor.pat"); else { file.read(numPats); for(pat = 0; pat < numPats; ++pat) { for(i = 0; i < pInput.getSize(); ++i) { file.read(tmp); pInput[pat][i] = tmp; } for(i = 0; i < pOutput.getSize(); ++i) { file.read(tmp); pOutput[pat][i] = tmp; } } file.close_file(); } */ } void TrainManager::simTrain() { int timeStep = NSL_SYSTEM->getTrainCurrentTimeStep() - 1; int endStep = (int) NSL_SYSTEM->getTrainEndTime() + 0.5; int current_pat = timeStep%endStep; dInput = pInput[current_pat]; dOutput = pOutput[current_pat]; } BackPropModel::BackPropModel() : NslModule("BackPropModel"), tf("tf",this, nPats,inSize,outSize), bp("bp",this, inSize,hidSize,outSize) { makeConn(); } void BackPropModel::makeConn() { nslConnect(tf.dInput,bp.input); nslConnect(tf.dOutput,bp.desired); } AslSchemaModel _BackPropModel("BackPropModel");