00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef DeSR_PA_H
00026 #define DeSR_PA_H
00027
00028
00029 #include <iostream>
00030 #include <vector>
00031
00032
00033 #include "text/WordIndex.h"
00034 #include "include/unordered_map.h"
00035
00037 typedef double Float;
00038 typedef unsigned FeatureID;
00039 typedef unsigned Y;
00040 typedef std::vector<FeatureID> X;
00041 typedef std::pair<X, Y> Case;
00042 typedef std::vector<Case> Cases;
00044
00049 BEGIN_NAMESPACE_HASH
00050 template<> struct hash<std::pair<unsigned, unsigned> > {
00051 size_t operator()(const std::pair<unsigned, unsigned>& x) const {
00052 unsigned a = x.first + ~(x.first << 13);
00053 unsigned b = x.second + ~(x.second << 9);
00054 a += (b >> 13);
00055 return a ^ b;
00056 }
00057 };
00058 END_NAMESPACE_HASH
00059
00067 class PA {
00068 public:
00069 unsigned k;
00070 unsigned d;
00071 int t;
00072
00073 std::vector<std::string> labels;
00074 Tanl::Text::WordIndex predIndex;
00075
00076 void train(Cases& cases, int T);
00077
00078 Y predict(X& x);
00079
00081 size_t virtual size() { }
00082
00084 virtual void save(std::ostream& os) = 0;
00085
00086 static void rand_permutation(std::vector<int>& OUT);
00087
00089 Float margin;
00090
00092 static bool verbose;
00093 static float updatePercent;
00094
00095 protected:
00096
00097 virtual void update(Y y, Float tau, X& x) = 0;
00098
00100 virtual Float score(unsigned i, X& v) = 0;
00101
00102 PA(int k = 0, int d = 0) : k(k), d(d), t(0) { }
00103
00107 virtual bool load(std::istream& is) = 0;
00108
00109 };
00110
00111 typedef unordered_map<std::pair<unsigned, unsigned>, Float> Matrix;
00112
00113 typedef unordered_map<unsigned, Float> Row;
00114
00118 class PAS : public PA {
00119 public:
00120 PAS(int _Y_, int _X_);
00121 void update(Y y, Float tau, X& x);
00122
00124 void save(std::ostream& os);
00125
00129 bool load(std::istream& is);
00130
00131 size_t size() { return M.size(); }
00132
00133 private:
00134 void init(int _Y_, int _X_);
00135
00137 Float score(unsigned i, X& v);
00138
00139 Matrix M;
00140 };
00141
00145 class PASV : public PA {
00146 public:
00147 PASV(int _Y_, int _X_);
00148 void update(Y y, Float tau, X& x);
00149
00151 PASV(char const* file);
00152
00154 void save(std::ostream& os);
00155
00159 bool load(std::istream& is);
00160
00161 size_t size() {
00162 size_t tot = 0;
00163 for (int i = 0; i < M.size(); i++)
00164 tot += M[i].size();
00165 return tot;
00166 }
00167
00168 private:
00170 Float score(unsigned i, X& v);
00171
00172 void init(int k, int d);
00173
00174 std::vector<Row> M;
00175 };
00176
00177 #ifdef MESCHACH
00178
00181 class PASM : public PA {
00182 public:
00183 PASM(int _Y_, int _X_);
00184 void update(Y y, Float tau, X& x);
00185
00187 PASM(char const* file);
00188
00190 void save(std::ostream& os);
00191
00195 bool load(std::istream& is);
00196
00197 size_t size() {
00198 size_t tot = 0;
00199 for (int i = 0; i < M.size(); i++)
00200 tot += M[i].size();
00201 return tot;
00202 }
00203
00204 private:
00206 Float score(unsigned i, X& v);
00207
00208 void init(int k, int d);
00209
00210 SPMAT* M;
00211 };
00212 #endif
00213
00217 class PAD : public PA {
00218 public:
00219 PAD(int _Y_, int _X_);
00220 void update(Y y, Float tau, X& x);
00221
00223 void save(std::ostream& os);
00224
00228 bool load(std::istream& is);
00229
00230 private:
00232 Float score(unsigned i, X& v);
00233
00234 std::vector<std::vector<Float> > M;
00235 };
00236
00237 #endif // DeSR_PA_H