SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
VwNonAdaptiveLearner.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  vw_size_t thread_mask = env->thread_mask;
39 
40  vw_size_t thread_num = 0;
41  float32_t* weights = reg->weight_vectors[thread_num];
42 
43  for (vw_size_t* i = ex->indices.begin; i != ex->indices.end; i++)
44  {
45  for (VwFeature* f = ex->atomics[*i].begin; f != ex->atomics[*i].end; f++)
46  weights[f->weight_index & thread_mask] += update * f->x;
47  }
48 
49  for (int32_t k = 0; k < env->pairs.get_num_elements(); k++)
50  {
51  char* i = env->pairs.get_element(k);
52 
53  v_array<VwFeature> temp = ex->atomics[(int32_t)(i[0])];
54  temp.begin = ex->atomics[(int32_t)(i[0])].begin;
55  temp.end = ex->atomics[(int32_t)(i[0])].end;
56  for (; temp.begin != temp.end; temp.begin++)
57  quad_update(weights, *temp.begin, ex->atomics[(int32_t)(i[1])], thread_mask, update);
58  }
59 }
60 
61 void CVwNonAdaptiveLearner::quad_update(float32_t* weights, VwFeature& page_feature, v_array<VwFeature> &offer_features, vw_size_t mask, float32_t update)
62 {
63  vw_size_t halfhash = quadratic_constant * page_feature.weight_index;
64  update *= page_feature.x;
65  for (VwFeature* elem = offer_features.begin; elem != offer_features.end; elem++)
66  weights[(halfhash + elem->weight_index) & mask] += update * elem->x;
67 }
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
virtual void train(VwExample *&ex, float32_t update)
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
void(* update)(float *foo, float bar)
Definition: JLCoverTree.h:529
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
v_array< vw_size_t > indices
Array of namespaces.
Definition: vw_example.h:84
Base class for all VW learners.
Definition: VwLearner.h:35
DynArray< char * > pairs
Pairs of features to cross for quadratic updates.
Regressor used by VW.
Definition: VwRegressor.h:37
Example class for VW.
Definition: vw_example.h:58
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
vw_size_t thread_mask
Mask used by regressor for learning.
v_array< VwFeature > atomics[256]
Array of features.
Definition: vw_example.h:86

SHOGUN Machine Learning Toolbox - Documentation