diff options
author | John Langford <jl@nyclamp.(none)> | 2014-01-10 22:18:14 +0400 |
---|---|---|
committer | John Langford <jl@nyclamp.(none)> | 2014-01-10 22:18:14 +0400 |
commit | 88c0725f6c515ddb7876f52917ee279a62625985 (patch) | |
tree | 1494e77328fbf94f6b0ba6545a594e3ab3be19d5 /vowpalwabbit/sparse_dense.h | |
parent | 18dd4ea36dc6eacac98049f99c27ce50a81121c2 (diff) |
remove reference to global datastructure on inner loop
Diffstat (limited to 'vowpalwabbit/sparse_dense.h')
-rw-r--r-- | vowpalwabbit/sparse_dense.h | 39 |
1 files changed, 25 insertions, 14 deletions
diff --git a/vowpalwabbit/sparse_dense.h b/vowpalwabbit/sparse_dense.h index ac0b7037..16e67553 100644 --- a/vowpalwabbit/sparse_dense.h +++ b/vowpalwabbit/sparse_dense.h @@ -10,22 +10,28 @@ license as described in the file LICENSE. #include "parse_example.h" #include "constant.h" +template<class R> +struct predict_data { + float prediction; + R extra; +}; + inline float sign(float w){ if (w < 0.) return -1.; else return 1.;} inline float trunc_weight(float w, float gravity){ return (gravity < fabsf(w)) ? w - sign(w) * gravity : 0.f; } -inline void vec_add(vw& all, float& p, float fx, uint32_t fi) { - p += all.reg.weight_vector[fi & all.reg.weight_mask] * fx; +inline void vec_add(vw& all, predict_data<float>& p, float fx, uint32_t fi) { + p.prediction += all.reg.weight_vector[fi & all.reg.weight_mask] * fx; } -inline void vec_add_trunc(vw& all, float& p, float fx, uint32_t fi) { - p += trunc_weight(all.reg.weight_vector[fi & all.reg.weight_mask], (float)all.sd->gravity) * fx; +inline void vec_add_trunc(vw& all, predict_data<float>& p, float fx, uint32_t fi) { + p.prediction += trunc_weight(all.reg.weight_vector[fi & all.reg.weight_mask], p.extra) * fx; } template <bool adaptive, size_t normalized_idx> -inline void vec_add_rescale(vw& all, float& p, float fx, uint32_t fi) { +inline void vec_add_rescale(vw& all, predict_data<float>& p, float fx, uint32_t fi) { weight* w = &all.reg.weight_vector[fi & all.reg.weight_mask]; float x_abs = fabs(fx); if( x_abs > w[normalized_idx] ) {// new scale discovered @@ -35,11 +41,11 @@ inline void vec_add_rescale(vw& all, float& p, float fx, uint32_t fi) { } w[normalized_idx] = x_abs; } - p += w[0] * fx; + p.prediction += w[0] * fx; } template <bool adaptive, size_t normalized_idx> -inline void vec_add_trunc_rescale(vw& all, float& p, float fx, uint32_t fi) { +inline void vec_add_trunc_rescale(vw& all, predict_data<float>& p, float fx, uint32_t fi) { weight* w = &all.reg.weight_vector[fi & all.reg.weight_mask]; float x_abs = fabs(fx); if( x_abs > w[normalized_idx] ) { @@ -49,14 +55,14 @@ inline void vec_add_trunc_rescale(vw& all, float& p, float fx, uint32_t fi) { } w[normalized_idx] = x_abs; } - p += trunc_weight(w[0], (float)all.sd->gravity) * fx; + p.prediction += trunc_weight(w[0], p.extra) * fx; } template <bool adaptive, size_t normalized_idx> -inline void vec_add_rescale_general(vw& all, float& p, float fx, uint32_t fi) { +inline void vec_add_rescale_general(vw& all, predict_data<float>& p, float fx, uint32_t fi) { weight* w = &all.reg.weight_vector[fi & all.reg.weight_mask]; float x_abs = fabs(fx); - float power_t_norm = 1.f - (adaptive ? all.power_t : 0.f); + float power_t_norm = 1.f - (adaptive ? p.extra : 0.f); if( x_abs > w[normalized_idx] ) { if( w[normalized_idx] > 0. ) { float rescale = (w[normalized_idx]/x_abs); @@ -64,14 +70,19 @@ inline void vec_add_rescale_general(vw& all, float& p, float fx, uint32_t fi) { } w[normalized_idx] = x_abs; } - p += w[0] * fx; + p.prediction += w[0] * fx; } +struct gnp { + float gravity; + float power_t; +}; + template <bool adaptive, size_t normalized_idx> -inline void vec_add_trunc_rescale_general(vw& all, float& p, float fx, uint32_t fi) { +inline void vec_add_trunc_rescale_general(vw& all, predict_data<gnp>& p, float fx, uint32_t fi) { weight* w = &all.reg.weight_vector[fi & all.reg.weight_mask]; float x_abs = fabs(fx); - float power_t_norm = 1.f - (adaptive ? all.power_t : 0.f); + float power_t_norm = 1.f - (adaptive ? p.extra.power_t : 0.f); if( x_abs > w[normalized_idx] ) { if( w[normalized_idx] > 0. ) { float rescale = (w[normalized_idx]/x_abs); @@ -79,7 +90,7 @@ inline void vec_add_trunc_rescale_general(vw& all, float& p, float fx, uint32_t } w[normalized_idx] = x_abs; } - p += trunc_weight(w[0], (float)all.sd->gravity) * fx; + p.prediction += trunc_weight(w[0], p.extra.gravity) * fx; } void sd_offset_update(weight* weights, size_t mask, feature* begin, feature* end, size_t offset, float update, float regularization); |