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
00026 #ifndef DeSR_AP_H
00027 #define DeSR_AP_H
00028
00029 #include <vector>
00030 #include <string>
00031
00032
00033 #include "text/WordIndex.h"
00034 #include "include/unordered_map.h"
00035
00040 #define AFFINE
00041
00043 typedef double Float;
00044 typedef unsigned FeatureID;
00045 typedef unsigned Y;
00046 typedef std::vector<FeatureID> X;
00047 typedef std::pair<X, Y> Case;
00048 typedef std::vector<Case> Cases;
00050
00055 BEGIN_NAMESPACE_HASH
00056 template<> struct hash<std::pair<unsigned, unsigned> > {
00057 size_t operator()(const std::pair<unsigned, unsigned>& x) const {
00058 unsigned a = x.first + ~(x.first << 13);
00059 unsigned b = x.second + ~(x.second << 9);
00060 a += (b >> 13);
00061 return a ^ b;
00062 }
00063 };
00064 END_NAMESPACE_HASH
00065
00066 class Fs {
00067 public:
00068 Float alpha;
00069 Float avg_num;
00070 int lst_upd;
00071
00075 inline Float weight(int it) const {
00076 return (avg_num + alpha * (it - lst_upd)) / it;
00077 }
00078
00079 inline void update(int it, Float tau) {
00080 avg_num += alpha * (it - lst_upd);
00081 lst_upd = it;
00082 alpha += tau;
00083 }
00084
00085 Fs(): alpha(0), avg_num(0), lst_upd(0) {}
00086 };
00087
00094 class AP {
00095 public:
00096 unsigned k;
00097 unsigned d;
00098 int t;
00099
00100 std::vector<std::string> labels;
00101 Tanl::Text::WordIndex predIndex;
00102
00107 void train(Cases& cases, int T);
00108
00110 int train(Case& cas);
00111
00113 Y predict(X& x);
00114
00116 std::vector<Float> scores(X& x);
00117
00119 size_t virtual size() { return 0; }
00120
00122 static float updatePercent;
00124 static bool verbose;
00126 static int partitionSize;
00127
00128 protected:
00129
00130 virtual void update(X& x, unsigned yt, std::vector<unsigned>& E) = 0;
00132 virtual Float score(unsigned i, X& v) = 0;
00133
00134 AP(int k = 0, int d = 0) : k(k), d(d), t(0) { }
00135
00139 virtual bool load(std::istream& is);
00140 };
00141
00142 typedef unordered_map<std::pair<unsigned, unsigned>, Fs> Matrix;
00143
00144 typedef unordered_map<unsigned, Fs> Row;
00145
00149 class APS : public AP {
00150 public:
00151 APS(int _Y_, int _X_);
00152
00154 APS(std::istream& is) { load(is); }
00155
00156 void update(X& x, unsigned yt, std::vector<unsigned>& E);
00157
00159 void save(std::ostream& os);
00160
00164 bool load(std::istream& is);
00165
00166 size_t size() { return M.size(); }
00167
00168 private:
00170 Float score(unsigned i, X& v);
00171
00172 Matrix M;
00173 };
00174
00178 class APSV : public AP
00179 {
00180 public:
00181 APSV() { }
00182
00183 APSV(int _Y_, int _X_);
00184
00185 void update(X& x, unsigned yt, std::vector<unsigned>& E);
00186
00188 APSV(std::istream& is) { load(is); }
00189
00191 void save(std::ostream& os);
00192
00196 bool load(std::istream& is);
00197
00198 size_t size() {
00199 size_t tot = 0;
00200 for (unsigned i = 0; i < M.size(); i++)
00201 tot += M[i].size();
00202 return tot;
00203 }
00204
00205 private:
00207 Float score(unsigned i, X& v);
00208
00209 std::vector<Row> M;
00210 };
00211
00215 class APD : public AP {
00216 public:
00217 APD(int _Y_, int _X_);
00218
00220 APD(std::istream& is) { load(is); }
00221
00222 void update(X& _x, unsigned yt, std::vector<unsigned>& E);
00223
00225 void save(std::ostream& os);
00226
00230 bool load(std::istream& is);
00231
00232 private:
00234 Float score(unsigned i, X& v);
00235
00236 std::vector<std::vector<Fs> > M;
00237 };
00238
00239 #endif // DeSR_AP_H