diff options
author | Joe Zhao <ztuowen@gmail.com> | 2014-04-14 08:14:45 +0800 |
---|---|---|
committer | Joe Zhao <ztuowen@gmail.com> | 2014-04-14 08:14:45 +0800 |
commit | cccccbf6cca94a3eaf813b4468453160e91c332b (patch) | |
tree | 23418cb73a10ae3b0688681a7f0ba9b06424583e /src/CuTNetLib/cuCompDisc.h | |
download | tnet-cccccbf6cca94a3eaf813b4468453160e91c332b.tar.gz tnet-cccccbf6cca94a3eaf813b4468453160e91c332b.tar.bz2 tnet-cccccbf6cca94a3eaf813b4468453160e91c332b.zip |
First commit
Diffstat (limited to 'src/CuTNetLib/cuCompDisc.h')
-rw-r--r-- | src/CuTNetLib/cuCompDisc.h | 288 |
1 files changed, 288 insertions, 0 deletions
diff --git a/src/CuTNetLib/cuCompDisc.h b/src/CuTNetLib/cuCompDisc.h new file mode 100644 index 0000000..5b3232e --- /dev/null +++ b/src/CuTNetLib/cuCompDisc.h @@ -0,0 +1,288 @@ +#ifndef _CUCOMPDISC_H_ +#define _CUCOMPDISC_H_ + + +#include "cuComponent.h" +#include "cumatrix.h" +#include "cuNetwork.h" + +#include "Matrix.h" +#include "Vector.h" +#include "Error.h" + + +namespace TNet { + + /** + * \brief A layer of updatable compenents + * + * \ingroup CuNNUpdatable + * Each components are individually propagated and backpropagated with discrete inputs and outputs + * + * Enabling multipath topological structure within the network by layers + */ + + class CuLumpUpdatable : public CuUpdatableComponent + { + public: + CuLumpUpdatable(size_t nInputs, size_t nOutputs, CuComponent *pPred) + : CuUpdatableComponent(nInputs, nOutputs, pPred) + { } + + void LearnRate(BaseFloat rate) + { + mLearningRate = rate; + for (int i=0;i<mBlocks.size();++i) + if ( mBlocks[i]->IsUpdatable() ) + { + CuUpdatableComponent& rComp = dynamic_cast<CuUpdatableComponent&>(*mBlocks[i]); + rComp.LearnRate(rate); + } + } + + + void Momentum(BaseFloat mmt) + { + mMomentum = mmt; + for (int i=0;i<mBlocks.size();++i) + if ( mBlocks[i]->IsUpdatable() ) + { + CuUpdatableComponent& rComp = dynamic_cast<CuUpdatableComponent&>(*mBlocks[i]); + rComp.Momentum(mmt); + } + } + + void Weightcost(BaseFloat cost) + { + mWeightcost = cost; + for (int i=0;i<mBlocks.size();++i) + if ( mBlocks[i]->IsUpdatable() ) + { + CuUpdatableComponent& rComp = dynamic_cast<CuUpdatableComponent&>(*mBlocks[i]); + rComp.Weightcost(cost); + } + } + + void GradDivFrm(bool div) + { + mGradDivFrm = div; + for (int i=0;i<mBlocks.size();++i) + if ( mBlocks[i]->IsUpdatable() ) + { + CuUpdatableComponent& rComp = dynamic_cast<CuUpdatableComponent&>(*mBlocks[i]); + rComp.GradDivFrm(div); + } + } + + protected: + std::vector< CuComponent* > mBlocks; ///< vector with component, one component is one block + }; + + /** + * \brief A layer of updatable compenents + * + * \ingroup CuNNUpdatable + * Each components are individually propagated and backpropagated with inputs and outputs within one matrix to save space + * + */ + + class CuDiscrete : public CuLumpUpdatable + { + public: + + typedef struct posID{ int block,pos; posID(int b, int p):block(b),pos(p){}} posID; + + + CuDiscrete(size_t nInputs, size_t nOutputs, CuComponent *pPred); + ~CuDiscrete(); + + ComponentType GetType() const; + const char* GetName() const; + + void Propagate(); + void Backpropagate(); + + void PropagateFnc(const CuMatrix<BaseFloat>& X, CuMatrix<BaseFloat>& Y); + void BackpropagateFnc(const CuMatrix<BaseFloat>& X, CuMatrix<BaseFloat>& Y); + + void Update(); + + void ReadFromStream(std::istream& rIn); + void WriteToStream(std::ostream& rOut); + + int GetInSect() + { + return inID.size(); + } + + int GetOutSect() + { + return outID.size(); + } + + CuComponent* FindInput(int &pos) + { + if (pos<0 or pos>=inID.size()) + Error("Position out of bound"); + int i=pos; + pos=inID[i].pos; + return mBlocks[inID[i].block]; + } + + CuComponent* FindOutput(int &pos) + { + if (pos<0 or pos>=outID.size()) + Error("Position out of bound"); + int i=pos; + pos=outID[i].pos; + return mBlocks[outID[i].block]; + } + + /// IO Data getters + CuMatrix<BaseFloat>& GetInput(int pos=0) + { + if (preComp!=NULL) + return preComp->GetOutput(pos); + return *mpInput; + } + CuMatrix<BaseFloat>& GetOutput(int pos=0) + { + CuComponent* pComp=FindOutput(pos); + return pComp->GetOutput(pos); + } + CuMatrix<BaseFloat>& GetErrorInput(int pos=0) + { + if (nxtComp!=NULL) + return nxtComp->GetErrorOutput(pos); + return *mpErrorInput; + } + CuMatrix<BaseFloat>& GetErrorOutput(int pos=0) + { + CuComponent* pComp=FindInput(pos); + return pComp->GetErrorOutput(pos); + } + + /// Set input vector (bind with the preceding NetworkComponent) + void SetInput(CuMatrix<BaseFloat>& rInput,int pos=0) + { + if (pos==0) + mpInput=&rInput; + CuComponent* pComp=FindInput(pos); + pComp->SetInput(rInput,pos); + } + /// Set error input vector (bind with the following NetworkComponent) + void SetErrorInput(CuMatrix<BaseFloat>& rErrorInput,int pos=0) + { + if (pos==0) + mpErrorInput=&rErrorInput; + CuComponent* pComp=FindOutput(pos); + pComp->SetErrorInput(rErrorInput,pos); + } + private: + std::vector< CuComponent* > mBlocks; + std::vector< posID > inID,outID; + }; + + + + + //////////////////////////////////////////////////////////////////////////// + // INLINE FUNCTIONS + // CuDiscrete:: + inline + CuDiscrete:: + CuDiscrete(size_t nInputs, size_t nOutputs, CuComponent *pPred) + : CuLumpUpdatable(nInputs, nOutputs, pPred) + { } + + + inline + CuDiscrete:: + ~CuDiscrete() + { + for(int i=0; i<mBlocks.size(); i++) { + delete mBlocks[i]; + } + mBlocks.clear(); + } + + inline CuComponent::ComponentType + CuDiscrete:: + GetType() const + { + return CuComponent::DISCRETE; + } + + inline const char* + CuDiscrete:: + GetName() const + { + return "<discrete>"; + } + + class CuCompound : public CuLumpUpdatable + { + public: + + CuCompound(size_t nInputs, size_t nOutputs, CuComponent *pPred); + ~CuCompound(); + + ComponentType GetType() const; + const char* GetName() const; + + void PropagateFnc(const CuMatrix<BaseFloat>& X, CuMatrix<BaseFloat>& Y); + void BackpropagateFnc(const CuMatrix<BaseFloat>& X, CuMatrix<BaseFloat>& Y); + + void Update(); + + void ReadFromStream(std::istream& rIn); + void WriteToStream(std::ostream& rOut); + + void PropagateF(CuMatrix<BaseFloat>& X, CuMatrix<BaseFloat>& Y); + void BackpropagateF(CuMatrix<BaseFloat>& X, CuMatrix<BaseFloat>& Y); + + protected: + + std::vector< CuComponent* > mBlocks; ///< vector with component, one component is one block + + }; + + //////////////////////////////////////////////////////////////////////////// + // INLINE FUNCTIONS + // CuLinearity:: + inline + CuCompound:: + CuCompound(size_t nInputs, size_t nOutputs, CuComponent *pPred) + : CuLumpUpdatable(nInputs, nOutputs, pPred) + { } + + + inline + CuCompound:: + ~CuCompound() + { + for(int i=0; i<mBlocks.size(); i++) { + delete mBlocks[i]; + } + mBlocks.clear(); + } + + inline CuComponent::ComponentType + CuCompound:: + GetType() const + { + return CuComponent::COMPOUND; + } + + inline const char* + CuCompound:: + GetName() const + { + return "<compound>"; + } + +} //namespace + + + +#endif |