SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
VwAdaptiveLearner.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 Yahoo! Inc. All rights reserved. The copyrights
3  * embodied in the content of this file are licensed under the BSD
4  * (revised) open source license.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Written (W) 2011 Shashwat Lal Das
12  * Adaptation of Vowpal Wabbit v5.1.
13  * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society.
14  */
15 
17 
18 using namespace shogun;
19 
21  : CVwLearner()
22 {
23 }
24 
26  : CVwLearner(regressor, vw_env)
27 {
28 }
29 
31 {
32 }
33 
35 {
36  if (fabs(update) == 0.)
37  return;
38 
39  vw_size_t thread_num = 0;
40 
41  vw_size_t thread_mask = env->thread_mask;
42  float32_t* weights = reg->weight_vectors[thread_num];
43 
45  vw_size_t ctr = 0;
46  for (vw_size_t* i = ex->indices.begin; i != ex->indices.end; i++)
47  {
48  for (VwFeature *f = ex->atomics[*i].begin; f != ex->atomics[*i].end; f++)
49  {
50  float32_t* w = &weights[f->weight_index & thread_mask];
51  w[1] += g * f->x * f->x;
52  float32_t t = f->x * CMath::invsqrt(w[1]);
53  w[0] += update * t;
54  }
55  }
56 
57  for (int32_t k = 0; k < env->pairs.get_num_elements(); k++)
58  {
59  char* i = env->pairs.get_element(k);
60 
61  v_array<VwFeature> temp = ex->atomics[(int32_t)(i[0])];
62  temp.begin = ex->atomics[(int32_t)(i[0])].begin;
63  temp.end = ex->atomics[(int32_t)(i[0])].end;
64  for (; temp.begin != temp.end; temp.begin++)
65  quad_update(weights, *temp.begin, ex->atomics[(int32_t)(i[1])], thread_mask, update, g, ex, ctr);
66  }
67 }
68 
69 void CVwAdaptiveLearner::quad_update(float32_t* weights, VwFeature& page_feature,
70  v_array<VwFeature> &offer_features, vw_size_t mask,
72 {
73  vw_size_t halfhash = quadratic_constant * page_feature.weight_index;
74  update *= page_feature.x;
75  float32_t update2 = g * page_feature.x * page_feature.x;
76 
77  for (VwFeature* elem = offer_features.begin; elem != offer_features.end; elem++)
78  {
79  float32_t* w = &weights[(halfhash + elem->weight_index) & mask];
80  w[1] += update2 * elem->x * elem->x;
81  float32_t t = elem->x * CMath::invsqrt(w[1]);
82  w[0] += update * t;
83  }
84 }
uint32_t weight_index
Hashed index in weight vector.
Definition: vw_example.h:41
uint32_t vw_size_t
vw_size_t typedef to work across platforms
Definition: vw_constants.h:26
T get_element(int32_t index) const
Definition: DynArray.h:142
T * end
Pointer to last set element in the array.
Definition: v_array.h:160
T * begin
Pointer to first element of the array.
Definition: v_array.h:157
Class CVwEnvironment is the environment used by VW.
Definition: VwEnvironment.h:41
CLossFunction * loss
Loss function.
Definition: VwRegressor.h:118
void(* update)(float *foo, float bar)
Definition: JLCoverTree.h:528
Class v_array taken directly from JL's implementation.
CVwRegressor * reg
Regressor object that will be used for getting updates.
Definition: VwLearner.h:100
float32_t ** weight_vectors
Weight vectors, one array for each thread.
Definition: VwRegressor.h:116
int32_t get_num_elements() const
Definition: DynArray.h:130
const int32_t quadratic_constant
Constant used while hashing/accessing quadratic features.
Definition: vw_constants.h:29
float32_t label
Label value.
Definition: vw_label.h:92
v_array< vw_size_t > indices
Array of namespaces.
Definition: vw_example.h:84
static float32_t invsqrt(float32_t x)
x^0.5, x being a complex128_t
Definition: Math.h:495
float32_t weight
Weight of example.
Definition: vw_label.h:94
Base class for all VW learners.
Definition: VwLearner.h:35
DynArray< char * > pairs
Pairs of features to cross for quadratic updates.
float32_t final_prediction
Final prediction.
Definition: vw_example.h:93
Regressor used by VW.
Definition: VwRegressor.h:37
Example class for VW.
Definition: vw_example.h:58
virtual void train(VwExample *&ex, float32_t update)
float float32_t
Definition: common.h:49
One feature in VW.
Definition: vw_example.h:34
float32_t x
Feature value.
Definition: vw_example.h:38
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
CVwEnvironment * env
Environment.
Definition: VwLearner.h:102
VwLabel * ld
Label object.
Definition: vw_example.h:79
vw_size_t thread_mask
Mask used by regressor for learning.
virtual float64_t get_square_grad(float64_t prediction, float64_t label)=0
v_array< VwFeature > atomics[256]
Array of features.
Definition: vw_example.h:86

SHOGUN Machine Learning Toolbox - Documentation