00001
00005 #ifndef HAZY_SGD_H
00006 #define HAZY_SGD_H
00007
00008
00009 #include "skiing.h"
00010 #include "reservoir.h"
00011 #include "incremental_sgd.h"
00012 #include "storage_manager.h"
00013 #include "ondisk_storage_manager.h"
00014 #include "hybrid_storage_manager.h"
00015 #include "main_memory_storage_manager.h"
00016 #include <pthread.h>
00017
00018 #include <string>
00019
00021 #define LAMBDA 0.0001
00022
00027 template <class T>
00028 class Hazy_Sgd {
00029 protected:
00030 int _dim;
00031
00033 Skiing ski;
00034
00036 IncrementalSGD<T> *sgd;
00042 struct hazy_model hm;
00043
00045 Storage_Manager<T> *st_man;
00046
00048 pthread_rwlock_t _rwlock;
00049
00051 struct model _last_sort_model;
00052
00053 typedef std::map<std::string, Hazy_Database*> _connection_map;
00054 _connection_map connections;
00055
00059 void updateModel();
00060
00066 void holder_difference_models(double &delta_w, double &delta_b);
00067
00074 bool _high_low_water_valid;
00078 void update_low_high_water() {
00079 double delta_bias = 0.0, delta_w = 0.0;
00080 holder_difference_models(delta_w, delta_bias);
00081
00082
00083 double cond_plus = -(delta_bias - delta_w);
00084 double cond_neg = -(delta_bias + delta_w);
00085 hm.low_water = std::min(hm.low_water, cond_neg);
00086 hm.high_water = std::max(hm.high_water, cond_plus);
00087 LOGGING_ONLY(std::cout << "low & high water: " << hm.low_water << ", " << hm.high_water << std::endl;);
00088 }
00089 public:
00097 Hazy_Sgd(Storage_Manager<T> *sm, IncrementalSGD<T> *sgd, Skiing &ski, hazy_model::strategy s);
00103 void updateModel(T vec, sClass label);
00104
00105 void deleteModel(std::string db_name, std::string table_name);
00106
00112 void readEntityClass(key entity_id, sClass &c);
00118 void readEntityClass(T vec, sClass &label);
00125 void readEntityClass(T vec, double eps, sClass &label);
00126
00132 void readNumInClass(sClass c, int &nClass);
00133 };
00134 #include "hazy_sgd.hxx"
00135
00139 template<class T>
00140 class Read_Specialized_Hazy_Sgd : public Hazy_Sgd<T> {
00141 public:
00151 Read_Specialized_Hazy_Sgd(Storage_Manager<T> *sm, IncrementalSGD<T> *sgd, Skiing &_ski, hazy_model::strategy s,
00152 double _low_water, double _high_water) :
00153 Hazy_Sgd<T>(sm, sgd, _ski, s) {this->hm.low_water = _low_water; this->hm.high_water = _high_water;}
00154 };
00155
00159 template<class T>
00160 class Multiclass_Specialized_Hazy_Sgd : public Hazy_Sgd<T> {
00161 public:
00169 Multiclass_Specialized_Hazy_Sgd(Storage_Manager<T> *sm, IncrementalSGD<T> *sgd, Skiing ski, hazy_model::strategy s) :
00170 Hazy_Sgd<T>(sm, sgd, ski, s) {}
00171
00175 void resort() {
00176 Timer t(true);
00177 this->st_man->resort(this->hm);
00178 this->_last_sort_model = model(this->hm._model);
00179
00180 this->ski.doResort(t.stop());
00181 this->hm.low_water = 0.0, this->hm.high_water = 0.0;
00182 }
00183 };
00184
00185 #endif