Skip to content
This repository
tree: 059bd826b5
Fetching contributors…

Cannot retrieve contributors at this time

file 223 lines (186 sloc) 7.275 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
/*
Copyright (c) by respective owners including Yahoo!, Microsoft, and
individual contributors. All rights reserved. Released under a BSD (revised)
license as described in the file LICENSE.
*/
#include <fstream>
#include <float.h>
#ifdef _WIN32
#include <winsock2.h>
#else
#include <netdb.h>
#endif
#include <string.h>
#include <stdio.h>
#include "parse_example.h"
#include "constant.h"
#include "sparse_dense.h"
#include "gd.h"
#include "cache.h"
#include "simple_label.h"

using namespace std;

void mf_local_predict(example* ec, regressor& reg);

float mf_inline_predict(vw& all, example* &ec)
{
  float prediction = 0.0;

  weight* weights = all.reg.weight_vectors;
  size_t mask = all.weight_mask;

  // clear stored predictions
  ec->topic_predictions.erase();

  float linear_prediction = 0;
  // linear terms
  for (size_t* i = ec->indices.begin; i != ec->indices.end; i++)
    linear_prediction += sd_add(weights,mask,ec->atomics[*i].begin, ec->atomics[*i].end);

  // store constant + linear prediction
  // note: constant is now automatically added
  push(ec->topic_predictions, linear_prediction);
  
  prediction += linear_prediction;

  // interaction terms
  for (vector<string>::iterator i = all.pairs.begin(); i != all.pairs.end();i++)
    {
      if (ec->atomics[(int)(*i)[0]].index() > 0 && ec->atomics[(int)(*i)[1]].index() > 0)
{
for (size_t k = 1; k <= all.rank; k++)
{
// x_l * l^k
// l^k is from index+1 to index+all.rank
float x_dot_l = sd_offset_add(weights, mask, ec->atomics[(int)(*i)[0]].begin, ec->atomics[(int)(*i)[0]].end, k);
// x_r * r^k
// r^k is from index+all.rank+1 to index+2*all.rank
float x_dot_r = sd_offset_add(weights, mask, ec->atomics[(int)(*i)[1]].begin, ec->atomics[(int)(*i)[1]].end, k+all.rank);

prediction += x_dot_l * x_dot_r;

// store prediction from interaction terms
push(ec->topic_predictions, x_dot_l);
push(ec->topic_predictions, x_dot_r);
}
}
    }
    
  // ec->topic_predictions has linear, x_dot_l_1, x_dot_r_1, x_dot_l_2, x_dot_r_2, ...

  return prediction;
}

void mf_inline_train(vw& all, example* &ec, float update)
{
      weight* weights = all.reg.weight_vectors;
      size_t mask = all.weight_mask;
      label_data* ld = (label_data*)ec->ld;

      // use final prediction to get update size
      // update = eta_t*(y-y_hat) where eta_t = eta/(3*t^p) * importance weight
      float eta_t = all.eta/pow(ec->example_t,all.power_t) / 3.f * ld->weight;
      update = all.loss->getUpdate(ec->final_prediction, ld->label, eta_t, 1.); //ec->total_sum_feat_sq);

      float regularization = eta_t * all.l2_lambda;

      // linear update
      for (size_t* i = ec->indices.begin; i != ec->indices.end; i++)
sd_offset_update(weights, mask, ec->atomics[*i].begin, ec->atomics[*i].end, 0, update, regularization);
      
      // quadratic update
      for (vector<string>::iterator i = all.pairs.begin(); i != all.pairs.end();i++)
{
if (ec->atomics[(int)(*i)[0]].index() > 0 && ec->atomics[(int)(*i)[1]].index() > 0)
{

// update l^k weights
for (size_t k = 1; k <= all.rank; k++)
{
// r^k \cdot x_r
float r_dot_x = ec->topic_predictions[2*k];
// l^k <- l^k + update * (r^k \cdot x_r) * x_l
sd_offset_update(weights, mask, ec->atomics[(int)(*i)[0]].begin, ec->atomics[(int)(*i)[0]].end, k, update*r_dot_x, regularization);
}

// update r^k weights
for (size_t k = 1; k <= all.rank; k++)
{
// l^k \cdot x_l
float l_dot_x = ec->topic_predictions[2*k-1];
// r^k <- r^k + update * (l^k \cdot x_l) * x_r
sd_offset_update(weights, mask, ec->atomics[(int)(*i)[1]].begin, ec->atomics[(int)(*i)[1]].end, k+all.rank, update*l_dot_x, regularization);
}

}
}
}

void mf_print_offset_features(vw& all, example* &ec, size_t offset)
{
  weight* weights = all.reg.weight_vectors;
  size_t mask = all.weight_mask;
  for (size_t* i = ec->indices.begin; i != ec->indices.end; i++)
    if (ec->audit_features[*i].begin != ec->audit_features[*i].end)
      for (audit_data *f = ec->audit_features[*i].begin; f != ec->audit_features[*i].end; f++)
{
cout << '\t' << f->space << '^' << f->feature << ':' << f->weight_index <<"(" << ((f->weight_index + offset) & mask) << ")" << ':' << f->x;

cout << ':' << weights[(f->weight_index + offset) & mask];
}
    else
      for (feature *f = ec->atomics[*i].begin; f != ec->atomics[*i].end; f++)
{
cout << '\t' << f->weight_index << ':' << f->x;
cout << ':' << weights[(f->weight_index + offset) & mask];
}
  for (vector<string>::iterator i = all.pairs.begin(); i != all.pairs.end();i++)
    if (ec->atomics[(int)(*i)[0]].index() > 0 && ec->atomics[(int)(*i)[1]].index() > 0)
      {
/* print out nsk^feature:hash:value:weight:nsk^feature^:hash:value:weight:prod_weights */
for (size_t k = 1; k <= all.rank; k++)
{
for (audit_data* f = ec->audit_features[(int)(*i)[0]].begin; f!= ec->audit_features[(int)(*i)[0]].end; f++)
for (audit_data* f2 = ec->audit_features[(int)(*i)[1]].begin; f2!= ec->audit_features[(int)(*i)[1]].end; f2++)
{
cout << '\t' << f->space << k << '^' << f->feature << ':' << ((f->weight_index+k)&mask)
<<"(" << ((f->weight_index + offset +k) & mask) << ")" << ':' << f->x;
cout << ':' << weights[(f->weight_index + offset + k) & mask];

cout << ':' << f2->space << k << '^' << f2->feature << ':' << ((f2->weight_index+k)&mask)
<<"(" << ((f2->weight_index + offset +k) & mask) << ")" << ':' << f2->x;
cout << ':' << weights[(f2->weight_index + offset + k) & mask];

cout << ':' << weights[(f->weight_index + offset + k) & mask] * weights[(f2->weight_index + offset + k) & mask];

}
}
      }
}

void mf_print_audit_features(vw& all, example* ec, size_t offset)
{
  print_result(fileno(stdout),ec->final_prediction,-1,ec->tag);
  mf_print_offset_features(all, ec, offset);
}

void mf_local_predict(vw& all, example* ec)
{
  label_data* ld = (label_data*)ec->ld;
  all.set_minmax(all.sd, ld->label);

  ec->final_prediction = finalize_prediction(all, ec->partial_prediction);

  if (ld->label != FLT_MAX)
    {
      ec->loss = all.loss->getLoss(all.sd, ec->final_prediction, ld->label) * ld->weight;
    }

  if (all.audit)
    mf_print_audit_features(all, ec, 0);

}

float mf_predict(vw& all, example* ex)
{
  float prediction = mf_inline_predict(all, ex);

  ex->partial_prediction = prediction;
  mf_local_predict(all, ex);

  return ex->final_prediction;
}

void drive_gd_mf(void* in)
{
  vw* all = (vw*)in;
  example* ec = NULL;
  
  size_t current_pass = 0;
  while ( true )
    {
      if ((ec = get_example(all->p)) != NULL)//blocking operation.
{
if (ec->pass != current_pass) {
all->eta *= all->eta_decay_rate;
save_predictor(*all, all->final_regressor_name, current_pass);
current_pass = ec->pass;
}
if (!command_example(*all, ec))
{
mf_predict(*all,ec);
if (all->training && ((label_data*)(ec->ld))->label != FLT_MAX)
mf_inline_train(*all, ec, ec->eta_round);
}
finish_example(*all, ec);
}
      else if (parser_done(all->p))
return;
      else
;//busywait when we have predicted on all examples but not yet trained on all.
    }
}
Something went wrong with that request. Please try again.