From cccccbf6cca94a3eaf813b4468453160e91c332b Mon Sep 17 00:00:00 2001 From: Joe Zhao Date: Mon, 14 Apr 2014 08:14:45 +0800 Subject: First commit --- src/CuTNetLib/cuMisc.h | 555 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 555 insertions(+) create mode 100644 src/CuTNetLib/cuMisc.h (limited to 'src/CuTNetLib/cuMisc.h') diff --git a/src/CuTNetLib/cuMisc.h b/src/CuTNetLib/cuMisc.h new file mode 100644 index 0000000..7319adf --- /dev/null +++ b/src/CuTNetLib/cuMisc.h @@ -0,0 +1,555 @@ +#ifndef _CUMISC_H_ +#define _CUMISC_H_ + +#include + +#include "cuComponent.h" +#include "cumatrix.h" + + +#include "Matrix.h" +#include "Vector.h" +#include "Error.h" + + +namespace TNet { + /** + * \brief A pipe for input and errorinput propagation(doesn't incurr copy) + * + * \ingroup CuNNMisc + */ + class CuPipe : public CuComponent + { + public: + CuPipe(size_t nInputs, size_t nOutputs, CuComponent* pPred) + : CuComponent(nInputs,nOutputs,pPred) + { } + + ~CuPipe() + { } + + ComponentType GetType() const + { return PIPE; } + + const char* GetName() const + { return ""; } + + void ReadFromStream(std::istream& rIn) + { } + + void WriteToStream(std::ostream& rOut) + { } + + void Propagate() + { + if (NULL == mpInput) Error("mpInput is NULL"); + mOutput.Init(*mpInput); + } + void BackPropagate() + { + if (NULL == mpErrorInput) Error("mpErrorInput is NULL"); + mErrorOutput.Init(*mpErrorInput); + } + + protected: + + void PropagateFnc(const CuMatrix& X, CuMatrix& Y) + { Y.CopyFrom(X);} + + void BackpropagateFnc(const CuMatrix& X, CuMatrix& Y) + { Y.CopyFrom(X); } + + }; + + /** + * \brief A pipe for input propagation(doesn't incurr copy) and set any error to zero + * + * \ingroup CuNNMisc + * + * @todo have to be set to zero on every pass(setup a common zeroed space?!) + */ + class CuLearnStop : public CuComponent + { + public: + CuLearnStop(size_t nInputs, size_t nOutputs, CuComponent* pPred) + : CuComponent(nInputs,nOutputs,pPred) + { } + + ~CuLearnStop() + { } + + ComponentType GetType() const + { return LEARNSTOP; } + + const char* GetName() const + { return ""; } + + void ReadFromStream(std::istream& rIn) + { } + + void WriteToStream(std::ostream& rOut) + { } + + void Propagate() + { + if (NULL == mpInput) Error("mpInput is NULL"); + mOutput.Init(*mpInput); + } + + protected: + + void PropagateFnc(const CuMatrix& X, CuMatrix& Y) + { Y.CopyFrom(X);} + + void BackpropagateFnc(const CuMatrix& X, CuMatrix& Y) + { Y.SetZero(); } + + }; + + /** + * \brief Distribute the input to several output + * + * \ingroup CuNNMisc + * + */ + class CuDistrib : public CuComponent + { + public: + CuDistrib(size_t nInputs, size_t nOutputs, CuComponent* pPred) + : CuComponent(nInputs,nOutputs,pPred),size(0),ErrInputVec() + { + } + + ~CuDistrib() + { } + + ComponentType GetType() const + { return DISTRIB; } + + const char* GetName() const + { return ""; } + + void ReadFromStream(std::istream& rIn) + { + rIn >> std::ws >> size; + ErrInputVec.clear(); + for (int i=0; i& GetErrorInput(int pos=0) + { + if (pos>=0 && pos& rErrorInput,int pos=0) + { + if (pos==0) + mpErrorInput=&rErrorInput; + if (pos>=0 && pos& X, CuMatrix& Y) + { Y.CopyFrom(X);} + + void BackpropagateFnc(const CuMatrix& X, CuMatrix& Y) + { + Y.SetZero(); + for (int i=0;i Scale; + }; + + /** + * \brief Combining(Adding) several inputs together + * + * \ingroup CuNNMisc + * + */ + class CuCombine : public CuComponent + { + public: + CuCombine(size_t nInputs, size_t nOutputs, CuComponent* pPred) + : CuComponent(nInputs,nOutputs,pPred),size(0),InputVec() + { + } + + ~CuCombine() + { } + + ComponentType GetType() const + { return COMBINE; } + + const char* GetName() const + { return ""; } + + void ReadFromStream(std::istream& rIn) + { + rIn >> std::ws >> size; + InputVec.clear(); + for (int i=0; i& GetInput(int pos=0) + { + if (pos>=0 && pos& rInput,int pos=0) + { + if (pos==0) + mpInput=&rInput; + if (pos>=0 && pos& X, CuMatrix& Y) + { + Y.SetZero(); + for (int i=0;i& X, CuMatrix& Y) + { + Y.CopyFrom(X); + } + + int size; + MatrixPtrVec InputVec; + }; + + /** + * \brief Divide the input matrix to several outputs + * + * \ingroup CuNNMisc + * + */ + class CuDivide : public CuComponent + { + public: + CuDivide(size_t nInputs, size_t nOutputs, CuComponent* pPred) + : CuComponent(nInputs,nOutputs,pPred),size(0) + { } + + ~CuDivide() + { } + + ComponentType GetType() const + { return DIVIDE; } + + const char* GetName() const + { return ""; } + + int GetOutSect() + { + return size; + } + + void ReadFromStream(std::istream& rIn) + { + int len; + for (int i=0; i> std::ws >> size; + OutputVec.clear(); + for (int i=0; i>len; + OutputVec.push_back(new CuMatrix()); + SectLen.push_back(len); + } + } + + void WriteToStream(std::ostream& rOut) + { + rOut<Init(*mpInput,loc,SectLen[i]); + loc+=SectLen[i]; + } + } + + protected: + + void PropagateFnc(const CuMatrix& X, CuMatrix& Y) + { Error("__func__ Nonsense"); } + + void BackpropagateFnc(const CuMatrix& X, CuMatrix& Y) + { + int loc=0; + for (int i=0;i SectLen; + + }; + + /** + * \brief Merge several input matrices to one single output + * + * \ingroup CuNNMisc + * + */ + class CuMerge : public CuComponent + { + public: + CuMerge(size_t nInputs, size_t nOutputs, CuComponent* pPred) + : CuComponent(nInputs,nOutputs,pPred) + { } + + ~CuMerge() + { } + + ComponentType GetType() const + { return MERGE; } + + const char* GetName() const + { return ""; } + + int GetInSect() + { + return size; + } + + void ReadFromStream(std::istream& rIn) + { + int len; + for (int i=0; i> std::ws >> size; + ErrorOutputVec.clear(); + for (int i=0; i>len; + ErrorOutputVec.push_back(new CuMatrix()); + SectLen.push_back(len); + } + } + + void WriteToStream(std::ostream& rOut) + { + rOut<Init(*mpErrorInput,loc,SectLen[i]); + loc+=SectLen[i]; + } + } + + protected: + + void PropagateFnc(const CuMatrix& X, CuMatrix& Y) + { + int loc=0; + for (int i=0;i& X, CuMatrix& Y) + { Error("__func__ Nonsense"); } + + int size; + + MatrixPtrVec InputVec; + MatrixPtrVec ErrorOutputVec; + std::vector SectLen; + + }; + + /** + * \brief Reordering several inputs + * + * \ingroup CuNNMisc + * + */ + class CuReorder : public CuComponent + { + public: + CuReorder(size_t nInputs, size_t nOutputs, CuComponent* pPred) + : CuComponent(nInputs,nOutputs,pPred) + { } + + ~CuReorder() + { } + + ComponentType GetType() const + { return REORDER; } + + const char* GetName() const + { return ""; } + + int GetInSect() + { + return size; + } + + int GetOutSect() + { + return size; + } + + void ReadFromStream(std::istream& rIn) + { + int pos; + for (int i=0; i> std::ws >> size; + Order.clear(); + PipeVec.clear(); + for (int i=0; i>pos; + Order.push_back(pos); + PipeVec.push_back(new CuPipe(0,0,NULL)); + } + } + + void WriteToStream(std::ostream& rOut) + { + rOut << size<< " "; + for (int i=0; iPropagate(); + } + + void Backpropagate() + { + if (NULL == mpErrorInput) Error("mpErrorInput is NULL"); + for (int i=0; iBackpropagate(); + } + + /// IO Data getters + CuMatrix& GetInput(int pos=0) + { + return PipeVec[pos]->GetInput(); + } + CuMatrix& GetOutput(int pos=0) + { + return PipeVec[Order[pos]]->GetOutput(); + } + CuMatrix& GetErrorInput(int pos=0) + { + return PipeVec[Order[pos]]->GetErrorInput(); + } + CuMatrix& GetErrorOutput(int pos=0) + { + return PipeVec[pos]->GetErrorOutput(); + } + + /// Set input vector (bind with the preceding NetworkComponent) + void SetInput(CuMatrix& rInput,int pos=0) + { + PipeVec[pos]->SetInput(rInput); + } + /// Set error input vector (bind with the following NetworkComponent) + void SetErrorInput(CuMatrix& rErrorInput,int pos=0) + { + PipeVec[Order[pos]]->SetErrorInput(rErrorInput); + } + + protected: + + void PropagateFnc(const CuMatrix& X, CuMatrix& Y) + { Error("__func__ Nonsense"); } + + void BackpropagateFnc(const CuMatrix& X, CuMatrix& Y) + { Error("__func__ Nonsense"); } + + int size; + + std::vector Order; + + std::vector< CuPipe* > PipeVec; + }; + +} //namespace + + + +#endif -- cgit v1.2.3-70-g09d2