diff options
author | Hal Daume III <me@hal3.name> | 2013-01-10 20:41:21 +0400 |
---|---|---|
committer | Hal Daume III <me@hal3.name> | 2013-01-10 20:41:21 +0400 |
commit | f9efc43a252cd8143c10ce2e0e9819c6ad6cc613 (patch) | |
tree | 16e2c28f9b816db4bea71fe640e2d67c1542f5c8 /vowpalwabbit/sparse_dense.h | |
parent | acec3da2ce7fbb2e019b77feba309fbf43d5e85c (diff) |
templating sd_add
Diffstat (limited to 'vowpalwabbit/sparse_dense.h')
-rw-r--r-- | vowpalwabbit/sparse_dense.h | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/vowpalwabbit/sparse_dense.h b/vowpalwabbit/sparse_dense.h index 687488fe..b125a945 100644 --- a/vowpalwabbit/sparse_dense.h +++ b/vowpalwabbit/sparse_dense.h @@ -15,12 +15,85 @@ inline float trunc_weight(float w, float gravity){ return (gravity < fabsf(w)) ? w - sign(w) * gravity : 0.f; } +template <void (*T)(vw&,float,uint32_t,float&)> +void sd_add(vw& all, feature* begin, feature* end, float& ret) +{ + for (feature* f = begin; f!= end; f++) + T(all, f->x, f->weight_index, ret); +} + +inline void vec_add(vw& all, float fx, uint32_t fi, float& ret) { + ret += all.reg.weight_vectors[fi & all.weight_mask] * fx; +} + +inline void vec_add_trunc(vw& all, float fx, uint32_t fi, float& ret) { + ret += trunc_weight(all.reg.weight_vectors[fi & all.weight_mask], all.sd->gravity) * fx; +} + +inline void vec_add_rescale(vw& all, float fx, uint32_t fi, float& ret) { + weight* w = &all.reg.weight_vectors[fi & all.weight_mask]; + float x_abs = fabs(fx); + if( x_abs > w[all.normalized_idx] ) { + if( w[all.normalized_idx] > 0. ) { + float rescale = (w[all.normalized_idx]/x_abs); + w[0] *= (all.adaptive ? rescale : rescale*rescale); + } + w[all.normalized_idx] = x_abs; + } + ret += w[0] * fx; +} + +inline void vec_add_trunc_rescale(vw& all, float fx, uint32_t fi, float& ret) { + weight* w = &all.reg.weight_vectors[fi & all.weight_mask]; + float x_abs = fabs(fx); + if( x_abs > w[all.normalized_idx] ) { + if( w[all.normalized_idx] > 0. ) { + float rescale = (w[all.normalized_idx]/x_abs); + w[0] *= (all.adaptive ? rescale : rescale*rescale); + } + w[all.normalized_idx] = x_abs; + } + ret += trunc_weight(w[0], all.sd->gravity) * fx; +} + +inline void vec_add_rescale_general(vw& all, float fx, uint32_t fi, float& ret) { + weight* w = &all.reg.weight_vectors[fi & all.weight_mask]; + float x_abs = fabs(fx); + float power_t_norm = 1.f - (all.adaptive ? all.power_t : 0.f); + if( x_abs > w[all.normalized_idx] ) { + if( w[all.normalized_idx] > 0. ) { + float rescale = (w[all.normalized_idx]/x_abs); + w[0] *= powf(rescale*rescale,power_t_norm); + } + w[all.normalized_idx] = x_abs; + } + ret += w[0] * fx; +} + +inline void vec_add_trunc_rescale_general(vw& all, float fx, uint32_t fi, float& ret) { + weight* w = &all.reg.weight_vectors[fi & all.weight_mask]; + float x_abs = fabs(fx); + float power_t_norm = 1.f - (all.adaptive ? all.power_t : 0.f); + if( x_abs > w[all.normalized_idx] ) { + if( w[all.normalized_idx] > 0. ) { + float rescale = (w[all.normalized_idx]/x_abs); + w[0] *= powf(rescale*rescale,power_t_norm); + } + w[all.normalized_idx] = x_abs; + } + ret += trunc_weight(w[0], all.sd->gravity) * fx; +} + + + +/* float sd_add(weight* weights, size_t mask, feature* begin, feature* end); float sd_add_trunc(weight* weights, size_t mask, feature* begin, feature* end, float gravity); float sd_add_rescale(weight* weights, size_t mask, feature* begin, feature* end, bool is_adaptive, size_t idx_norm); float sd_add_trunc_rescale(weight* weights, size_t mask, feature* begin, feature* end, float gravity, bool is_adaptive, size_t idx_norm); float sd_add_rescale_general(weight* weights, size_t mask, feature* begin, feature* end, size_t idx_norm, float power_t_norm); float sd_add_trunc_rescale_general(weight* weights, size_t mask, feature* begin, feature* end, float gravity, size_t idx_norm, float power_t_norm); +*/ float sd_offset_add(weight* weights, size_t mask, feature* begin, feature* end, size_t offset); void sd_offset_update(weight* weights, size_t mask, feature* begin, feature* end, size_t offset, float update, float regularization); |