/////////////////////////////////////////////////////////////////////////////// // // // This file is part of ModelBlocks. Copyright 2009, ModelBlocks developers. // // // // ModelBlocks is free software: you can redistribute it and/or modify // // it under the terms of the GNU General Public License as published by // // the Free Software Foundation, either version 3 of the License, or // // (at your option) any later version. // // // // ModelBlocks is distributed in the hope that it will be useful, // // but WITHOUT ANY WARRANTY; without even the implied warranty of // // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // // GNU General Public License for more details. // // // // You should have received a copy of the GNU General Public License // // along with ModelBlocks. If not, see . // // // // ModelBlocks developers designate this particular file as subject to // // the "Moses" exception as provided by ModelBlocks developers in // // the LICENSE file that accompanies this code. // // // /////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// template class QuadConvolved : public X { public: typedef typename X::ElementType ElementType; static const unsigned int SIZE = X::SIZE+1; private: ElementType eC; public: QuadConvolved ( ) : eC(1.0) { } ElementType& operator[] ( unsigned int i ) { assert(i*> operator>> ( StringInput si, QuadConvolved& x ) { return pair*>(si,&x); } friend StringInput operator>> ( pair*> si_x, const char* psD ) { StringInput si = si_x.first>>static_cast(*si_x.second)>>psD; si_x.second->set(SIZE-1)=0.0; for(int i=0;iset(SIZE-1)+=pow(si_x.second->get(i)/1000000.0,2.0); return si; } friend ostream& operator<< ( ostream& os, const QuadConvolved& x ) { os< class Shifted : public X { public: typedef typename X::ElementType ElementType; static const unsigned int SIZE = X::SIZE+1; private: ElementType e0; public: Shifted ( ) : e0(0.0) { } ElementType& operator[] ( unsigned int i ) { assert(i*> operator>> ( StringInput si, Shifted& x ) { return pair*>(si,&x); } //friend StringInput operator>> ( pair*> si_x, const char* psD ) { // StringInput si=si_x.first; for(int i=0;i>si_x.second->set(i)>>((i> ( pair*> si_x, const char* psD ) { StringInput si = si_x.first>>static_cast(*si_x.second)>>psD; si_x.second->set(SIZE-1)=1.0; return si; } //friend ostream& operator<< ( ostream& os, const Shifted& x ) { os<& x ) { if(x.get(SIZE-1)!=1.0)for(uint i=1;i class Vector : public X { public: typedef typename X::ElementType ElementType; static const unsigned int SIZE = X::SIZE; Vector ( ) { } Vector ( const Vector& v ) { for(uint i=0;i& v ) const { ElementType d=0.0; for(uint i=0;i operator+ ( const Vector& v ) const { Vector vO; for(uint i=0;i operator- ( const Vector& v ) const { Vector vO; for(uint i=0;i operator* ( ElementType d ) const { Vector vO; for(uint i=0;i operator/ ( ElementType d ) const { Vector vO; for(uint i=0;i operator+ ( ElementType d ) const { Vector vO; for(uint i=0;i operator- ( ElementType d ) const { Vector vO; for(uint i=0;i operator* ( ElementType d, const Vector& v ) { Vector vO; for(uint i=0;i operator/ ( ElementType d, const Vector& v ) { Vector vO; for(uint i=0;i operator+ ( ElementType d, const Vector& v ) { Vector vO; for(uint i=0;i operator- ( ElementType d, const Vector& v ) { Vector vO; for(uint i=0;i& operator*= ( ElementType d ) { for(uint i=0;i& operator/= ( ElementType d ) { for(uint i=0;i& operator+= ( ElementType d ) { for(uint i=0;i& operator-= ( ElementType d ) { for(uint i=0;i& operator+= ( const Vector& v ) { for(uint i=0;i& operator-= ( const Vector& v ) { for(uint i=0;i class LinSepModel { public: class CondVarType : public Vector > { public: CondVarType(){} CondVarType(const Vector >& v):Vector >(v){} }; class InputTrainingExample : public Joint2DRV { // public: // friend pair operator>> ( StringInput si, InputTrainingExample& x ) { return pair(si,&x); } // friend StringInput operator>> ( pair si_x, const char* psD ) { // StringInput si = si_x.first>>static_cast(*si_x.second)>>psD; si_x.second->set(SIZE-1)=1.0; return si; } }; typedef P ProbType; CondVarType w; // weight vector private: typename X::ElementType sigmoid ( double a ) { return (!isnan(1.0/(1.0+exp(-a)))) ? 1.0/(1.0+exp(-a)) : (a>0.0) ? 1.0 : 0.0; } public: template void train ( const SubArray > apyx, F& yfn, double iP, double iN ) { const double SCALER = 1000.0; // 1000.0 works just as well //cerr<<" ---"< wtx(apyx.size(),0.0); // weighted sum for ( int k=0; k<100 && err>0.0 && (errPrev-err>0.001||k<=3); k++ ) { errPrev = err; ////cerr<<" ? "<.001 && t<1; t++ ) { double a = 0.0; double b = 0.0; ////cerr<<" w = "< z="< 0.0 ); } friend ostream& operator<< ( ostream& os, const LinSepModel& m ) { return os<*> operator>> ( StringInput si, LinSepModel& m ) { return pair*>(si,&m); } friend StringInput operator>> ( pair*> si_m, const char* psD ) { return (si_m.first==NULL) ? StringInput(NULL) : si_m.first>>si_m.second->w>>psD; } }; //////////////////////////////////////////////////////////////////////////////// template class SimpleLinSepModel { public: class CondVarType : public Vector { }; class InputTrainingExample : public Joint2DRV { // public: // friend pair operator>> ( StringInput si, InputTrainingExample& x ) { return pair(si,&x); } // friend StringInput operator>> ( pair si_x, const char* psD ) { // StringInput si = si_x.first>>static_cast(*si_x.second)>>psD; si_x.second->set(SIZE-1)=1.0; return si; } }; typedef P ProbType; CondVarType w; // weight vector private: typename X::ElementType sigmoid ( double a ) { return 1.0/(1.0+exp(-a)); } public: ////void train ( const SafePtr apyx[], const int N, bool fn(const Q&), double iPnotused, double iNnotused ) { template void train ( const SubArray > apyx, F& yfn, double iPnotused, double iNnotused ) { double dIntercept = 0.0; CondVarType vP,vN,vDelt,vMid; // pos/neg centroids, difference, midpoint int iP=0,iN=0; // pos/neg counts // For each example... for ( unsigned int i=0; i0 ) for ( unsigned int j=0; j0 ) for ( unsigned int j=0; j -1.0)... for ( unsigned int j=0; j -1.0 ); } friend ostream& operator<< ( ostream& os, const SimpleLinSepModel& m ) { return os<*> operator>> ( StringInput si, SimpleLinSepModel& m ) { return pair*>(si,&m); } friend StringInput operator>> ( pair*> si_m, const char* psD ) { return (si_m.first==NULL) ? StringInput(NULL) : si_m.first>>si_m.second->w>>psD; } };