1 /* 2 Copyright (c) by respective owners including Yahoo!, Microsoft, and 3 individual contributors. All rights reserved. Released under a BSD 4 license as described in the file LICENSE. 5 */ 6 #pragma once 7 #ifdef __FreeBSD__ 8 #include <sys/socket.h> 9 #endif 10 11 #include "parse_regressor.h" 12 #include "constant.h" 13 14 namespace GD{ 15 LEARNER::base_learner* setup(vw& all); 16 17 float finalize_prediction(shared_data* sd, float ret); 18 void print_audit_features(vw&, example& ec); 19 void save_load_regressor(vw& all, io_buf& model_file, bool read, bool text); 20 void save_load_online_state(vw& all, io_buf& model_file, bool read, bool text); 21 22 // iterate through one namespace (or its part), callback function T(some_data_R, feature_value_x, feature_weight) 23 template <class R, void (*T)(R&, const float, float&)> 24 inline void foreach_feature(weight* weight_vector, size_t weight_mask, feature* begin, feature* end, R& dat, uint32_t offset=0, float mult=1.) 25 { 26 for (feature* f = begin; f!= end; f++) 27 T(dat, mult*f->x, weight_vector[(f->weight_index + offset) & weight_mask]); 28 } 29 30 // iterate through one namespace (or its part), callback function T(some_data_R, feature_value_x, feature_index) 31 template <class R, void (*T)(R&, float, uint32_t)> 32 void foreach_feature(weight* weight_vector, size_t weight_mask, feature* begin, feature* end, R&dat, uint32_t offset=0, float mult=1.) 33 { 34 for (feature* f = begin; f!= end; f++) 35 T(dat, mult*f->x, f->weight_index + offset); 36 } 37 38 // iterate through all namespaces and quadratic&cubic features, callback function T(some_data_R, feature_value_x, S) 39 // where S is EITHER float& feature_weight OR uint32_t feature_index 40 template <class R, class S, void (*T)(R&, float, S)> foreach_feature(vw & all,example & ec,R & dat)41 inline void foreach_feature(vw& all, example& ec, R& dat) 42 { 43 uint32_t offset = ec.ft_offset; 44 45 for (unsigned char* i = ec.indices.begin; i != ec.indices.end; i++) 46 foreach_feature<R,T>(all.reg.weight_vector, all.reg.weight_mask, ec.atomics[*i].begin, ec.atomics[*i].end, dat, offset); 47 48 for (vector<string>::iterator i = all.pairs.begin(); i != all.pairs.end();i++) { 49 if (ec.atomics[(unsigned char)(*i)[0]].size() > 0) { 50 v_array<feature> temp = ec.atomics[(unsigned char)(*i)[0]]; 51 for (; temp.begin != temp.end; temp.begin++) 52 { 53 uint32_t halfhash = quadratic_constant * (temp.begin->weight_index + offset); 54 55 foreach_feature<R,T>(all.reg.weight_vector, all.reg.weight_mask, ec.atomics[(unsigned char)(*i)[1]].begin, ec.atomics[(unsigned char)(*i)[1]].end, dat, 56 halfhash, temp.begin->x); 57 } 58 } 59 } 60 61 for (vector<string>::iterator i = all.triples.begin(); i != all.triples.end();i++) { 62 if ((ec.atomics[(unsigned char)(*i)[0]].size() == 0) || (ec.atomics[(unsigned char)(*i)[1]].size() == 0) || (ec.atomics[(unsigned char)(*i)[2]].size() == 0)) { continue; } 63 v_array<feature> temp1 = ec.atomics[(unsigned char)(*i)[0]]; 64 for (; temp1.begin != temp1.end; temp1.begin++) { 65 v_array<feature> temp2 = ec.atomics[(unsigned char)(*i)[1]]; 66 for (; temp2.begin != temp2.end; temp2.begin++) { 67 68 uint32_t halfhash = cubic_constant2 * (cubic_constant * (temp1.begin->weight_index + offset) + temp2.begin->weight_index + offset); 69 float mult = temp1.begin->x * temp2.begin->x; 70 foreach_feature<R,T>(all.reg.weight_vector, all.reg.weight_mask, ec.atomics[(unsigned char)(*i)[2]].begin, ec.atomics[(unsigned char)(*i)[2]].end, dat, halfhash, mult); 71 } 72 } 73 } 74 } 75 76 // iterate through all namespaces and quadratic&cubic features, callback function T(some_data_R, feature_value_x, feature_weight) 77 template <class R, void (*T)(R&, float, float&)> foreach_feature(vw & all,example & ec,R & dat)78 inline void foreach_feature(vw& all, example& ec, R& dat) 79 { 80 foreach_feature<R,float&,T>(all, ec, dat); 81 } 82 vec_add(float & p,const float fx,float & fw)83 inline void vec_add(float& p, const float fx, float& fw) { p += fw * fx; } 84 inline_predict(vw & all,example & ec)85 inline float inline_predict(vw& all, example& ec) 86 { 87 float temp = ec.l.simple.initial; 88 foreach_feature<float, vec_add>(all, ec, temp); 89 return temp; 90 } 91 } 92