FValue inner_product(const FVector& lhs, const FVector& rhs) { if (lhs.size() >= rhs.size()) { return rhs.inner_product(lhs); } else { return lhs.inner_product(rhs); } }
void SvmSgd::get_delta_w(FVector w2, map <double, int> & delta_w) { double value; cout << "w2.size(): " << w2.size() << endl; cout << "w2[0] value: " << w2[0] << endl; for(int i = 1; i < w2.size(); i++){ value = fabs(w2[i] - w[i]); delta_w.insert(pair<double, int>(value, i - 1)); } }
void SvmSgd::output_w(const char* filename) { //TODO: output parameter w!!! VFloat *f1 = w; cout << "\n Prepare to output w: \n" ; int m = w.size(); cout << "size of m: " << m << endl; ofstream outwfd(filename); if(outwfd){ outwfd << t << endl; outwfd << wDivisor << endl; outwfd << wBias << endl; while(--m >= 0){ outwfd << *f1++ << "\t" ; } } else{ cout << t << endl; cout << wDivisor << endl; cout << wBias << endl; while(--m >= 0){ cout << *f1++ << "\t" ; } } }
void SvmSgd::calibrate(int imin, int imax, const xvec_t &xp, const yvec_t &yp) { cout << "Estimating sparsity and bscale." << endl; int j; // compute average gradient size double n = 0; double m = 0; double r = 0; FVector c(w.size()); for (j=imin; j<=imax && m<=1000; j++,n++) { const SVector &x = xp.at(j); n += 1; r += x.npairs(); const SVector::Pair *p = x; while (p->i >= 0 && p->i < c.size()) { double z = c.get(p->i) + fabs(p->v); c.set(p->i, z); m = max(m, z); p += 1; } } // bias update scaling bscale = m/n; // compute weight decay skip skip = (int) ((8 * n * w.size()) / r); cout << " using " << n << " examples." << endl; cout << " skip: " << skip << " bscale: " << setprecision(6) << bscale << endl; }
static void dLogSum(double g, const FVector &v, FVector &r) { assert(v.size() <= r.size()); dLogSum(g, v, r, v.size()); }
static double logSum(const FVector &v) { return logSum(v, v.size()); }