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/TNetLib/.svn/text-base/CRBEDctFeat.h.svn-base | 432 ++++++++++++++++++++++ 1 file changed, 432 insertions(+) create mode 100644 src/TNetLib/.svn/text-base/CRBEDctFeat.h.svn-base (limited to 'src/TNetLib/.svn/text-base/CRBEDctFeat.h.svn-base') diff --git a/src/TNetLib/.svn/text-base/CRBEDctFeat.h.svn-base b/src/TNetLib/.svn/text-base/CRBEDctFeat.h.svn-base new file mode 100644 index 0000000..0984c36 --- /dev/null +++ b/src/TNetLib/.svn/text-base/CRBEDctFeat.h.svn-base @@ -0,0 +1,432 @@ +#ifndef _CUCRBEDCTFEATURES_H_ +#define _CUCRBEDCTFEATURES_H_ + + +#include "Component.h" +#include "Matrix.h" +#include "Vector.h" +#include "cblas.h" + + +namespace TNet { + + /** + * Expands the time context of the input features + * in N, out k*N, FrameOffset o_1,o_2,...,o_k + * FrameOffset example 11frames: -5 -4 -3 -2 -1 0 1 2 3 4 5 + */ + class Expand : public Component + { + public: + Expand(size_t nInputs, size_t nOutputs, Component* pPred) + : Component(nInputs,nOutputs,pPred) + { } + + ~Expand() + { } + + ComponentType GetType() const + { return EXPAND; } + + const char* GetName() const + { return ""; } + + Component* Clone() const + { + Expand* p = new Expand(GetNInputs(),GetNOutputs(),NULL); + p->mFrameOffset.Init(mFrameOffset.Dim()); + p->mFrameOffset.Copy(mFrameOffset); + return p; + } + + void ReadFromStream(std::istream& rIn) + { rIn >> mFrameOffset; } + + void WriteToStream(std::ostream& rOut) + { rOut << mFrameOffset; } + + protected: + void PropagateFnc(const Matrix& X, Matrix& Y) + { + assert(X.Cols()*mFrameOffset.Dim() == Y.Cols()); + assert(X.Rows() == Y.Rows()); + + for(size_t r=0;r= X.Rows()) r_off = X.Rows()-1; + memcpy(Y.pRowData(r)+off*X.Cols(),X.pRowData(r_off),sizeof(BaseFloat)*X.Cols()); + } + } + } + + void BackpropagateFnc(const Matrix& X, Matrix& Y) + { Error("__func__ Nonsense"); } + + protected: + Vector mFrameOffset; + }; + + + + /** + * Rearrange the matrix columns according to the indices in mCopyFromIndices + */ + class Copy : public Component + { + public: + Copy(size_t nInputs, size_t nOutputs, Component* pPred) + : Component(nInputs,nOutputs,pPred) + { } + + ~Copy() + { } + + ComponentType GetType() const + { return COPY; } + + const char* GetName() const + { return ""; } + + Component* Clone() const + { + Copy* p = new Copy(GetNInputs(),GetNOutputs(),NULL); + p->mCopyFromIndices.Init(mCopyFromIndices.Dim()); + p->mCopyFromIndices.Copy(mCopyFromIndices); + return p; + } + + void ReadFromStream(std::istream& rIn) + { + Vector vec; rIn >> vec; vec.Add(-1); + mCopyFromIndices.Init(vec.Dim()).Copy(vec); + } + + void WriteToStream(std::ostream& rOut) + { + Vector vec(mCopyFromIndices); + vec.Add(1); rOut << vec; + } + + protected: + void PropagateFnc(const Matrix& X, Matrix& Y) + { + assert(mCopyFromIndices.Dim() == Y.Cols()); + for(int i=0; i= 0 && mCopyFromIndices[i] < X.Cols()); + } + + for(size_t r=0; r& X, Matrix& Y) + { Error("__func__ Nonsense"); } + + protected: + Vector mCopyFromIndices; + }; + + class Transpose : public Component + { + public: + Transpose(size_t nInputs, size_t nOutputs, Component* pPred) + : Component(nInputs,nOutputs,pPred), mContext(0) + { } + + ~Transpose() + { } + + ComponentType GetType() const + { return TRANSPOSE; } + + const char* GetName() const + { return ""; } + + Component* Clone() const + { + Transpose* p = new Transpose(GetNInputs(),GetNOutputs(),NULL); + p->mCopyFromIndices.Init(mCopyFromIndices.Dim()); + p->mCopyFromIndices.Copy(mCopyFromIndices); + p->mContext = mContext; + return p; + } + + void ReadFromStream(std::istream& rIn) + { + rIn >> std::ws >> mContext; + + if(GetNInputs() != GetNOutputs()) { + Error("Input dim must be same as output dim"); + } + + Vector vec(GetNInputs()); + int channels = GetNInputs() / mContext; + for(int i=0, ch=0; ch& X, Matrix& Y) + { + assert(mCopyFromIndices.Dim() == Y.Cols()); + for(int i=0; i= 0 && mCopyFromIndices[i] < X.Cols()); + } + + for(size_t r=0; r& X, Matrix& Y) + { Error("__func__ Nonsense"); } + + protected: + int mContext; + Vector mCopyFromIndices; + }; + + + /** + * BlockLinearity is used for the blockwise multiplication by + * DCT transform loaded from disk + */ + class BlockLinearity : public Component + { + public: + BlockLinearity(size_t nInputs, size_t nOutputs, Component* pPred) + : Component(nInputs,nOutputs,pPred) + { } + + ~BlockLinearity() + { } + + + ComponentType GetType() const + { return Component::BLOCK_LINEARITY; } + + const char* GetName() const + { return ""; } + + Component* Clone() const + { + BlockLinearity* p = new BlockLinearity(GetNInputs(),GetNOutputs(),NULL); + p->mBlockLinearity.Init(mBlockLinearity.Rows(),mBlockLinearity.Cols()); + p->mBlockLinearity.Copy(mBlockLinearity); + return p; + } + + void PropagateFnc(const Matrix& X, Matrix& Y) + { + assert(X.Rows() == Y.Rows()); + assert(X.Cols()%mBlockLinearity.Rows() == 0); + assert(Y.Cols()%mBlockLinearity.Cols() == 0); + assert(X.Cols()/mBlockLinearity.Rows() == Y.Cols()/mBlockLinearity.Cols()); + + int instN = X.Cols()/mBlockLinearity.Rows(); + for(int inst=0; inst& X, Matrix& Y) + { Error("__func__ Not implemented"); } + + + void ReadFromStream(std::istream& rIn) + { + Matrix mat; + rIn >> mat; + Matrix trans(mat,TRANS); + mBlockLinearity.Init(trans.Rows(),trans.Cols()).Copy(trans); + + if((GetNOutputs() % mBlockLinearity.Cols() != 0) || + (GetNInputs() % mBlockLinearity.Rows() != 0) || + ((GetNOutputs() / mBlockLinearity.Cols()) != + (GetNInputs() / mBlockLinearity.Rows()))) + { + Error("BlockLinearity matrix dimensions must divide IO dims"); + } + } + + void WriteToStream(std::ostream& rOut) + { + Matrix trans(mBlockLinearity,TRANS); + rOut << trans; + } + + private: + Matrix mBlockLinearity; + }; + + + + class Bias : public Component + { + public: + Bias(size_t nInputs, size_t nOutputs, Component* pPred) + : Component(nInputs,nOutputs,pPred) + { } + + ~Bias() + { } + + + ComponentType GetType() const + { return Component::BIAS; } + + const char* GetName() const + { return ""; } + + Component* Clone() const + { + Bias* p = new Bias(GetNInputs(),GetNOutputs(),NULL); + p->mBias.Init(mBias.Dim()); + p->mBias.Copy(mBias); + return p; + } + + void PropagateFnc(const Matrix& X, Matrix& Y) + { + Y.Copy(X); + for(size_t r=0; r& X, Matrix& Y) + { Y.Copy(X); } + + + void ReadFromStream(std::istream& rIn) + { rIn >> mBias; } + + void WriteToStream(std::ostream& rOut) + { rOut << mBias; } + + private: + Vector mBias; + }; + + + + class Window : public Component + { + public: + Window(size_t nInputs, size_t nOutputs, Component* pPred) + : Component(nInputs, nOutputs, pPred) + { } + + ~Window() + { } + + + ComponentType GetType() const + { return Component::WINDOW; } + + const char* GetName() const + { return ""; } + + Component* Clone() const + { + Window* p = new Window(GetNInputs(),GetNOutputs(),NULL); + p->mWindow.Init(mWindow.Dim()); + p->mWindow.Copy(mWindow); + return p; + } + + + void PropagateFnc(const Matrix& X, Matrix& Y) + { Y.Copy(X); + for(size_t r=0; r& X, Matrix& Y) + { Error("__func__ Not implemented"); } + + + void ReadFromStream(std::istream& rIn) + { rIn >> mWindow; } + + void WriteToStream(std::ostream& rOut) + { rOut << mWindow; } + + private: + Vector mWindow; + }; + + class Log : public Component + { + public: + Log(size_t nInputs, size_t nOutputs, Component* pPred) + : Component(nInputs, nOutputs, pPred) + { } + + ~Log() + { } + + + ComponentType GetType() const + { return Component::LOG; } + + const char* GetName() const + { return ""; } + + Component* Clone() const + { return new Log(GetNInputs(),GetNOutputs(),NULL); } + + + void PropagateFnc(const Matrix& X, Matrix& Y) + { Y.Copy(X); Y.ApplyLog(); } + + void BackpropagateFnc(const Matrix& X, Matrix& Y) + { Error("__func__ Not implemented"); } + + + void ReadFromStream(std::istream& rIn) + { } + + void WriteToStream(std::ostream& rOut) + { } + + }; + +} + + +#endif + -- cgit v1.2.3-70-g09d2