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_APA_H
00026 #define DeSR_APA_H
00027
00028 #include <iostream>
00029 #include <vector>
00030
00031
00032 #include "text/WordIndex.h"
00033
00035 typedef double Float;
00036 typedef unsigned FeatureID;
00037 typedef unsigned Y;
00038 typedef std::vector<FeatureID> X;
00039 typedef std::pair<X, Y> Case;
00040 typedef std::vector<Case> Cases;
00042
00047 BEGIN_NAMESPACE_HASH
00048 template<> struct hash<std::pair<unsigned, unsigned> > {
00049 size_t operator()(const std::pair<unsigned, unsigned>& x) const {
00050 unsigned a = x.first + ~(x.first << 13);
00051 unsigned b = x.second + ~(x.second << 9);
00052 a += (b >> 13);
00053 return a ^ b;
00054 }
00055 };
00056 END_NAMESPACE_HASH
00057
00058 class Fs {
00059 public:
00060 Float alpha;
00061 Float avg_num;
00062 int lst_upd;
00063
00067 inline Float weight(int it) const {
00068 #ifdef AVG
00069 return (avg_num + alpha * (it - lst_upd)) / it;
00070 #else
00071 return alpha;
00072 #endif
00073 }
00074
00075 inline void update(int it, Float upd_val) {
00076 avg_num += alpha * (it - lst_upd);
00077 lst_upd = it;
00078 alpha += upd_val;
00079 }
00080
00081 Fs(): alpha(0), avg_num(0), lst_upd(0) {}
00082 };
00083
00091 class APA {
00092 public:
00093 unsigned k;
00094 unsigned d;
00095 int t;
00096
00097 std::vector<std::string> labels;
00098 Tanl::Text::WordIndex predIndex;
00099
00100 void train(Cases& cases, int T);
00101
00102 Y predict(X& x);
00103
00105 size_t virtual size() { }
00106
00108 virtual void save(std::ostream& os) = 0;
00109
00110 static Float C;
00111 static Float ka;
00112 static int kd;
00113
00115 Float margin;
00116
00118 static bool verbose;
00119 static float updatePercent;
00120
00121 protected:
00122 static void rand_permutation(std::vector<int>& OUT);
00123
00124
00125 virtual void update(Y y, Float tau, X& x) = 0;
00126
00128 virtual Float score(unsigned i, X& v) = 0;
00129
00130 APA(int k = 0, int d = 0) : k(k), d(d), t(0) { }
00131
00135 virtual bool load(std::istream& is) = 0;
00136
00137 };
00138
00139 typedef unordered_map<std::pair<unsigned, unsigned>, Fs> Matrix;
00140
00141 typedef unordered_map<unsigned, Fs> Row;
00142
00146 class APAS : public APA {
00147 public:
00148 APAS(int _Y_, int _X_);
00149 void update(Y y, Float tau, X& x);
00150
00152 void save(std::ostream& os);
00153
00157 bool load(std::istream& is);
00158
00159 size_t size() { return M.size(); }
00160
00161 private:
00162 void init(int _Y_, int _X_);
00163
00165 Float score(unsigned i, X& v);
00166
00167 Matrix M;
00168 };
00169
00173 class APASV : public APA {
00174 public:
00175 APASV() { }
00176
00177 APASV(int _Y_, int _X_);
00178 void update(Y y, Float tau, X& x);
00179
00181 APASV(char const* file);
00182
00184 void save(std::ostream& os);
00185
00189 bool load(std::istream& is);
00190
00191 size_t size() {
00192 size_t tot = 0;
00193 for (int i = 0; i < M.size(); i++)
00194 tot += M[i].size();
00195 return tot;
00196 }
00197
00198 private:
00200 Float score(unsigned i, X& v);
00201
00202 void init(int k, int d);
00203
00204 std::vector<Row> M;
00205 };
00206
00207 #endif // DeSR_APA_H