SNAP Library , Developer Reference
2013-01-07 14:03:36
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
|
#include <linalg.h>
Public Member Functions | |
TSigmoid () | |
TSigmoid (const double &A_, const double &B_) | |
TSigmoid (const TFltIntKdV &data) | |
TSigmoid (TSIn &SIn) | |
void | Load (TSIn &SIn) |
void | Save (TSOut &SOut) const |
double | GetVal (const double &x) const |
double | operator() (const double &x) const |
void | GetSigmoidAB (double &A_, double &B_) |
Static Private Member Functions | |
static double | EvaluateFit (const TFltIntKdV &data, const double A, const double B) |
static void | EvaluateFit (const TFltIntKdV &data, const double A, const double B, double &J, double &JA, double &JB) |
static void | EvaluateFit (const TFltIntKdV &data, const double A, const double B, const double U, const double V, const double lambda, double &J, double &JJ, double &JJJ) |
Private Attributes | |
TFlt | A |
TFlt | B |
TSigmoid::TSigmoid | ( | ) | [inline] |
TSigmoid::TSigmoid | ( | const double & | A_, |
const double & | B_ | ||
) | [inline] |
TSigmoid::TSigmoid | ( | const TFltIntKdV & | data | ) |
Definition at line 1530 of file linalg.cpp.
References A, B, EvaluateFit(), TVec< TVal >::Len(), and TMath::Sqr().
{ // Let z_i be the projection of the i'th training example, and y_i \in {-1, +1} be its class label. // Our sigmoid is: P(Y = y | Z = z) = 1 / [1 + e^{-Az + B}] // and we want to maximize \prod_i P(Y = y_i | Z = z_i) // = \prod_{i : y_i = 1} 1 / [1 + e^{-Az_i + B}] \prod_{i : y_i = -1} e^{-Az_i + B} / [1 + e^{-Az_i + B}] // or minimize its negative logarithm, // J(A, B) = \sum_{i : y_i = 1} ln [1 + e^{-Az_i + B}] + \sum_{i : y_i = -1} [ln [1 + e^{-Az_i + B}] - {-Az_i + B}] // = \sum_i ln [1 + e^{-Az_i + B}] - \sum_{i : y_i = -1} {-Az_i + B}. // partial J / partial A = \sum_i (-z_i) e^{-Az_i + B} / [1 + e^{-Az_i + B}] + \sum_{i : y_i = -1} Az_i. // partial J / partial B = \sum_i e^{-Az_i + B} / [1 + e^{-Az_i + B}] + \sum_{i : y_i = -1} (-1). double minProj = data[0].Key, maxProj = data[0].Key; {for (int i = 1; i < data.Len(); i++) { double zi = data[i].Key; if (zi < minProj) minProj = zi; if (zi > maxProj) maxProj = zi; }} //const bool dump = false; A = 1.0; B = 0.5 * (minProj + maxProj); double bestJ = 0.0, bestA = 0.0, bestB = 0.0, lambda = 1.0; for (int nIter = 0; nIter < 50; nIter++) { double J, JA, JB; TSigmoid::EvaluateFit(data, A, B, J, JA, JB); if (nIter == 0 || J < bestJ) { bestJ = J; bestA = A; bestB = B; } // How far should we move? //if (dump) printf("Iter %2d: A = %.5f, B = %.5f, J = %.5f, partial = (%.5f, %.5f)\n", nIter, A, B, J, JA, JB); double norm = TMath::Sqr(JA) + TMath::Sqr(JB); if (norm < 1e-10) break; const int cl = -1; // should be -1 double Jc = TSigmoid::EvaluateFit(data, A + cl * lambda * JA / norm, B + cl * lambda * JB / norm); //if (dump) printf(" At lambda = %.5f, Jc = %.5f\n", lambda, Jc); if (Jc > J) { while (lambda > 1e-5) { lambda = 0.5 * lambda; Jc = TSigmoid::EvaluateFit(data, A + cl * lambda * JA / norm, B + cl * lambda * JB / norm); //if (dump) printf(" At lambda = %.5f, Jc = %.5f\n", lambda, Jc); } } else if (Jc < J) { while (lambda < 1e5) { double lambda2 = 2 * lambda; double Jc2 = TSigmoid::EvaluateFit(data, A + cl * lambda2 * JA / norm, B + cl * lambda2 * JB / norm); //if (dump) printf(" At lambda = %.5f, Jc = %.5f\n", lambda2, Jc2); if (Jc2 > Jc) break; lambda = lambda2; Jc = Jc2; } } if (Jc >= J) break; A += cl * lambda * JA / norm; B += cl * lambda * JB / norm; //if (dump) printf(" Lambda = %.5f, new A = %.5f, new B = %.5f, new J = %.5f\n", lambda, A, B, Jc); } A = bestA; B = bestB; }
TSigmoid::TSigmoid | ( | TSIn & | SIn | ) | [inline] |
double TSigmoid::EvaluateFit | ( | const TFltIntKdV & | data, |
const double | A, | ||
const double | B | ||
) | [static, private] |
Definition at line 1472 of file linalg.cpp.
References TVec< TVal >::Len().
Referenced by TSigmoid().
{ double J = 0.0; for (int i = 0; i < data.Len(); i++) { double zi = data[i].Key; int yi = data[i].Dat; double e = exp(-A * zi + B); double denum = 1.0 + e; double prob = (yi > 0) ? (1.0 / denum) : (e / denum); J -= log(prob < 1e-20 ? 1e-20 : prob); } return J; }
void TSigmoid::EvaluateFit | ( | const TFltIntKdV & | data, |
const double | A, | ||
const double | B, | ||
double & | J, | ||
double & | JA, | ||
double & | JB | ||
) | [static, private] |
Definition at line 1486 of file linalg.cpp.
References TVec< TVal >::Len().
{ // J(A, B) = \sum_{i : y_i = 1} ln [1 + e^{-Az_i + B}] + \sum_{i : y_i = -1} [ln [1 + e^{-Az_i + B}] - {-Az_i + B}] // = \sum_i ln [1 + e^{-Az_i + B}] - \sum_{i : y_i = -1} {-Az_i + B}. // partial J / partial A = \sum_i (-z_i) e^{-Az_i + B} / [1 + e^{-Az_i + B}] + \sum_{i : y_i = -1} Az_i. // partial J / partial B = \sum_i e^{-Az_i + B} / [1 + e^{-Az_i + B}] + \sum_{i : y_i = -1} (-1). J = 0.0; double sum_all_PyNeg = 0.0, sum_all_ziPyNeg = 0.0, sum_yNeg_zi = 0.0, sum_yNeg_1 = 0.0; for (int i = 0; i < data.Len(); i++) { double zi = data[i].Key; int yi = data[i].Dat; double e = exp(-A * zi + B); double denum = 1.0 + e; double prob = (yi > 0) ? (1.0 / denum) : (e / denum); J -= log(prob < 1e-20 ? 1e-20 : prob); sum_all_PyNeg += e / denum; sum_all_ziPyNeg += zi * e / denum; if (yi < 0) { sum_yNeg_zi += zi; sum_yNeg_1 += 1; } } JA = -sum_all_ziPyNeg + sum_yNeg_zi; JB = sum_all_PyNeg - sum_yNeg_1; }
void TSigmoid::EvaluateFit | ( | const TFltIntKdV & | data, |
const double | A, | ||
const double | B, | ||
const double | U, | ||
const double | V, | ||
const double | lambda, | ||
double & | J, | ||
double & | JJ, | ||
double & | JJJ | ||
) | [static, private] |
Definition at line 1508 of file linalg.cpp.
References TVec< TVal >::Len().
{ // Let E_i = e^{-(A + lambda U) z_i + (B + lambda V)}. Then we have // J(lambda) = \sum_i ln [1 + E_i] - \sum_{i : y_i = -1} {-(A + lambda U)z_i + (B + lambda V)}. // J'(lambda) = \sum_i (V - U z_i) E_i / [1 + E_i] - \sum_{i : y_i = -1} {V - U z_i). // = \sum_i (V - U z_i) [1 - 1 / [1 + E_i]] - \sum_{i : y_i = -1} {V - U z_i). // J"(lambda) = \sum_i (V - U z_i)^2 E_i / [1 + E_i]^2. J = 0.0; JJ = 0.0; JJJ = 0.0; for (int i = 0; i < data.Len(); i++) { double zi = data[i].Key; int yi = data[i].Dat; double e = exp(-A * zi + B); double denum = 1.0 + e; double prob = (yi > 0) ? (1.0 / denum) : (e / denum); J -= log(prob < 1e-20 ? 1e-20 : prob); double VU = V - U * zi; JJ += VU * (e / denum); if (yi < 0) JJ -= VU; JJJ += VU * VU * e / denum / denum; } }
void TSigmoid::GetSigmoidAB | ( | double & | A_, |
double & | B_ | ||
) | [inline] |
double TSigmoid::GetVal | ( | const double & | x | ) | const [inline] |
void TSigmoid::Load | ( | TSIn & | SIn | ) | [inline] |
double TSigmoid::operator() | ( | const double & | x | ) | const [inline] |
void TSigmoid::Save | ( | TSOut & | SOut | ) | const [inline] |
TFlt TSigmoid::A [private] |
Definition at line 454 of file linalg.h.
Referenced by GetSigmoidAB(), GetVal(), Load(), Save(), and TSigmoid().
TFlt TSigmoid::B [private] |
Definition at line 455 of file linalg.h.
Referenced by GetSigmoidAB(), GetVal(), Load(), Save(), and TSigmoid().