SHOGUN
v3.2.0
|
00001 /* 00002 * Copyright (c) 2009 Yahoo! Inc. All rights reserved. The copyrights 00003 * embodied in the content of this file are licensed under the BSD 00004 * (revised) open source license. 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 3 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * Written (W) 2011 Shashwat Lal Das 00012 * Adaptation of Vowpal Wabbit v5.1. 00013 * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society. 00014 */ 00015 00016 #include <shogun/classifier/vw/learners/VwAdaptiveLearner.h> 00017 00018 using namespace shogun; 00019 00020 CVwAdaptiveLearner::CVwAdaptiveLearner() 00021 : CVwLearner() 00022 { 00023 } 00024 00025 CVwAdaptiveLearner::CVwAdaptiveLearner(CVwRegressor* regressor, CVwEnvironment* vw_env) 00026 : CVwLearner(regressor, vw_env) 00027 { 00028 } 00029 00030 CVwAdaptiveLearner::~CVwAdaptiveLearner() 00031 { 00032 } 00033 00034 void CVwAdaptiveLearner::train(VwExample* &ex, float32_t update) 00035 { 00036 if (fabs(update) == 0.) 00037 return; 00038 00039 vw_size_t thread_num = 0; 00040 00041 vw_size_t thread_mask = env->thread_mask; 00042 float32_t* weights = reg->weight_vectors[thread_num]; 00043 00044 float32_t g = reg->loss->get_square_grad(ex->final_prediction, ex->ld->label) * ex->ld->weight; 00045 vw_size_t ctr = 0; 00046 for (vw_size_t* i = ex->indices.begin; i != ex->indices.end; i++) 00047 { 00048 for (VwFeature *f = ex->atomics[*i].begin; f != ex->atomics[*i].end; f++) 00049 { 00050 float32_t* w = &weights[f->weight_index & thread_mask]; 00051 w[1] += g * f->x * f->x; 00052 float32_t t = f->x * CMath::invsqrt(w[1]); 00053 w[0] += update * t; 00054 } 00055 } 00056 00057 for (int32_t k = 0; k < env->pairs.get_num_elements(); k++) 00058 { 00059 char* i = env->pairs.get_element(k); 00060 00061 v_array<VwFeature> temp = ex->atomics[(int32_t)(i[0])]; 00062 temp.begin = ex->atomics[(int32_t)(i[0])].begin; 00063 temp.end = ex->atomics[(int32_t)(i[0])].end; 00064 for (; temp.begin != temp.end; temp.begin++) 00065 quad_update(weights, *temp.begin, ex->atomics[(int32_t)(i[1])], thread_mask, update, g, ex, ctr); 00066 } 00067 } 00068 00069 void CVwAdaptiveLearner::quad_update(float32_t* weights, VwFeature& page_feature, 00070 v_array<VwFeature> &offer_features, vw_size_t mask, 00071 float32_t update, float32_t g, VwExample* ex, vw_size_t& ctr) 00072 { 00073 vw_size_t halfhash = quadratic_constant * page_feature.weight_index; 00074 update *= page_feature.x; 00075 float32_t update2 = g * page_feature.x * page_feature.x; 00076 00077 for (VwFeature* elem = offer_features.begin; elem != offer_features.end; elem++) 00078 { 00079 float32_t* w = &weights[(halfhash + elem->weight_index) & mask]; 00080 w[1] += update2 * elem->x * elem->x; 00081 float32_t t = elem->x * CMath::invsqrt(w[1]); 00082 w[0] += update * t; 00083 } 00084 }